IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #缩略图

IoTClient is a communication protocol implementation client for IoT devices, which includes commonly used industrial communication protocols such as mainstream PLC communication reading, ModBus protocol, Bacnet protocol, etc. This component is based on NET Standard 2.0, Can be used for Net's cross platform development, such as Windows, Linux, and even running on Raspberry Pi.

Instructions

Reference Component

Nuget Installation 

Install-Package IoTClient

or Graphic Installation

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图

ModBusTcp Read and Write Operations

//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();
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);

For more usage of ModBusTcp, please refer toUnit Testing

ModBusRtu Read and Write Operations

//实例化客户端 - [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 Read and Write Operations

//实例化客户端 - [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 Read and Write Operations

//串口透传 即:用Tcp的方式sendRtuformatmessage

//实例化客户端 - IP、端口、超时time、大小端设置
ModbusRtuOverTcpClient client = new ModbusRtuOverTcpClient("127.0.0.1", 502, 1500, EndianFormat.ABCD);

//其他读写Operation和ModBusTcpClient的读写Operation一致

Siemens Client Read and Write Operations

//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;

Attention: Regarding Siemens PLC address

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 Intelligent 200 1200/1500/300
Small amount V1.0 DB1.DBX1.0
bytes VB1 DB1.DBB1

ushort
Volkswagen 2 DB1.DB2
int
uint
floating-point number
VD4 DB1.DBD4

Siemens Customer Best Practices

1、什么时候不要主动Open
西门子plc一般最多允许8个长connect。所以当connect数不够用的时候或者做测试的时候就不要主动Open,这样组件会自动Open并即时Close。

2、什么时候主动Open
当长connectquantity还够用,且想要提升读写性能。

3、除了主动Openconnect,还可以通过批量读写,大幅提升读写性能。
//批量读取
Dictionary addresses = new Dictionary();
addresses.Add("DB4.24", DataTypeEnum.Float);
addresses.Add("DB1.434.0", DataTypeEnum.Bool);
addresses.Add("V4109", DataTypeEnum.Byte);
...
var result = client.BatchRead(addresses);

//批量写入
Dictionary addresses = new Dictionary();
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。

Mitsubishi Client read and write operations

//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 read and write operations

//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;

AllenBradley Client (Rockwell) read and write operations

//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;

Some projects based on IoT Client library

IoTClient Tool Desktop Program Tool (Open Source)

Belonging individual:Farm code for a lifetime

IoTClient ToolDesktop program tools,Address open source.

  • 1. Communication that can be used to test PLCs and related protocols
  • 2. Can be used as an example for the IoTCClient library.
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图1
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图2
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图3
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图4
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图5
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图6
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图7
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图8
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图9
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图10

iotgateway (open source)

Belonging individual:iioter

IoT Gateway

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图11

Energy Management System

ChengQinnar Technology

Energy Management - On site - Single Project

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图12
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图13

Energy Management - Cloud - Multi Project

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图14
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图15
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图16
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图17
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图18
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图19
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图20

Energy Management - Mobile

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图21
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图22
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图23
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图24
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图25
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图26
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图27

Haidilao is about to take control

ChengQinnar Technology

Haidilao terminates control - web

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图28
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图29
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图30
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图31

Haidilao Termination Control - Mobile Terminal

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图32
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图33
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图34
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #插图35
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.