# EasyModbusTCP.NET **Repository Path**: object-cloud/EasyModbusTCP.NET ## Basic Information - **Project Name**: EasyModbusTCP.NET - **Description**: Modbus C#通信源码 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 10 - **Created**: 2022-09-26 - **Last Updated**: 2024-06-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # EasyModbusTCP.NET - www.EasyModbusTCP.NET Modbus TCP, Modbus UDP and Modbus RTU client/server library for .NET
Industry approved!!
Fast and secure access from PC or Embedded Systems to many PLC-Systems and other components for industry automation.
Only a few lines of codes are needed to read or write data from or to a PLC.

Implementation Guide and Codesamples: www.easymodbustcp.net

Additional Software tools e.g. Modbus Server Simulator, makes software development fast and easy. Download Library (*.DLL) from NuGet or from: Download EasyModbusTCP/UDP/RTU .NET Supported Function Codes: - Read Coils (FC1) - Read Discrete Inputs (FC2) - Read Holding Registers (FC3) - Read Input Registers (FC4) - Write Single Coil (FC5) - Write Single Register (FC6) - Write Multiple Coils (FC15) - Write Multiple Registers (FC16) - Read/Write Multiple Registers (FC23) Modbus TCP, Modbus UDP and Modbus RTU client/server library Copyright (c) 2018-2020 Rossmann-Engineering Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

使用例子:

Modbus-RTU Master Simple Read and Write operations ``` using System; using EasyModbus; namespace ModbusRS485Master { class Program { public static void Main(string[] args) { ModbusClient modbusClient = new ModbusClient("COM1"); //modbusClient.UnitIdentifier = 1; Not necessary since default slaveID = 1; //modbusClient.Baudrate = 9600; // Not necessary since default baudrate = 9600 //modbusClient.Parity = System.IO.Ports.Parity.None; //modbusClient.StopBits = System.IO.Ports.StopBits.Two; //modbusClient.ConnectionTimeout = 500; modbusClient.Connect(); Console.WriteLine("Value of Discr. Input #1: "+modbusClient.ReadDiscreteInputs(0,1)[0].ToString()); //Reads Discrete Input #1 Console.WriteLine("Value of Input Reg. #10: "+modbusClient.ReadInputRegisters(9,1)[0].ToString()); //Reads Inp. Reg. #10 modbusClient.WriteSingleCoil(4,true); //Writes Coil #5 modbusClient.WriteSingleRegister(19,4711); //Writes Holding Reg. #20 Console.WriteLine("Value of Coil #5: "+modbusClient.ReadCoils(4,1)[0].ToString()); //Reads Discrete Input #1 Console.WriteLine("Value of Holding Reg.. #20: "+modbusClient.ReadHoldingRegisters(19,1)[0].ToString()); //Reads Inp. Reg. #10 modbusClient.WriteMultipleRegisters(49, new int[10] {1,2,3,4,5,6,7,8,9,10}); modbusClient.WriteMultipleCoils(29, new bool[10] {true,true,true,true,true,true,true,true,true,true,}); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } } ``` Modbus-TCP Client Simple Read and Write operations ``` namespace ModbusClientApplication { class Program { public static void Main(string[] args) { ModbusClient modbusClient = new ModbusClient("190.201.100.100", 502); //Ip-Address and Port of Modbus-TCP-Server modbusClient.Connect(); //Connect to Server modbusClient.WriteMultipleCoils(4, new bool[] {true, true, true, true, true, true, true, true, true, true}); //Write Coils starting with Address 5 bool[] readCoils = modbusClient.ReadCoils(9,10); //Read 10 Coils from Server, starting with address 10 int[] readHoldingRegisters = modbusClient.ReadHoldingRegisters(0,10); //Read 10 Holding Registers from Server, starting with Address 1 // Console Output for (int i = 0; i < readCoils.Length; i++) Console.WriteLine("Value of Coil " + (9 + i + 1) + " " + readCoils[i].ToString()); for (int i = 0; i < readHoldingRegisters.Length; i++) Console.WriteLine("Value of HoldingRegister " + (i + 1) + " "+ readHoldingRegisters[i].ToString()); modbusClient.Disconnect(); //Disconnect from Server Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } } ``` Read Values from Modus-Server and Publish the Values to a MQTT-Broker ``` class Program { static void Main(string[] args) { EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient("127.0.0.1", 502); //Increase the Connection Timeout to 5 seconds modbusClient.ConnectionTimeout = 5000; //We create a Log File. This will also active the Event logging modbusClient.LogFileFilename = "test.txt"; //The Messages sent to the MQTT-Broker will be retained in the Broker. After subscribtion, the client will automatically //receive the last retained message. By default the Retain-Flag is FALSE -> Messages will not be retained modbusClient.MqttRetainMessages = true; //Connect to the ModbusTCP Server modbusClient.Connect(); while (true) { // We read two registers from the Server, and Publish them to the MQTT-Broker. By default Values will be published // on change By default we publish to the Topic 'easymodbusclient/' and the the address e.g. ''easymodbusclient/holdingregister1' // The propery "Password" and "Username" can be used to connect to a Broker which require User and Password. By default the //MQTT-Broker port is 1883 int[] holdingRegister = modbusClient.ReadHoldingRegisters(60, 2, "www.mqtt-dashboard.com"); System.Threading.Thread.Sleep(1000); } modbusClient.Disconnect(); Console.ReadKey(); } } ``` Automatically poll values from a Modus-Server and Publish them to a MQTT-Broker – The Topic are changed ``` static void Main(string[] args) { EasyModbus.EasyModbus2Mqtt easyModbus2Mqtt= new EasyModbus.EasyModbus2Mqtt(); //easyModbus2Mqtt.AutomaticReconnect = false; easyModbus2Mqtt.MqttUserName = "sr555"; easyModbus2Mqtt.MqttPassword = "******************"; easyModbus2Mqtt.MqttBrokerPort = 18972; //easyModbus2Mqtt.MqttBrokerAddress = "broker.hivemq.com"; //easyModbus2Mqtt.MqttBrokerAddress = "192.168.178.101"; easyModbus2Mqtt.MqttBrokerAddress = "m21.cloudmqtt.com"; easyModbus2Mqtt.ModbusIPAddress = "127.0.0.1"; easyModbus2Mqtt.AddReadOrder(EasyModbus.FunctionCode.ReadCoils, 2, 0, 200, new string[] { "easymodbusclient/customtopic1", "easymodbusclient/customtopic2" }); easyModbus2Mqtt.AddReadOrder(EasyModbus.FunctionCode.ReadInputRegisters, 2, 0, 200); EasyModbus.ReadOrder readOrder = new EasyModbus.ReadOrder(); readOrder.Hysteresis = new int[10] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }; readOrder.Unit = new string[10] {"°C", "°C", "°C", "°C", "°C", "°C", "°C", "°C", "°C", "°C"}; readOrder.Scale = new float[10] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f }; readOrder.Quantity = 10; readOrder.StartingAddress = 10; readOrder.FunctionCode = EasyModbus.FunctionCode.ReadHoldingRegisters; easyModbus2Mqtt.AddReadOrder(readOrder); easyModbus2Mqtt.start(); } ``` Modbus-TCP Server which publishes the values on change to a MQTT-Broker ``` class Program { static void Main(string[] args) { Program application = new Program(); application.startServer(); } public void startServer() { EasyModbus.ModbusServer modbusServer = new EasyModbus.ModbusServer(); modbusServer.MqttRootTopic = "examplemodbusserver"; modbusServer.MqttBrokerAddress = "www.mqtt-dashboard.com"; modbusServer.Listen(); modbusServer.holdingRegistersChanged += new EasyModbus.ModbusServer.HoldingRegistersChanged(holdingRegistersChanged); Console.ReadKey(); modbusServer.StopListening(); } public void holdingRegistersChanged(int startingAddress, int quantity) { Console.WriteLine(startingAddress); Console.WriteLine(quantity); } } ```