C# ModBus Tcp客户端读取数据 完整Demo

简单介绍:

  项目上需要与多家公司做接口对接。我们提供接口的有,其他公司提供的接口也有。所有的接口全部对接完了,遇到一个非常棘手的问题,需要获取甲方船厂设备上的状态,就给了一个文档,文档上写了IP、端口、协议、一些地址,没有API文档,拿到手上一面懵逼,这怎么玩儿。。。。

文档如下:

 

百度百科:

  Modbus是一种串行通信协议,是Modicon公司(现在的施耐德电气 Schneider Electric)于1979年为使用可编程逻辑控制器(PLC)通信而发表。Modbus已经成为工业领域通信协议的业界标准(De facto),并且现在是工业电子设备之间常用的连接方式。

  看上去好像跟Socket差不多,本身又不是工业领域出身的,大概知道是一种工业领域通用的一套通信标准,下面直接上DEMO示例

第一步:下载类库

使用的类库已上传百度云盘:

链接:https://pan.baidu.com/s/1JtaGC0r17jjnQPMhkMKRJg
提取码:wagl

第二步:引入类库

 

第三步:引入命名空间

1 using HslCommunication.ModBus;
2 using HslCommunication;

第四步:初始化对象

1 初始化方式一、
2 private ModBusTcpClient busTcpClient = new ModBusTcpClient("192.168.1.195", 502, 0xFF);   // ip、端口、站号(默认为0xFF)
3 
4 初始化方式二、
5 private ModBusTcpClient busTcpClient = new ModBusTcpClient("192.168.1.195");   // 端口号502,站号0

第五步:开启连接

1 开启连接:
2 busTcpClient.ConnectServer();
3 
4 关闭连接:
5 busTcpClient.ConnectClose( );

第六步:读写操作

 1 private void userButton30_Click(object sender, EventArgs e)
 2 {
 3     // 读取操作
 4     bool coil100 = busTcpClient.ReadCoil("100").Content;   // 读取线圈100的通断
 5     short short100 = busTcpClient.ReadInt16("100").Content; // 读取寄存器100的short值
 6     ushort ushort100 = busTcpClient.ReadUInt16("100").Content; // 读取寄存器100的ushort值
 7     int int100 = busTcpClient.ReadInt32("100").Content;      // 读取寄存器100-101的int值
 8     uint uint100 = busTcpClient.ReadUInt32("100").Content;   // 读取寄存器100-101的uint值
 9     float float100 = busTcpClient.ReadFloat("100").Content; // 读取寄存器100-101的float值
10     long long100 = busTcpClient.ReadInt64("100").Content;    // 读取寄存器100-103的long值
11     ulong ulong100 = busTcpClient.ReadUInt64("100").Content; // 读取寄存器100-103的ulong值
12     double double100 = busTcpClient.ReadDouble("100").Content; // 读取寄存器100-103的double值
13     string str100 = busTcpClient.ReadString("100", 5).Content;// 读取100到104共10个字符的字符串
14  
15     // 写入操作
16     busTcpClient.WriteCoil("100", true);// 写入线圈100为通
17     busTcpClient.Write("100", (short)12345);// 写入寄存器100为12345
18     busTcpClient.Write("100", (ushort)45678);// 写入寄存器100为45678
19     busTcpClient.Write("100", 123456789);// 写入寄存器100-101为123456789
20     busTcpClient.Write("100", (uint)123456778);// 写入寄存器100-101为123456778
21     busTcpClient.Write("100", 123.456);// 写入寄存器100-101为123.456
22     busTcpClient.Write("100", 12312312312414L);//写入寄存器100-103为一个大数据
23     busTcpClient.Write("100", 12634534534543656UL);// 写入寄存器100-103为一个大数据
24     busTcpClient.Write("100", 123.456d);// 写入寄存器100-103为一个双精度的数据
25     busTcpClient.Write("100", "K123456789");
26      
27 }

项目界面:

完整代码(粗略写了DEMO,有不规范的地方,大佬莫怪):

  1 using System;
  2 using System.Windows.Forms;
  3 using HslCommunication.ModBus;
  4 using HslCommunication;
  5 
  6 namespace ModbusDemo
  7 {
  8     public partial class Form1 : Form
  9     {
 10         public Form1()
 11         {
 12             InitializeComponent();
 13         }
 14         /// <summary>
 15         /// 初始化类
 16         /// </summary>
 17         private ModbusTcpNet busTcpClient =null;
 18         /// <summary>
 19         /// 监听状态
 20         /// </summary>
 21         private bool IsEnable = false;
 22         private void Form1_Load(object sender, EventArgs e)
 23         {
 24             txtPort.Text = "502";
 25             txtIp.Text = "172.30.16.220";
 26             textBox2.Text = "00141";
 27         }
 28         /// <summary>
 29         /// 开启服务
 30         /// </summary>
 31         /// <param name="sender"></param>
 32         /// <param name="e"></param>
 33         private void Button5_Click(object sender, EventArgs e)
 34         {
 35             try
 36             {
 37                 if (IsEnable)
 38                 {
 39                     MessageBox.Show("请勿重复建立连接!");
 40                     return;
 41                 }
 42                 string ip = txtIp.Text.Trim();
 43                 int port = Convert.ToInt32(txtPort.Text);
 44                 if (ip==null || ip=="")
 45                 {
 46                     MessageBox.Show("ip不能为空!");
 47                     return;
 48                 }
 49                 busTcpClient = new ModbusTcpNet(ip, port, 0x01);
 50                 OperateResult res = busTcpClient.ConnectServer();
 51                 if (res.IsSuccess==true) //接收状态返回值
 52                 {
 53                     IsEnable = true;
 54                     MessageBox.Show("开启连接成功");
 55                 }
 56                 else
 57                 {
 58                     MessageBox.Show("开启连接失败");
 59                 }
 60             }
 61             catch (Exception ex)
 62             {
 63                 MessageBox.Show("开启连接失败!", ex.Message.ToString());
 64             }
 65         }        
 66         private void Button6_Click(object sender, EventArgs e)
 67         {
 68             try
 69             {
 70                 if (!IsEnable)
 71                 {
 72                     MessageBox.Show("尚未建立连接!");
 73                     return;
 74                 }
 75                 busTcpClient.ConnectClose();
 76                 IsEnable = false;
 77                 MessageBox.Show("关闭连接成功!");
 78             }
 79             catch (Exception ex)
 80             {
 81                 MessageBox.Show("关闭连接失败!", ex.Message.ToString());
 82             }
 83         }
 84 
 85         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 86         {
 87             Application.Exit();
 88         }
 89 
 90         private void Button3_Click(object sender, EventArgs e)
 91         {
 92             try
 93             {
 94                 if (!IsEnable)
 95                 {
 96                     MessageBox.Show("尚未建立连接!");
 97                     return;
 98                 }
 99                 if (busTcpClient == null)
100                 {
101                     MessageBox.Show("尚未初始化对象!");
102                     return;
103                 }
104                 string txt = textBox2.Text.Trim();
105                 if (txt=="")
106                 {
107                     MessageBox.Show("地址不能为空!");
108                     return;
109                 }
110                 bool coil100 = busTcpClient.ReadCoil(txt).Content;   // 读取线圈100的通断
111                 textBox1.Text = "";
112                 MessageBox.Show("监听成功!");
113                 textBox1.Text = coil100 == true ? "true" : "false";
114             }
115             catch (Exception ex)
116             {
117                 MessageBox.Show(ex.Message.ToString());
118             }
119         }
120     }
121 }

项目:

链接:https://pan.baidu.com/s/1a3pnftQ2QAZFJcoKl9p_qQ
提取码:rhsl

posted @ 2019-08-22 15:30  陈彦斌  阅读(23055)  评论(6编辑  收藏  举报