15. Byte[] receivedData = new Byte[sp1.BytesToRead]; //创建接收字节数组 16. sp1.Read(receivedData, 0, receivedData.Length); //读取数据
17. sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer 18. string strRcv = null; 19.
20. for (int i = 0; i < receivedData.Length; i++) //窗体显示 21. {
22.
23. strRcv += receivedData[i].ToString(\进制显示 24. }
25. txtReceive.Text += strRcv + \26. }
27. catch (System.Exception ex) 28. {
29. MessageBox.Show(ex.Message, \出错提示\30. txtSend.Text = \
31. } 32. } 33. } 34. else 35. {
36. MessageBox.Show(\请打开某个串口\错误提示\37. } 38. }
为了友好和美观,我将当前时间也显示出来,又将显示字体的颜色做了修改:
[csharp] view plaincopyprint?
1. 3. txtReceive.Text += dt.GetDateTimeFormats('f')[0].ToString() + \ 4. txtReceive.SelectAll(); 5. txtReceive.SelectionColor = Color.Blue; //改变字体的颜色 做到这里,大部分功能就已实现了,剩下的工作就是些简单的操作设置了,有保存设置、定时发送信息、控制文本框输入内容等。 六、保存设置 这部分相对简单,但当时我没接触过,也花了点时间,现在想想,也不过如此。 保存用户设置用ini文件是个不错的选择,虽然大部分都用注册表实现,但ini文件保存还是有比较广泛的使用。 .ini 文件是Initialization File的缩写,也就是初始化文件。 为了不偏离正题,也不过多说明,可参考相关内容(网上资源都不错,因人而异,就不加链接了)。 使用Inifile读写ini文件,这里我用到了两个主要方法: [csharp] view plaincopyprint? 1. //读出ini文件 2. a:=inifile.Readstring('节点','关键字',缺省值);// string类型 3. b:=inifile.Readinteger('节点','关键字',缺省值);// integer类型 4. c:=inifile.Readbool('节点','关键字',缺省值);// boolean类型 5. 其中[缺省值]为该INI文件不存在该关键字时返回的缺省值。 6. //写入INI文件: 7. inifile.writestring('节点','关键字',变量或字符串值); 8. inifile.writeinteger('节点','关键字',变量或整型值); 9. inifile.writebool('节点','关键字',变量或True或False); 请看代码: [csharp] view plaincopyprint? 1. //using 省写了 2. 3. 4. 5. 6. 7. namespace INIFILE { class Profile { public static void LoadProfile() { 8. string strPath = AppDomain.CurrentDomain.BaseDirectory; 9. _file = new IniFile(strPath + \ 10. G_BAUDRATE = _file.ReadString(\0\读数据,下同 11. G_DATABITS = _file.ReadString(\12. G_STOP = _file.ReadString(\ 13. G_PARITY = _file.ReadString(\14. 15. } 16. 17. public static void SaveProfile() 18. { 19. string strPath = AppDomain.CurrentDomain.BaseDirectory; 20. _file = new IniFile(strPath + \ 21. _file.WriteString(\ //写数据,下同 22. _file.WriteString(\23. _file.WriteString(\ 24. _file.WriteString(\25. } 26. 27. private static IniFile _file;//内置了一个对象 28. 29. public static string G_BAUDRATE = \给ini文件赋新值,并且影响界面下拉框的显示 30. public static string G_DATABITS = \31. public static string G_STOP = \ 32. public static string G_PARITY = \ 33. } 34. } _file声明成了内置对象,可以方便各函数的调用。 下面是“保存设置”的部分代码: [csharp] view plaincopyprint? 1. private void btnSave_Click(object sender, EventArgs e) 2. { 3. 4. //设置各“串口设置” 5. string strBaudRate = cbBaudRate.Text; 6. string strDateBits = cbDataBits.Text; 7. string strStopBits = cbStop.Text; 8. Int32 iBaudRate = Convert.ToInt32(strBaudRate); 9. Int32 iDateBits = Convert.ToInt32(strDateBits); 10. 11. 12. Profile.G_BAUDRATE = iBaudRate+\波特率 13. Profile.G_DATABITS = iDateBits+\数据位 14. switch (cbStop.Text) //停止位 15. { 16. case \ 17. Profile.G_STOP = \18. break; 19. case \ 20. Profile.G_STOP = \21. break; 22. //防止过多刷屏,下面省写了 23. …… 24. } 25. switch (cbParity.Text) //校验位 26. { 27. case \无\ 28. Profile.G_PARITY = \ 29. break; 30. ………… 31. } 32. Profile.SaveProfile(); //保存设置 33. } 读取ini文件主要在加载窗体时执行: INIFILE.Profile.LoadProfile();//加载所有 七、控制文本输入这里倒挺简单,只是注意一点。当我们控制输入非法字符时,可通过控制e.Handed的属性值实现,注意这里的Handed属性是“操作过”的含义,而非“执行此处操作”之意,Handled是过去式,看字面意思,\操作过的=是;\,将这个操作的状态设为已处理过,自然就不会再处理了。具体参见MSDN:Handed [csharp] view plaincopyprint? 1. private void txtSend_KeyPress(object sender, KeyPressEventArgs e) 2. { 3. if (radio1.Checked== true) 4. { 5. //正则匹配 6. string patten = \//“\\b”:退格键 7. Regex r = new Regex(patten); 8. Match m = r.Match(e.KeyChar.ToString()); 9. 10. 11. if (m.Success )//&&(txtSend.Text.LastIndexOf(\Text.Length-1)) 12. { 13. e.Handled = false; 14. } 15. else 16. { 17. e.Handled = true; 18. } 19. }//end of radio1 八、定时发送信息 这边看似很简单,但也有一点需要注意,当定时器生效时,我们要间隔访问“发送”按键的内容,怎么实现?还好MS给我们提供了必要的支持,使用Button的 PerformClick可以轻松做到, PerformClick参见MSDN:PerformClick [csharp] view plaincopyprint? 1. private void tmSend_Tick(object sender, EventArgs e) 2. { 3. //转换时间间隔 4. 5. 6. 7. string strSecond = txtSecond.Text; try { int isecond = int.Parse(strSecond) * 1000;//Interval以微秒为单位 8. tmSend.Interval = isecond; 9. if (tmSend.Enabled == true) 10. { 11. btnSend.PerformClick(); //产生“发送”的click事件 12. } 13. } 14. catch (System.Exception ex) 15. { 16. MessageBox.Show(\错误的定时输入!\17. } 18. 19. } 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库c#串口通信(3)在线全文阅读。
相关推荐: