libmodbus We have introduced it many times in our public account and application notes, regarding its download, compilation, installation, configuration, cross-compilation, etc. If you are unclear, you can refer to previous articles in our public account.
Classic example demonstration of Modbus TCP/IP
Today I recommend: libModbus
This article mainly involves testing the Modbus RTU mode. Regarding Modbus TCP/IP, it has also been pushed in previous articles. Because the hardware serial port and Bluetooth of the Raspberry Pi 3 are shared, some configurations and settings need to be made on the Raspberry Pi when using serial communication. And turn off the serial console function.
You need to run the command sudo raspi-config to configure.
After configuring, you need to reboot reboot to make the configuration take effect.
Modify the cmdline.txt configuration file, use vi or nano to open the file.
Enter the command sudo nano /boot/cmdline.txt
Set config.txt
Enter the command sudo nano /boot/config.txt
Add a prohibition for Bluetooth at the last line of the file,
After all configurations are complete, reboot reboot the Raspberry Pi.
Connect the computer to the Raspberry Pi via serial port. Since the GPIO serial port of the Raspberry Pi is TTL level, we need to use TTL to RS232. If your laptop has an RS232 serial port, you can connect your computer using TTL to RS232. However, most laptops do not have serial ports, so you need to add an RS232 to USB converter to connect to the computer. Make sure the wiring is correct to prevent damage to the Raspberry Pi hardware. As shown below
=========================================================== Now let’s test
Before testing, some source code needs to be modified. On the Raspberry Pi, you need to modify a bit of code because the routine does not use ttyAMA0 serial port.
Then save and exit the compilation.
Enter the libmodbus directory, go to the tests folder and run
sudo ./unit-test-server rtu
Run on the Raspberry Pi sudo ./unit-test-client rtu
Make sure to include the parameter rtu, and it must be lowercase. If you do not include the parameter, the default is to test modbus TCP/IP.
You can see that many function codes have been tested, such as 01,02,03,04,05 06,10, etc. Finally, you can see that all tests pass with success. This proves that our MODBUS RTU test can pass completely, and some exception codes have also been tested. If you’re interested, you can take a look at these source codes. Thus, our testing of the libmodbus protocol stack’s MODBUS RTU mode is complete. Along with the previously pushed MODBUS TCP/IP, we have introduced both RTU and TCP under libmodbus. Based on this, you can modify the routine to achieve your own functions, such as using the Raspberry Pi as a client to connect to some devices, or using the serial port of the Raspberry Pi to communicate with some controllers that have a serial port through modbus RTU, and then the Raspberry Pi can connect to the Internet through Modbus TCP/IP, achieving a gateway function.
Leave a Comment
Your email address will not be published. Required fields are marked *