🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.
本文目录
- 1. 引用组件
- 2. ModBusTcp读写Operation
- 3. ModBusRtu读写Operation
- 4. ModBusAscii读写Operation
- 5. ModbusRtuOverTcp读写Operation
- 6. SiemensClient(西门子)读写Operation
- 7. 注意:关于西门子的PLCaddress
- 8. 西门子客户最佳实践
- 9. MitsubishiClient(三菱)读写Operation
- 10. OmronFinsClient(欧姆龙)读写Operation
- 11. AllenBradleyClient(罗克韦尔)读写Operation
- 12. IoTClient Tool 桌面程序Tools(开源)
- 13. IoTClient Tool桌面程序Tools,address开源。
- 14. iotgateway(开源)
- 15. 物联网网关
- 16. 能源管理system()
- 17. 能源管理-现场-单项目
- 18. 能源管理-云端-多项目
- 19. 能源管理-移动端
- 20. 海底捞即将控制()
- 21. 海底捞终止控制-web
- 22. 海底捞终止控制-移动端
IoTClient是一个物联网设备通讯协议实现客户端,将包括主流PLC通信读取、ModBus协议、Bacnet协议等常用工业通讯协议。本组件基于.NET Standard 2.0,可用于.Net的跨平台开发,如Windows、Linux甚至可运行于树莓派上。
使用说明
引用组件
Install-Package IoTClient
或图形化安装

ModBusTcp读写Operation
//1、实例化客户端 - 输入正确的IP和端口
ModBusTcpClient client = new ModBusTcpClient("127.0.0.1", 502);
//2、写Operation - 参数依次是:address 、值 、站号 、function code
client.Write("4", (short)33, 2, 16);
//2.1、【注意】写入data的时候需要明确data type
client.Write("0", (short)33, 2, 16); //写入shortTypenumerical value
client.Write("4", (ushort)33, 2, 16); //写入ushortTypenumerical value
client.Write("8", (int)33, 2, 16); //写入intTypenumerical value
client.Write("12", (uint)33, 2, 16); //写入uintTypenumerical value
client.Write("16", (long)33, 2, 16); //写入longTypenumerical value
client.Write("20", (ulong)33, 2, 16); //写入ulongTypenumerical value
client.Write("24", (float)33, 2, 16); //写入floatTypenumerical value
client.Write("28", (double)33, 2, 16); //写入doubleTypenumerical value
client.Write("32", true, 2, 5); //写入线圈Type值
client.Write("100", "orderCode", stationNumber); //写入字符串
//3、读Operation - 参数依次是:address 、站号 、function code
var value = client.ReadInt16("4", 2, 3).Value;
//3.1、其他Typedata读取
client.ReadInt16("0", stationNumber, 3); //shortTypedata读取
client.ReadUInt16("4", stationNumber, 3); //ushortTypedata读取
client.ReadInt32("8", stationNumber, 3); //intTypedata读取
client.ReadUInt32("12", stationNumber, 3); //uintTypedata读取
client.ReadInt64("16", stationNumber, 3); //longTypedata读取
client.ReadUInt64("20", stationNumber, 3); //ulongTypedata读取
client.ReadFloat("24", stationNumber, 3); //floatTypedata读取
client.ReadDouble("28", stationNumber, 3); //doubleTypedata读取
client.ReadCoil("32", stationNumber, 1); //线圈Typedata读取
client.ReadDiscrete("32", stationNumber, 2);//离散Typedata读取
client.ReadString("100", stationNumber,10); //读取字符串
//4、如果没有主动Open,则会每次读写Operation的时候自动打开自动和Closeconnect,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();
//5、读写Operation都会返回Operation结果对象Result
var result = client.ReadInt16("4", 2, 3);
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取Operation实际send的Requestmessage
var requst = result.Requst;
//5.4 读取Operation服务端Response的message
var response = result.Response;
//5.5 读取到的值
var value3 = result.Value;
//6、批量读取
var list = new List<ModBusInput>();
list.Add(new ModBusInput()
{
Address = "2",
DataType = DataTypeEnum.Int16,
FunctionCode = 3,
StationNumber = 1
});
list.Add(new ModBusInput()
{
Address = "2",
DataType = DataTypeEnum.Int16,
FunctionCode = 4,
StationNumber = 1
});
list.Add(new ModBusInput()
{
Address = "199",
DataType = DataTypeEnum.Int16,
FunctionCode = 3,
StationNumber = 1
});
var result = client.BatchRead(list);
//7、构造函数其他参数
//IP、端口、超时time、大小端设置
ModBusTcpClient client = new ModBusTcpClient("127.0.0.1", 502, 1500, EndianFormat.ABCD);
ModBusTcpMore使用方式,请参考单元测试
ModBusRtu读写Operation
//实例化客户端 - [COM port名称,Baud rate,data bit,stop bit,奇even parity]
ModBusRtuClient client = new ModBusRtuClient("COM3", 9600, 8, StopBits.One, Parity.None);
//其他读写Operation和ModBusTcpClient的读写Operation一致
ModBusAscii读写Operation
//实例化客户端 - [COM port名称,Baud rate,data bit,stop bit,奇even parity]
ModbusAsciiClient client = new ModbusAsciiClient("COM3", 9600, 8, StopBits.One, Parity.None);
//其他读写Operation和ModBusTcpClient的读写Operation一致
ModbusRtuOverTcp读写Operation
//串口透传 即:用Tcp的方式sendRtuformatmessage
//实例化客户端 - IP、端口、超时time、大小端设置
ModbusRtuOverTcpClient client = new ModbusRtuOverTcpClient("127.0.0.1", 502, 1500, EndianFormat.ABCD);
//其他读写Operation和ModBusTcpClient的读写Operation一致
SiemensClient(西门子)读写Operation
//1、实例化客户端 - 输入型号、IP和端口
//其他型号:SiemensVersion.S7_200、SiemensVersion.S7_300、SiemensVersion.S7_400、SiemensVersion.S7_1200、SiemensVersion.S7_1500
SiemensClient client = new SiemensClient(SiemensVersion.S7_200Smart, "127.0.0.1",102);
//2、写Operation
client.Write("Q1.3", true);
client.Write("V2205", (short)11);
client.Write("V2209", 33);
client.Write("V2305", "orderCode"); //写入字符串
//3、读Operation
var value1 = client.ReadBoolean("Q1.3").Value;
var value2 = client.ReadInt16("V2205").Value;
var value3 = client.ReadInt32("V2209").Value;
var value4 = client.ReadString("V2305").Value; //读取字符串
//4、如果没有主动Open,则会每次读写Operation的时候自动打开自动和Closeconnect,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();
//5、读写Operation都会返回Operation结果对象Result
var result = client.ReadInt16("V2205");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取Operation实际send的Requestmessage
var requst = result.Requst;
//5.4 读取Operation服务端Response的message
var response = result.Response;
//5.5 读取到的值
var value4 = result.Value;
注意:关于西门子的PLCaddress
VB263、VW263、VD263中的B、W、D分别表示:byte型(8位)、word型(16位)、doubleword型(32位)。
在本组件传入address的时候不需要带data type,直接使用对应方法读取对应Type即可,如:
VB263 - client.ReadByte("V263")
VD263 - client.ReadFloat("V263")
VD263 - client.ReadInt32("V263")
DB108.DBW4 - client.ReadUInt16("DB108.4")
DB1.DBX0.0 - client.ReadBoolean("DB1.0.0")
DB1.DBD0 - client.ReadFloat("DB1.0")
| C#data type | 智能200 | 1200/1500/300 |
|---|---|---|
| 少量 | V1.0 | DB1.DBX1.0 |
| byte | VB1 | DB1.DBB1 |
| 短 ushort |
大众2 | DB1.DBW2 |
| int uint 浮点数 |
VD4 | DB1.DBD4 |
西门子客户最佳实践
1、什么时候不要主动Open
西门子plc一般最多允许8个长connect。所以当connect数不够用的时候或者做测试的时候就不要主动Open,这样组件会自动Open并即时Close。
2、什么时候主动Open
当长connectquantity还够用,且想要提升读写性能。
3、除了主动Openconnect,还可以通过批量读写,大幅提升读写性能。
//批量读取
Dictionary<string, DataTypeEnum> addresses = new Dictionary<string, DataTypeEnum>();
addresses.Add("DB4.24", DataTypeEnum.Float);
addresses.Add("DB1.434.0", DataTypeEnum.Bool);
addresses.Add("V4109", DataTypeEnum.Byte);
...
var result = client.BatchRead(addresses);
//批量写入
Dictionary<string, object> addresses = new Dictionary<string, object>();
addresses.Add("DB4.24", (float)1);
addresses.Add("DB4.0", (float)2);
addresses.Add("DB1.434.0", true);
...
var result = client.BatchWrite(addresses);
4、【注意】写入data的时候需要明确data type
client.Write("DB4.12", 9); //写入的是intType
client.Write("DB4.12", (float)9); //写入的是floatType
5、SiemensClient是线程安全类
由于plc长connect有限,SiemensClient被设计成线程安全类。可以把SiemensClient设置成单例,在多个线程之间使用SiemensClient的实例读写Operationplc。
MitsubishiClient(三菱)读写Operation
//1、实例化客户端 - 输入正确的IP和端口
MitsubishiClient client = new MitsubishiClient(MitsubishiVersion.Qna_3E, "127.0.0.1",6000);
//2、写Operation
client.Write("M100", true);
client.Write("D200", (short)11);
client.Write("D210", 33);
//3、读Operation
var value1 = client.ReadBoolean("M100").Value;
var value2 = client.ReadInt16("D200").Value;
var value3 = client.ReadInt32("D210").Value;
//4、如果没有主动Open,则会每次读写Operation的时候自动打开自动和Closeconnect,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();
//5、读写Operation都会返回Operation结果对象Result
var result = client.ReadInt16("D210");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取Operation实际send的Requestmessage
var requst = result.Requst;
//5.4 读取Operation服务端Response的message
var response = result.Response;
//5.5 读取到的值
var value4 = result.Value;
OmronFinsClient(欧姆龙)读写Operation
//1、实例化客户端 - 输入正确的IP和端口
OmronFinsClient client = new OmronFinsClient("127.0.0.1",6000);
//2、写Operation
client.Write("M100", true);
client.Write("D200", (short)11);
client.Write("D210", 33);
//3、读Operation
var value1 = client.ReadBoolean("M100").Value;
var value2 = client.ReadInt16("D200").Value;
var value3 = client.ReadInt32("D210").Value;
//4、如果没有主动Open,则会每次读写Operation的时候自动打开自动和Closeconnect,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();
//5、读写Operation都会返回Operation结果对象Result
var result = client.ReadInt16("D210");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取Operation实际send的Requestmessage
var requst = result.Requst;
//5.4 读取Operation服务端Response的message
var response = result.Response;
//5.5 读取到的值
var value4 = result.Value;
AllenBradleyClient(罗克韦尔)读写Operation
//1、实例化客户端 - 输入正确的IP和端口
AllenBradleyClient client = new AllenBradleyClient("127.0.0.1",44818);
//2、写Operation
client.Write("A1", (short)11);
//3、读Operation
var value = client.ReadInt16("A1").Value;
//4、如果没有主动Open,则会每次读写Operation的时候自动打开自动和Closeconnect,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();
//5、读写Operation都会返回Operation结果对象Result
var result = client.ReadInt16("A1");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取Operation实际send的Requestmessage
var requst = result.Requst;
//5.4 读取Operation服务端Response的message
var response = result.Response;
//5.5 读取到的值
var value4 = result.Value;
基于IoTClient库的一些项目
IoTClient Tool 桌面程序Tools(开源)
所属个人:农码一生
IoTClient Tool桌面程序Tools,address开源。
- 1、可用于测试PLC及Related协议的通信
- 2、可作为IoTClient库使用示例。










iotgateway(开源)
所属个人:iioter
物联网网关

能源管理system()
公司名称:擎纳尔科技
能源管理-现场-单项目


能源管理-云端-多项目






能源管理-移动端







海底捞即将控制()
公司名称:擎纳尔科技
海底捞终止控制-web




海底捞终止控制-移动端




发表回复