Modbus with C#: libraries, code, utilities and examples

In this post you will find how to use nModbus with C# and how to create a simulation enviroment to test your program with Modbus.
Other useful resources that you can find in this post are:

  • using a Modbus simulator
  • Creating virtual Com ports
  • Using sniffer tools to debug the transmission
  • A Modbus TCP Client in C# with another library

Getting NModbus

The C# library that i use the most when i need Modbus communication between pc and plcs or other components is nModbus.
nModbus manages both Modbus TCP and RTU protocols, it’s well tested and sources are available. The site for nModbus is http://code.google.com/p/nmodbus/and here you can get the latest sources.

Once you extract the sources you can open the solution file, located in the src folder.

Sample code

NModbus contains many samples included in the source code. Once you open it, you can watch at the MySample project that contains many examples on how to read and write using different devices:

The two examples that i use the most are Modbus TCP and RTU, and an example of the code to use the library is this:

Modbus TCP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 *  Reading
 */
TcpClient client = new TcpClient("127.0.0.1", 502);
ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
// read five input values
ushort startAddress = 100;
ushort numInputs = 5;
bool[] inputs = master.ReadInputs(startAddress, numInputs);
for (int i = 0; i < numInputs; i++)
    Console.WriteLine("Input {0}={1}", startAddress + i, inputs[i] ? 1 : 0);
/*
 *  Writing
 */
ushort startAddress = 1;
// write three coils
master.WriteMultipleCoils(startAddress, new bool[] { true, false, true });

Modbus RTU

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SerialPort port = new SerialPort("COM1");
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
byte slaveId = 1;
ushort startAddress = 1;
ushort numRegisters = 5;
// read five registers
ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
for (int i = 0; i < numRegisters; i++)
    Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
byte slaveId = 1;
ushort startAddress = 1;
// write three coils
master.WriteMultipleCoils(slaveId, startAddress, new bool[] { true, false, true });

Depending on the registers that you are going to read and write you have different methods, that will call the different functions of the protocol, and that will return the results in 2 formats: ushort or bool (if you are reading multiple registers you will get array of ushorts and bools).

ITDONOTWORK

NModbus is supported by thousands of users, and you can find help on the NModbus google Group.

Testing your program with a simulation:

The most common Modbus simulator is located at http://www.plcsimulator.org/

This simulator provides both Modbus TCP and RTU protocols, and shows the content of the registers in the main windows so you can analyze the behaviour of your program directly in the tables of the simulator.

Creating Virtual COM Ports to test Modbus RTU:

While it’s easy to use TCP to analyze the Modbus TCP on the host (just create a socket with a 127.0.0.1 ip), testing Modbus RTU or ASCII may require some hardware, like a null-modem cable and 2 COM Ports.
To avoid this you can download a null modem emulator called Com0Com (open source, located at http://sourceforge.net/projects/com0com/) to create a pair of virtual com ports wich you can use to connect your simulator and software.

Sniffer tools available for free:

If you need to analyze the traffic between the 2 devices, to see what’s going on with your communication, there are 2 useful tools:
Wireshark is used to sniff ethernet packets and to decode the protocol. This tool can decode Modbus TCP protocol quite good and it’s useful to debug the transmission.
Free Serial Port Monitor is a serial port sniffer that can analyze the transmission between 2 COM Ports.

Other Modbus TCP libraries:

a good Modbus TCP library is available athttp://www.codeproject.com/A

rticles/16260/Modbus-TCP-class

This library offers a client with some interesting features and it’s also useful when debugging devices.

via: http://www.mesta-automation.com/modbus-with-c-sharp-libraries-examples/

One thought on “Modbus with C#: libraries, code, utilities and examples

  1. Hi ABlanco!!

    You have simplified the whole concept and I have understood it pretty easily.. Thanks for that. I am interested in Modbus RTU, so could you provide me with some more help?? I will be doing all in Windows Forms using C#

Leave a Reply

Your email address will not be published. Required fields are marked *