Modbus串口通信协议
全部资料51hei下载地址:
C#串口MudbudRTU协议编程.zip
(260.39 KB, 下载次数: 131)
部分源码预览:
- /*
- //读取串口中一个字节的数据
- String ch = mySerialPort.ReadExisting();
- switch (ch)
- {
- case "$":
- //接收到串口头
- ReceiveData = "";
- break;
- case "\n":
- //接收到串口尾
- //在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
- //即在控件textBoxInformation的父窗口form中执行委托.
- textBoxInformation.Invoke
- (
- new MethodInvoker
- (
- delegate
- {
- //textBoxInformation.AppendText(ReceiveData);
- textBoxReceiveData.Text = ReceiveData;
- }
- )
- );
- break;
- default:
- ReceiveData += ch;
- break;
- }
- int ch = mySerialPort.ReadByte();
- string str = string.Empty;
- switch (ch)
- {
- case 0x12:
- //接收到串口头,清空数组
- Array.Clear(ReceiveData, 0, ReceiveData.Length);
- ReceiveDataIndex = 0;
- break;
- case 0x14:
- //接收到串口尾,输出string
- for (int i = 0; i < ReceiveData.Length; i++)
- {
- str += (ReceiveData[i] - '0').ToString();
- }
- //在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
- //即在控件textBoxInformation的父窗口form中执行委托.
- textBoxInformation.Invoke
- (
- new MethodInvoker
- (
- delegate
- {
- //textBoxInformation.AppendText(ReceiveData);
- textBoxReceiveData.Text = str;
- }
- )
- );
- break;
- default:
- ReceiveData[ReceiveDataIndex] = ch;
- ReceiveDataIndex++;
- if (ReceiveDataIndex > ReceiveData.Length)
- {
- ReceiveDataIndex = ReceiveData.Length - 1;
- }
- break;
- }
复制代码
|