標(biāo)題:
C#串口MudbudRTU協(xié)議編程
[打印本頁]
作者:
Liruonn
時間:
2018-7-24 19:50
標(biāo)題:
C#串口MudbudRTU協(xié)議編程
Modbus串口通信協(xié)議
0.png
(43.21 KB, 下載次數(shù): 104)
下載附件
2018-7-24 23:38 上傳
全部資料51hei下載地址:
C#串口MudbudRTU協(xié)議編程.zip
(260.39 KB, 下載次數(shù): 131)
2018-7-24 19:50 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
部分源碼預(yù)覽:
/*
//讀取串口中一個字節(jié)的數(shù)據(jù)
String ch = mySerialPort.ReadExisting();
switch (ch)
{
case "$":
//接收到串口頭
ReceiveData = "";
break;
case "\n":
//接收到串口尾
//在擁有此控件的基礎(chǔ)窗口句柄的線程上執(zhí)行委托Invoke(Delegate)
//即在控件textBoxInformation的父窗口form中執(zhí)行委托.
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:
//接收到串口頭,清空數(shù)組
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();
}
//在擁有此控件的基礎(chǔ)窗口句柄的線程上執(zhí)行委托Invoke(Delegate)
//即在控件textBoxInformation的父窗口form中執(zhí)行委托.
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;
}
復(fù)制代碼
作者:
a425623964
時間:
2020-5-16 00:15
多謝分享,謝謝!
作者:
JACKDENG288
時間:
2020-5-26 19:42
很好,多謝
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1