标题: C#串口MudbudRTU协议编程 [打印本页]

作者: Liruonn    时间: 2018-7-24 19:50
标题: C#串口MudbudRTU协议编程
Modbus串口通信协议


全部资料51hei下载地址:
C#串口MudbudRTU协议编程.zip (260.39 KB, 下载次数: 131)


部分源码预览:
  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.                     break;
  59.                 default:
  60.                     ReceiveData[ReceiveDataIndex] = ch;
  61.                     ReceiveDataIndex++;
  62.                     if (ReceiveDataIndex > ReceiveData.Length)
  63.                     {
  64.                         ReceiveDataIndex = ReceiveData.Length - 1;
  65.                     }
  66.                     break;
  67.             }
复制代码



作者: a425623964    时间: 2020-5-16 00:15
多谢分享,谢谢!
作者: JACKDENG288    时间: 2020-5-26 19:42
很好,多谢




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