Modbus programming in C#

Modbus is a widely used communication protocol in industrial automation. It allows communication between programmable logic controllers (PLCs), sensors, and human-machine interfaces (HMIs). In C#, you can use libraries or implement Modbus communication from scratch using the .NET framework.

To get started, you’ll need to install the NModbus library, which provides easy-to-use functions for Modbus communication in C#. You can install it using NuGet Package Manager in Visual Studio.
Modbus programming in c#

Example Code for TCP Communication:

csharp
using Modbus.Device; using System; using System.Net.Sockets; class Program { static void Main(string[] args) { string ipAddress = "192.168.1.1"; // IP address of the Modbus device int port = 502; // Modbus TCP port number TcpClient client = new TcpClient(ipAddress, port); ModbusIpMaster master = ModbusIpMaster.CreateIp(client); // Read holding registers from Modbus device ushort startAddress = 0; // Starting address of the registers ushort numRegisters = 10; // Number of registers to read ushort[] registers = master.ReadHoldingRegisters(1, startAddress, numRegisters); for (int i = 0; i < registers.Length; i++) { Console.WriteLine("Register {0}: {1}", startAddress + i, registers[i]); } client.Close(); Console.ReadLine(); } }

 

In this example, we establish a TCP connection with the Modbus device using its IP address and port number. Then, we create an instance of the ModbusIpMaster class to communicate with the device.

We read a range of holding registers from the Modbus device by calling the ReadHoldingRegisters method, specifying the slave ID (1 in this example), the starting address, and the number of registers to read. The method returns an array of the read register values, which we print on the console.

Example Code for RTU Communication:

csharp
using Modbus.Device; using System; using System.IO.Ports; class Program { static void Main(string[] args) { string portName = "COM1"; // Serial port name int baudRate = 9600; // Baud rate Parity parity = Parity.None; // Parity int dataBits = 8; // Data bits StopBits stopBits = StopBits.One; // Stop bits SerialPort port = new SerialPort(portName, baudRate, parity, dataBits, stopBits); ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port); // Read holding registers from Modbus device byte slaveId = 1; // Modbus slave ID ushort startAddress = 0; // Starting address of the registers ushort numRegisters = 10; // Number of registers to read ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters); for (int i = 0; i < registers.Length; i++) { Console.WriteLine("Register {0}: {1}", startAddress + i, registers[i]); } port.Close(); Console.ReadLine(); } }

In this example, we configure the serial port parameters such as port name, baud rate, parity, data bits, and stop bits. Then, we create an instance of the ModbusSerialMaster class to communicate with the Modbus device over the serial port.

The process of reading and holding registers is similar to the TCP example. We specify the slave ID, starting address, and number of registers to read. The ReadHoldingRegisters method returns an array of the read register values, which we display on the console.

Make sure to replace the IP address, port, and serial port settings with appropriate values according to your Modbus device configuration.

These examples provide a basic understanding of Modbus programming in C# using the NModbus library. You can extend the code to implement other Modbus functions or perform read/write operations on different data types, such as input registers or coils. Consult the NModbus documentation for further information on advanced Modbus functionality and error handling.