找回密码
 注册

QQ登录

只需一步,快速开始

搜索

modbus rtu串口上位机调试程序C#源码

[复制链接]
山海致远 发表于 2020-5-2 02:01:07 | 显示全部楼层 |阅读模式
1.png

modbus 串口C#源码.rar (79.71 KB, 售价: 3 E币)
  1.   /*
  2.             //读取串口中一个字节的数据
  3.             String ch = mySerialPort.ReadExisting();

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

  12.                     //在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
  13.                     //即在控件textBoxInformation的父窗口form中执行委托.
  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.                     //接收到串口头,清空数组
  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.                     //在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
  46.                     //即在控件textBoxInformation的父窗口form中执行委托.
  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.             }
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-3-29 16:39

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表