找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4739|回复: 2
打印 上一主题 下一主题
收起左侧

C#串口MudbudRTU协议编程

[复制链接]
跳转到指定楼层
楼主
Modbus串口通信协议


全部资料51hei下载地址:
C#串口MudbudRTU协议编程.zip (260.39 KB, 下载次数: 131)


部分源码预览:
  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好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:165292 发表于 2020-5-16 00:15 | 只看该作者
多谢分享,谢谢!
回复

使用道具 举报

板凳
ID:404756 发表于 2020-5-26 19:42 | 只看该作者
很好,多谢
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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