Modbus串口通信協議
0.png (43.21 KB, 下載次數: 104)
下載附件
2018-7-24 23:38 上傳
全部資料51hei下載地址:
C#串口MudbudRTU協議編程.zip
(260.39 KB, 下載次數: 131)
2018-7-24 19:50 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
部分源碼預覽:
- /*
- //讀取串口中一個字節的數據
- 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;
- }
復制代碼
|