找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4757|回復: 2
收起左側

C#串口MudbudRTU協(xié)議編程

[復制鏈接]
ID:368428 發(fā)表于 2018-7-24 19:50 | 顯示全部樓層 |閱讀模式
Modbus串口通信協(xié)議
0.png

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


部分源碼預覽:
  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.                     //在擁有此控件的基礎窗口句柄的線程上執(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.                     //在擁有此控件的基礎窗口句柄的線程上執(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.             }
復制代碼


回復

使用道具 舉報

ID:165292 發(fā)表于 2020-5-16 00:15 | 顯示全部樓層
多謝分享,謝謝!
回復

使用道具 舉報

ID:404756 發(fā)表于 2020-5-26 19:42 | 顯示全部樓層
很好,多謝
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表