preamble
IoTClient is an open source client library for the Internet of Things (IoT) space, which is primarily used to enable communication with a variety of industrial devices. The library is written in C# and is based on .NET Standard 2.0, which means that it can be used with multiple versions of .NET, including .NET Framework, .NET Core, .NET 5 and above, as well as Xamarin and UWP.
Projects
IoTClient is an open source free client library based on .NET Standard 2.0 for cross-platform .
The component supports major industrial communication protocols, including PLC communications (e.g. Siemens, Mitsubishi, Omron, Allen Bradley, etc.), ModBus (TCP/RTU/ASCII), and Bacnet.
IoTClient is distributed under the MIT license, and you are free to modify and use it commercially (please note that it is well evaluated and tested before commercial use).
Project Characteristics
Supports multiple communication protocols
- ModBus: Supports ModBus TCP, ModBus RTU, ModBus ASCII and ModBus RTU over TCP.
- Bacnet: Supports the Bacnet protocol.
- PLC CommunicationSupport PLC communication of Siemens, Mitsubishi, Omron, Allen Bradley and other major brands.
cross-platform
- Runs on Windows, Linux.
- Supports deployment on small devices like the Raspberry Pi.
Open source and free
- Distributed under the MIT license, a very liberal license that allows free use, modification and distribution of the software.
NuGet Package
- projects that can be installed via the NuGet Package Manager.
Project use
1、Reference components
Nuget Installation
Install-Package IoTClient
or graphical installation
2, 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 in order: address, value, station number, function code ("4", (short)33, 2, 16); //2.1. [Note] You need to specify the data type when writing data ("0", (short)33, 2, 16); //Writes a value of type short ("4", (ushort)33, 2, 16); //Write ushort value ("8", (int)33, 2, 16); //Write a value of type int ("12", (uint)33, 2, 16); //Write a value of type uint ("16", (long)33, 2, 16); //Write a value of type long ("20", (ulong)33, 2, 16); //Write a value of type ulong ("24", (float)33, 2, 16); //Write a float type value ("28", (double)33, 2, 16); //Write a value of type double ("32", true, 2, 5); //Write coil type value ("100", "orderCode", stationNumber); //Write String //3、Read operation - parameters in order: 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 reading client.ReadUInt16("4", stationNumber, 3); //Ushort type data reading client.ReadInt32("8", stationNumber, 3); //int type data reading client.ReadUInt32("12", stationNumber, 3); //uint type data reading client.ReadInt64("16", stationNumber, 3); //Long type data reading client.ReadUInt64("20", stationNumber, 3); //Read data of type ulong ("24", stationNumber, 3); //Float type data reading ("28", stationNumber, 3); //Double type data reading ("32", stationNumber, 1); //Coil type data reading ("32", stationNumber, 2);//Discrete type data reading ("100", stationNumber,10); //Retrieve String //4, if there is no active Open, it will automatically open the automatic and close the connection every time you read and write operations, which will make the read and write efficiency is greatly reduced. So it is recommended to manually Open and Close. (); //5, read and write operations will return the results of the operation object Result var result = client.ReadInt16("4", 2, 3); //5.1 Successful reading (true or false) var isSucceed = ; //5.2 Read Failure Exception Message var errMsg = ; //5.3 Reading the actual request message sent by the operation var requst = ; //5.4 Read the Operational Server Response Message var response = ; //5.5 Read-in values var value3 = ; //6、Batch reading var list = new List<ModBusInput>(); (new ModBusInput() { Address = "2", DataType = DataTypeEnum.Int16, FunctionCode = 3, StationNumber = 1 }); (new ModBusInput() { Address = "2", DataType = DataTypeEnum.Int16, FunctionCode = 4, StationNumber = 1 }); (new ModBusInput() { Address = "199", DataType = DataTypeEnum.Int16, FunctionCode = 3, StationNumber = 1 }); var result = (list); //7, other parameters of the constructor //IP, port, timeout, size end settings ModBusTcpClient client = new ModBusTcpClient("127.0.0.1", 502, 1500, );
3, ModBusRtu read and write operations
//Instantiate Client - [COM port name, baud rate, data bits, stop bits, parity] ModBusRtuClient client = new ModBusRtuClient("COM3", 9600, 8, , ); //Other read and write operations are the same as for ModBusTcpClient.
4, ModBusAscii read and write operations
//Instantiate Client - [COM Port Name, Baud Rate, Data Bits, Stop Bits, Parity] ModbusAsciiClient client = new ModbusAsciiClient("COM3", 9600, 8, , ); //Other read and write operations are the same as for ModBusTcpClient.
5, ModbusRtuOverTcp read and write operations
//Serial port passthrough i.e.: send Rtu format message by Tcp. //Instantiating the client - IP, port, timeout, size end settings ModbusRtuOverTcpClient client = new ModbusRtuOverTcpClient("127.0.0.1", 502, 1500, ); //Other read and write operations are the same as for ModBusTcpClient.
6, SiemensClient (Siemens) read and write operations
//1. Instantiate the client - enter the model number, IP and port //Other models: 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、Write operation ("Q1.3", true); ("V2205", (short)11); ("V2209", 33); ("V2305", "orderCode"); //Write String //3、Read operation var value1 = ("Q1.3").Value; var value2 = client.ReadInt16("V2205").Value; var value3 = client.ReadInt32("V2209").Value; var value4 = ("V2305").Value; //Retrieve String //4, if there is no active Open, it will automatically open the automatic and close the connection every time you read and write operations, which will make the read and write efficiency is greatly reduced. So it is recommended to manually Open and Close. (); //5, read and write operations will return the results of the operation object Result var result = client.ReadInt16("V2205"); //5.1 Successful reading (true or false) var isSucceed = ; //5.2 Read Failure Exception Message var errMsg = ; //5.3 Reading the actual request message sent by the operation var requst = ; //5.4 Read the Operational Server Response Message var response = ; //5.5 Read-in values var value4 = ;
7. SiemensClient Best Practices
1When not to initiate Open Siemens plc generally allows up to 8 long connections. So when the number of connections is not enough or when you are doing tests, don't take the initiative to open, so that the component will automatically open and close instantly. 2When to Open When the number of long connections is still sufficient and you want to improve read and write performance. 3In addition to active Open connections, read and write performance can be dramatically improved by batch reading and writing. //batch read Dictionary<string, DataTypeEnum> addresses = new Dictionary<string, DataTypeEnum>(); ("DB4.24", ); ("DB1.434.0", ); ("V4109", ); ... var result = (addresses); //Batch Write Dictionary<string, object> addresses = new Dictionary<string, object>(); ("DB4.24", (float)1); ("DB4.0", (float)2); ("DB1.434.0", true); ... var result = (addresses); 4, [Note] When writing data you need to specify the data type ("DB4.12", 9); //The write is of type int ("DB4.12", (float)9); //The write is of type float 5SiemensClient is a thread-safe class. SiemensClient is designed as a thread-safe class due to the limited number of plc long connections. It is possible to set SiemensClient as a single instance and use the instance of SiemensClient to read, write and operate plc between multiple threads.
8、MitsubishiClient
//1. Instantiate the client - enter the correct IP and port MitsubishiClient client = new MitsubishiClient(MitsubishiVersion.Qna_3E, "127.0.0.1",6000); //2、Write operation ("M100", true); ("D200", (short)11); ("D210", 33); //3、Read operation var value1 = ("M100").Value; var value2 = client.ReadInt16("D200").Value; var value3 = client.ReadInt32("D210").Value; //4, if there is no active Open, it will automatically open the automatic and close the connection every time you read and write operations, which will make the read and write efficiency is greatly reduced. So it is recommended to manually Open and Close. (); //5, read and write operations will return the results of the operation object Result var result = client.ReadInt16("D210"); //5.1 Successful reading (true or false) var isSucceed = ; //5.2 Read Failure Exception Message var errMsg = ; //5.3 Reading the actual request message sent by the operation var requst = ; //5.4 Read the Operational Server Response Message var response = ; //5.5 Read-in values var value4 = ;
9, OmronFinsClient (Omron) read and write operations
//1. Instantiate the client - enter the correct IP and port OmronFinsClient client = new OmronFinsClient("127.0.0.1",6000); //2、Write operation ("M100", true); ("D200", (short)11); ("D210", 33); //3、Read operation var value1 = ("M100").Value; var value2 = client.ReadInt16("D200").Value; var value3 = client.ReadInt32("D210").Value; //4, if there is no active Open, it will automatically open the automatic and close the connection every time you read and write operations, which will make the read and write efficiency is greatly reduced. So it is recommended to manually Open and Close. (); //5, read and write operations will return the results of the operation object Result var result = client.ReadInt16("D210"); //5.1 Successful reading (true or false) var isSucceed = ; //5.2 Read Failure Exception Message var errMsg = ; //5.3 Reading the actual request message sent by the operation var requst = ; //5.4 Read the Operational Server Response Message var response = ; //5.5 Read-in values var value4 = ;
10, AllenBradleyClient (Rockwell) read and write operations
//1. Instantiate the client - enter the correct IP and port AllenBradleyClient client = new AllenBradleyClient("127.0.0.1",44818); //2、Write operation ("A1", (short)11); //3、Read operation var value = client.ReadInt16("A1").Value; //4, if there is no active Open, it will automatically open the automatic and close the connection every time you read and write operations, which will make the read and write efficiency is greatly reduced. So it is recommended to manually Open and Close. (); //5, read and write operations will return the results of the operation object Result var result = client.ReadInt16("A1"); //5.1 Successful reading (true or false) var isSucceed = ; //5.2 Read Failure Exception Message var errMsg = ; //5.3 Reading the actual request message sent by the operation var requst = ; //5.4 Read the Operational Server Response Message var response = ; //5.5 Read-in values var value4 = ;
IoTClient Library Project
IoTClient Tool Desktop Program Tool
1、Open Source Address /zhaopeiym/
2, can be used to test the communication of PLC and related protocols
3. Can be used as an example of the IoTClient library.
4、IoTClient Tool effect picture
iotgateway
1、Open source address /iioter/iotgateway
2、Online experience User name: admin Password:
3. Cross-platform IoT gateway based on .NET 5.
4, through the visualization of the configuration, easy to connect to any device and system (such as PLC, sweep gun, CNC, database, serial devices, host computer, OPC Server, OPC UA Server, Mqtt Server, etc.)
5, provide simple driver development interface; of course, can also be edge computing.
6、System page display
Project Commercial Results
1. Energy management - on-site - single project
mobile
2、Yuebang Intelligent Sorting System
Project Address
github:/zhaopeiym/IoTClient
gitee:/zhaopeiym/IoTClient
ultimate
If you found this article helpful, why not support it with a like? Your support is what keeps me motivated to continue sharing my knowledge. Feel free to leave a comment if you have any questions or need further help.
You can also join WeChat [DotNet Technician] community to share ideas and grow with other tech-loving peers!