domenica 23 ottobre 2016

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 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 new NModbus4 library on GitHub, but to compile it you need Visual Studio 2015, due to some C# 6 features used.
You can also download from NuGet by searching for NModbus4.
NModbus4
The old library on google code has bugs (check comments for more informations), please consider using either the new NModbus4 library, or you can download the old code from this repository: https://github.com/mesta1/nmodbus.
The old library of 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 go to MySample project that contains many examples on how to read and write using different devices. You can check a how-to video here: http://www.mesta-automation.com/modbus-with-c-an-how-to-video/
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 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 athttp://sourceforge.net/projects/com0com/) to create a pair of virtual com ports which you can use to connect your simulator and software.

Sniffer tools available for free:

If you need to analyze the traffic between two devices to see what’s going on with your communication, there are two useful tools:
Wireshark is used to sniff ethernet packets and to decode the protocol. This tool can decode Modbus TCP protocol quite well and it’s also 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:

While NModbus is the best choice, another Modbus TCP library is available at http://www.codeproject.com/Articles/16260/Modbus-TCP-class
This library provides a client with some interesting features and it’s also useful for debugging the devices.

How-to video on NModbus

You can get the software configuration in the article.

Nessun commento:

Posta un commento