赞
踩
关于ModbusTCP通信的内容。
本文主要以C#作为编程语言,结合目前市场上常用的5种通信库,分别与台达DVP-32 PLC进行通信研究,并对研究的结论进行分享。
一、EasyModbus
简介:EasyModbus支持Modbus TCP, Modbus UDP and Modbus RTU,开源协议为MIT。
二、SharpModbus
简介:SharpModbus是一个开源的Modbus工具,支持ModbusTCP与ModbusRTU,开源协议为MIT。
三、NModbus4
简介:NModbus4是一个开源的Modbus通信库,开源协议为MIT。
四、HslCommunication
简介:HslCommunication是一个商业通信库,涵盖各种不同设备的通信。
五、xktComm
简介:xktComm是一个商业通信库,涵盖西门子、欧姆龙、三菱、基恩士等PLC,也支持Modbus及OPC通信。
- public enum ModbusLib
- {
- EasyModbus,
- SharpModbus,
- NModbus4,
- hslCommunication,
- xktComm
- }
-
- //创建对象
-
- //EasyModbus
- private ModbusClient easyModbus = new ModbusClient();
-
- //SharpModbus
- private ModbusMaster sharpModbus;
-
- //NModbus4
- private ModbusIpMaster nModbus;
-
- //HslCommunication
- private ModbusTcpNet hsl;
-
- //xktComm
- private ModbusTcp xktModbus = new ModbusTcp();
-
- private bool Connect(ModbusLib modbusLib)
- {
- try
- {
- switch (modbusLib)
- {
- case ModbusLib.EasyModbus:
- easyModbus.Connect(ipAddress, port);
- break;
- case ModbusLib.SharpModbus:
- sharpModbus = ModbusMaster.TCP(ipAddress, port);
- break;
- case ModbusLib.NModbus4:
- TcpClient tcpClient = new TcpClient();
- tcpClient.Connect(ipAddress, port);
- nModbus = ModbusIpMaster.CreateIp(tcpClient);
- break;
- case ModbusLib.hslCommunication:
- hsl = new ModbusTcpNet(ipAddress, port);
- return hsl.ConnectServer().IsSuccess;
- case ModbusLib.xktComm:
- return xktModbus.Connect(ipAddress, port);
- default:
- break;
- }
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- private void DisConnect(ModbusLib modbusLib)
- {
- switch (modbusLib)
- {
- case ModbusLib.EasyModbus:
- easyModbus.Disconnect();
- break;
- case ModbusLib.SharpModbus:
- sharpModbus.Dispose();
- break;
- case ModbusLib.NModbus4:
- nModbus.Dispose();
- break;
- case ModbusLib.hslCommunication:
- hsl.ConnectClose();
- break;
- case ModbusLib.xktComm:
- xktModbus.DisConnect();
- break;
- default:
- break;
- }
- }
- public bool ReadSingleReg(ModbusLib modbusLib)
- {
- try
- {
- switch (modbusLib)
- {
- case ModbusLib.EasyModbus:
- return easyModbus.ReadHoldingRegisters(0, 1).Length == 1;
- case ModbusLib.SharpModbus:
- return sharpModbus.ReadHoldingRegisters(1, 0, 1).Length == 1;
- case ModbusLib.NModbus4:
- return nModbus.ReadHoldingRegisters(0, 1).Length == 1;
- case ModbusLib.hslCommunication:
- return hsl.ReadInt16("0").IsSuccess;
- case ModbusLib.xktComm:
- return xktModbus.ReadKeepReg(0, 1).Length == 2;
- default:
- return false;
- }
- }
- catch (Exception)
- {
- return false;
- }
- }
- private void modbusCtl1_Start(object sender, EventArgs e)
- {
- if (sender is ModbusCtl control)
- {
- ModbusLib modbusLib = control.ModbusType;
- if (Connect(modbusLib))
- {
- cts = new CancellationTokenSource();
- stopwatch.Restart();
- int successCount = 0;//成功次数
- int totalCount = 0;//总次数
- Task.Run(() =>
- {
- while (!cts.IsCancellationRequested)
- {
- if (ReadSingleReg(modbusLib))
- {
- successCount++;
- }
- totalCount++;
- if (successCount != 0 && totalCount != 0)
- {
- control.Result = successCount.ToString() + "/" + totalCount.ToString();
- control.CommRate = stopwatch.ElapsedMilliseconds / successCount;
- }
- }
- }, cts.Token);
- cts.Token.Register(() =>
- {
- stopwatch.Stop();
- Thread.Sleep(100);
- DisConnect(modbusLib);
- });
- }
- }
- }
通过以上测试发现,ModbusTCP通信周期与ModbusTCP服务器(PLC硬件)紧密关联,甚至可以达到毫秒级。
可能有的小伙伴会感到疑惑,为什么SharpModbus连接台达PLC那么慢,连接其他PLC都正常呢?
发布于 2021-05-06 10:27
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。