122 Star 435 Fork 141

农码一生 / IoTClient

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

IoTClient

image image image

English | 简体中文

  • This is an IoT device communication protocol realization client, which will include mainstream PLC communication reading, ModBus protocol, Bacnet protocol and other common industrial communication protocols.
  • This component is based on .NET Standard 2.0 and can be used for cross-platform development of .Net, such as Windows, Linux and even run on Raspberry Pi.
  • This component is open source and free for life, and adopts the most relaxed MIT protocol. You can also modify and use it for commercial use (commercial use please evaluate and test).
  • Development tools:Visual Studio 2019
  • QQ exchange group:995475200

Instructions for use

Reference component

Nuget installation Install-Package IoTClient
Or graphical installation
image

ModBusTcp read and write operations (click to expand)

ModBusTcp read and write operations

//1、Instantiate the client-enter the correct IP and port
ModBusTcpClient client = new ModBusTcpClient("127.0.0.1", 502);

//2、Write operation-parameters are: address, value, station number, function code
client.Write("4", (short)33, 2, 16);

//2.1、[Note] When writing data, you need to clarify the data type
client.Write("0", (short)33, 2, 16);    //Write short type value
client.Write("4", (ushort)33, 2, 16);   //Write ushort type value
client.Write("8", (int)33, 2, 16);      //Write int type value
client.Write("12", (uint)33, 2, 16);    //Write uint type value
client.Write("16", (long)33, 2, 16);    //Write long type value
client.Write("20", (ulong)33, 2, 16);   //Write ulong type value
client.Write("24", (float)33, 2, 16);   //Write float type value
client.Write("28", (double)33, 2, 16);  //Write double type value
client.Write("32", true, 2, 5);         //Write Coil type value
client.Write("100", "orderCode", stationNumber);  //Write string

//3、Read operation-the parameters are: address, station number, function code
var value = client.ReadInt16("4", 2, 3).Value;

//3.1、Other types of data reading
client.ReadInt16("0", stationNumber, 3);    //short type data read
client.ReadUInt16("4", stationNumber, 3);   //ushort type data read
client.ReadInt32("8", stationNumber, 3);    //int type data read
client.ReadUInt32("12", stationNumber, 3);  //uint type data read
client.ReadInt64("16", stationNumber, 3);   //long type data read
client.ReadUInt64("20", stationNumber, 3);  //ulong type data read
client.ReadFloat("24", stationNumber, 3);   //float type data read
client.ReadDouble("28", stationNumber, 3);  //double type data read
client.ReadCoil("32", stationNumber, 1);    //Coil type data read
client.ReadDiscrete("32", stationNumber, 2);//Discrete type data read
client.ReadString("100", stationNumber,readLength:10); //Read string

//4、If there is no active Open, it will automatically open and close the connection every time you read and write operations, which will greatly reduce the efficiency of reading and writing. So it is recommended to open and close manually.
client.Open();

//5、Read and write operations will return the operation result object Result
var result = client.ReadInt16("4", 2, 3);
//5.1 Whether the reading is successful (true or false)
var isSucceed = result.IsSucceed;
//5.2 Exception information for failed reading
var errMsg = result.Err;
//5.3 Read the request message actually sent by the operation
var requst  = result.Requst;
//5.4 Read the response message from the server
var response = result.Response;
//5.5 Read value
var value3 = result.Value;

//6、Batch read
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、Other parameters of the constructor
//IP, port, timeout time, big and small end settings
ModBusTcpClient client = new ModBusTcpClient("127.0.0.1", 502, 1500, EndianFormat.ABCD);

For more usage of ModBusTcp, please refer to Unit Test

ModBusRtu read and write operations

ModBusRtu read and write operations

//实例化客户端 - [COM端口名称,波特率,数据位,停止位,奇偶校验]
ModBusRtuClient client = new ModBusRtuClient("COM3", 9600, 8, StopBits.One, Parity.None);

//其他读写操作和ModBusTcpClient的读写操作一致
ModBusAscii read and write operations

ModBusAscii read and write operations

//实例化客户端 - [COM端口名称,波特率,数据位,停止位,奇偶校验]
ModbusAsciiClient client = new ModbusAsciiClient("COM3", 9600, 8, StopBits.One, Parity.None);

//其他读写操作和ModBusTcpClient的读写操作一致
ModbusRtuOverTcp read and write operations

ModbusRtuOverTcp read and write operations

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

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

//其他读写操作和ModBusTcpClient的读写操作一致
SiemensClient (Siemens) read and write operations

SiemensClient (Siemens) 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);

//3、读操作
var value1 = client.ReadBoolean("Q1.3").Value;
var value2 = client.ReadInt16("V2205").Value;
var value3 = client.ReadInt32("V2209").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;
Note: About Siemens PLC address

Note: About 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#数据类型 smart200 1200/1500/300
bit V1.0 DB1.DBX1.0
byte VB1 DB1.DBB1
shor
ushort
VW2 DB1.DBW2
int
uint
float
VD4 DB1.DBD4
SiemensClient best practices

SiemensClient best practices

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

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

3、除了主动Open连接,还可以通过批量读写,大幅提升读写性能。
//批量读取
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、【注意】写入数据的时候需要明确数据类型
client.Write("DB4.12", 9);          //写入的是int类型
client.Write("DB4.12", (float)9);   //写入的是float类型

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

MitsubishiClient (Mitsubishi) 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 (Omron) read and write operations

OmronFinsClient (Omron) 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;
AllenBradleyClient read and write operations

AllenBradleyClient 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 IoTClient library

IoTClient Tool Desktop program tool (open source)

IoTClient Tool 桌面程序工具,开源地址

  • 1、可用来测试PLC和相关协议的通信
  • 2、可作为IoTClient库使用例子。

image

image

image

image

image

image

image

image

image

image

Energy Management System (Commercial)

能源管理(现场-单项目)

image
image

能源管理(云端-多项目)

image
image
image
image
image
image

能源管理(移动端)

imageimageimageimageimageimageimage

Haidilao terminal control (commercial)

海底捞末端控制-web

image
image
image
image

海底捞末端控制-移动端

imageimageimageimage

Comments ( 14 )

Sign in for post a comment

About

这是一个物联网设备通讯协议实现客户端,将包括主流PLC通信读取、ModBus协议、Bacnet协议等常用工业通讯协议。本组件终身开源免费,采用最宽松MIT协议,您也可以随意修改和商业使用(商业使用请做好评估和测试)。 spread retract
C#
MIT
Cancel

Releases

No release

IoTClient

Contributors

All

Activities

load more
can not load any more
C#
1
https://gitee.com/zhaopeiym/IoTClient.git
git@gitee.com:zhaopeiym/IoTClient.git
zhaopeiym
IoTClient
IoTClient
master

Search