标题: modbus上位机 Test_SerialPort c#源码 [打印本页]

作者: 木木棉百合    时间: 2018-4-16 10:28
标题: modbus上位机 Test_SerialPort c#源码
modbus上位机运行界面:


Test_SerialPort的c#源程序如下:
  1.   /*
  2.             //读取串口中一个字节的数据
  3.             String ch = mySerialPort.ReadExisting();

  4.             switch (ch)
  5.             {
  6.                 case "$":
  7.                     //接收到串口头
  8.                     ReceiveData = "";
  9.                     break;
  10.                 case "\n":
  11.                     //接收到串口尾

  12.                     //在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
  13.                     //即在控件textBoxInformation的父窗口form中执行委托.
  14.                     textBoxInformation.Invoke
  15.                     (
  16.                         new MethodInvoker
  17.                         (
  18.                             delegate
  19.                             {
  20.                                 //textBoxInformation.AppendText(ReceiveData);
  21.                                 textBoxReceiveData.Text = ReceiveData;
  22.                             }
  23.                         )
  24.                      );
  25.                     break;
  26.                 default:
  27.                     ReceiveData += ch;
  28.                     break;

  29.              }

  30.             int ch = mySerialPort.ReadByte();
  31.             string str = string.Empty;
  32.             switch (ch)
  33.             {
  34.                 case 0x12:
  35.                     //接收到串口头,清空数组
  36.                     Array.Clear(ReceiveData, 0, ReceiveData.Length);
  37.                     ReceiveDataIndex = 0;
  38.                     break;
  39.                 case 0x14:
  40.                     //接收到串口尾,输出string
  41.                     for (int i = 0; i < ReceiveData.Length; i++)
  42.                     {
  43.                         str += (ReceiveData[i] - '0').ToString();
  44.                     }
  45.                     //在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
  46.                     //即在控件textBoxInformation的父窗口form中执行委托.
  47.                     textBoxInformation.Invoke
  48.                     (
  49.                         new MethodInvoker
  50.                         (
  51.                             delegate
  52.                             {
  53.                                 //textBoxInformation.AppendText(ReceiveData);
  54.                                 textBoxReceiveData.Text = str;
  55.                             }
  56.                         )
  57.                      );
  58. ……………………

  59. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码



所有资料51hei提供下载:
串口Mudbud.rar (79.71 KB, 下载次数: 273)





作者: 875113301    时间: 2018-10-11 13:23
感谢大佬
作者: smilexxy    时间: 2018-11-21 14:13
感谢大佬分享
作者: 597543289    时间: 2019-1-19 15:59
挺好的一个学习的例子,可以修改成自己的项目程序
作者: 51cc    时间: 2019-2-14 17:05
正好参考。。
作者: xtuwz    时间: 2019-3-7 17:26
急需要,怎么获取
作者: ly81222    时间: 2019-3-13 07:45

急需要,怎么获取
作者: Alan_zcb    时间: 2019-3-27 11:04
感谢大佬,学习了。
作者: Garfieldsss    时间: 2019-4-9 21:48
感谢大佬

作者: hxa4056    时间: 2019-9-27 08:46
感谢大佬
作者: 神蜂    时间: 2020-3-29 16:20
感谢大佬,下载学习
作者: xintiandi    时间: 2020-5-11 22:05
支持一下。。。
作者: nymz    时间: 2020-6-6 22:27

感谢大佬分享




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1