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
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.
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.
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:
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).
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.