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 #Figure

ModBusTcp Read and Write Operations

//1、实例化客户端 - 输入正确的IP和端口
ModBusTcpClient client = new ModBusTcpClient("127.0.0.1", 502);

//2、写操作 - 参数依次是:address 、值 、站号 、function code
client.Write("4", (short)33, 2, 16);

//2.1、【注意】写入数据的时候需要明确数据类型
client.Write("0", (short)33, 2, 16);    //写入short类型数值
client.Write("4", (ushort)33, 2, 16);   //写入ushort类型数值
client.Write("8", (int)33, 2, 16);      //写入int类型数值
client.Write("12", (uint)33, 2, 16);    //写入uint类型数值
client.Write("16", (long)33, 2, 16);    //写入long类型数值
client.Write("20", (ulong)33, 2, 16);   //写入ulong类型数值
client.Write("24", (float)33, 2, 16);   //写入float类型数值
client.Write("28", (double)33, 2, 16);  //写入double类型数值
client.Write("32", true, 2, 5);         //写入线圈类型值
client.Write("100", "orderCode", stationNumber);  //写入字符串

//3、读操作 - 参数依次是:address 、站号 、function code
var value = client.ReadInt16("4", 2, 3).Value;

//3.1、其他类型数据读取
client.ReadInt16("0", stationNumber, 3);    //short类型数据读取
client.ReadUInt16("4", stationNumber, 3);   //ushort类型数据读取
client.ReadInt32("8", stationNumber, 3);    //int类型数据读取
client.ReadUInt32("12", stationNumber, 3);  //uint类型数据读取
client.ReadInt64("16", stationNumber, 3);   //long类型数据读取
client.ReadUInt64("20", stationNumber, 3);  //ulong类型数据读取
client.ReadFloat("24", stationNumber, 3);   //float类型数据读取
client.ReadDouble("28", stationNumber, 3);  //double类型数据读取
client.ReadCoil("32", stationNumber, 1);    //线圈类型数据读取
client.ReadDiscrete("32", stationNumber, 2);//离散类型数据读取
client.ReadString("100", stationNumber,10); //读取字符串

//4、如果没有主动Open,则会每次读写操作的时候自动打开自动和关闭连接,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();

//5、读写操作都会返回操作结果对象Result
var result = client.ReadInt16("4", 2, 3);
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取操作实际发送的请求报文
var requst  = result.Requst;
//5.4 读取操作服务端响应的报文
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、端口、超时时间、大小端设置
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 portName,Baud rate,data bit,stop bit,奇偶校验]
ModBusRtuClient client = new ModBusRtuClient("COM3", 9600, 8, StopBits.One, Parity.None);

//其他读写操作和ModBusTcpClient的读写操作一致

ModBusAscii Read and Write Operations

//实例化客户端 - [COM portName,Baud rate,data bit,stop bit,奇偶校验]
ModbusAsciiClient client = new ModbusAsciiClient("COM3", 9600, 8, StopBits.One, Parity.None);

//其他读写操作和ModBusTcpClient的读写操作一致

ModbusRtuOverTcp Read and Write Operations

//串口透传 即:用Tcp的方式发送Rtu格式报文

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

//其他读写操作和ModBusTcpClient的读写操作一致

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、写操作
client.Write("Q1.3", true);
client.Write("V2205", (short)11);
client.Write("V2209", 33);
client.Write("V2305", "orderCode");             //写入字符串

//3、读操作
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,则会每次读写操作的时候自动打开自动和关闭连接,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();

//5、读写操作都会返回操作结果对象Result
var result = client.ReadInt16("V2205");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取操作实际发送的请求报文
var requst  = result.Requst;
//5.4 读取操作服务端响应的报文
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位)。

在本组件传入地址的时候不需要带数据类型,直接使用对应方法读取对应类型即可,如:
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
Short
ushort
Volkswagen 2 DB1.DB2
int
uint
floating-point number
VD4 DB1.DBD4

Siemens Customer Best Practices

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

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

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、【注意】写入数据的时候需要明确数据类型
client.Write("DB4.12", 9);          //写入的是intType
client.Write("DB4.12", (float)9);   //写入的是floatType

5、SiemensClient是线程安全类
由于plc长连接有限,SiemensClient被设计成线程安全类。可以把SiemensClient设置成单例,在多个线程之间使用SiemensClient的实例读写操作plc。

Mitsubishi Client read and write operations

//1、实例化客户端 - 输入正确的IP和端口
MitsubishiClient client = new MitsubishiClient(MitsubishiVersion.Qna_3E, "127.0.0.1",6000);

//2、写操作
client.Write("M100", true);
client.Write("D200", (short)11);
client.Write("D210", 33);

//3、读操作
var value1 = client.ReadBoolean("M100").Value;
var value2 = client.ReadInt16("D200").Value;
var value3 = client.ReadInt32("D210").Value;

//4、如果没有主动Open,则会每次读写操作的时候自动打开自动和关闭连接,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();

//5、读写操作都会返回操作结果对象Result
var result = client.ReadInt16("D210");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取操作实际发送的请求报文
var requst  = result.Requst;
//5.4 读取操作服务端响应的报文
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、写操作
client.Write("M100", true);
client.Write("D200", (short)11);
client.Write("D210", 33);

//3、读操作
var value1 = client.ReadBoolean("M100").Value;
var value2 = client.ReadInt16("D200").Value;
var value3 = client.ReadInt32("D210").Value;

//4、如果没有主动Open,则会每次读写操作的时候自动打开自动和关闭连接,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();

//5、读写操作都会返回操作结果对象Result
var result = client.ReadInt16("D210");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取操作实际发送的请求报文
var requst  = result.Requst;
//5.4 读取操作服务端响应的报文
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、写操作 
client.Write("A1", (short)11); 

//3、读操作
var value = client.ReadInt16("A1").Value;

//4、如果没有主动Open,则会每次读写操作的时候自动打开自动和关闭连接,这样会使读写效率大大减低。所以建议手动Open和Close。
client.Open();

//5、读写操作都会返回操作结果对象Result
var result = client.ReadInt16("A1");
//5.1 读取是否成功(true或false)
var isSucceed = result.IsSucceed;
//5.2 读取失败的异常信息
var errMsg = result.Err;
//5.3 读取操作实际发送的请求报文
var requst  = result.Requst;
//5.4 读取操作服务端响应的报文
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 #Figure1
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure2
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure3
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure4
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure5
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure6
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure7
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure8
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure9
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure10

iotgateway (open source)

Belonging individual:iioter

IoT Gateway

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

Energy Management System

ChengQinnar Technology

Energy Management - On site - Single Project

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

Energy Management - Cloud - Multi Project

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure14
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure15
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure16
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure17
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure18
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure19
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure20

Energy Management - Mobile

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure21
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure22
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure23
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure24
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure25
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure26
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure27

Haidilao is about to take control

ChengQinnar Technology

Haidilao terminates control - web

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure28
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure29
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure30
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure31

Haidilao Termination Control - Mobile Terminal

IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure32
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure33
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure34
IoTClient: A Communication Protocol Client for IoT Devices Developed Based on C #Figure35
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 *.