国产免费AV|泡泡玛特欧洲总部将设在伦敦|中文天堂网www新版资源在线|一本久道综合在线中文|国精产品一二三产区的使用方法|香蕉鱼在线观看|www.27eee

 找回密碼
 注冊
搜索

modbus rtu串口上位機調(diào)試程序C#源碼

[復(fù)制鏈接]
樓主
山海致遠 發(fā)表于 2020-5-2 02:01:07 | 只看該作者 |只看大圖 |倒序瀏覽 |閱讀模式
modbus rtu串口上位機調(diào)試程序C#源碼 1.png

modbus 串口C#源碼.rar (79.71 KB, 售價: 3 E幣)
  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ù)制代碼


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

本版積分規(guī)則

手機版|小黑屋|ELEOK |網(wǎng)站地圖

GMT+8, 2026-5-26 07:16

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回復(fù) 返回頂部 返回列表