標(biāo)題: C#串口MudbudRTU協(xié)議編程 [打印本頁]

作者: Liruonn    時間: 2018-7-24 19:50
標(biāo)題: C#串口MudbudRTU協(xié)議編程
Modbus串口通信協(xié)議


全部資料51hei下載地址:
C#串口MudbudRTU協(xié)議編程.zip (260.39 KB, 下載次數(shù): 131)


部分源碼預(yù)覽:
  1.   /*
  2.             //讀取串口中一個字節(jié)的數(shù)據(jù)
  3.             String ch = mySerialPort.ReadExisting();

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

  12.                     //在擁有此控件的基礎(chǔ)窗口句柄的線程上執(zhí)行委托Invoke(Delegate)
  13.                     //即在控件textBoxInformation的父窗口form中執(zhí)行委托.
  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.                     //接收到串口頭,清空數(shù)組
  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.                     //在擁有此控件的基礎(chǔ)窗口句柄的線程上執(zhí)行委托Invoke(Delegate)
  46.                     //即在控件textBoxInformation的父窗口form中執(zhí)行委托.
  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.             }
復(fù)制代碼



作者: a425623964    時間: 2020-5-16 00:15
多謝分享,謝謝!
作者: JACKDENG288    時間: 2020-5-26 19:42
很好,多謝




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1