Run a modbus RTU example on NX8MM-35
From ICOP tech wiki
Contents
Run a modbus RTU example on NX8MM-35
This article will tell you how to run a modbus RTU example on NX8MM-35.
We used libmodbus to utilize the modbus function via 2x RS485 (1 modbus server & 1 modbus client on the same SBC) on NX8MM-35 under Yocto in this tips.
Key Features of Modbus:
- Master-Slave (Client-Server) Architecture: A master (client) device initiates communication, while slave (server) devices respond.
- Simple and Open Protocol: Easy to implement and widely supported in industrial systems.
- Multiple Communication Modes:
- Modbus RTU (Remote Terminal Unit): Uses binary data format over serial communication (RS-232, RS-485).
- Modbus ASCII: Uses human-readable ASCII characters for serial communication.
- Modbus TCP/IP: Runs over Ethernet, allowing faster and more flexible networking.
Common Uses of Modbus:
Monitoring and control of industrial equipment.
SCADA (Supervisory Control and Data Acquisition) systems.
Energy meters, temperature sensors, and other field devices
Tools you need:
- i.MX series products like NX8MM-35 / PN8M-090T / EBOX-IMX8MM
- In this tips, we use NX8MM-35 as an example.
- Yocto Linux 4.0 sample image with libmodbus: Image / Guide
- In the default recipe, you will need to add "libmodbus-dev" to IMAGE_INSTALL:append in local.conf before you build your own image.
- Modbus test tool sample code file: Download link
- NX8MM-35 with the test fixture connectoing 2xRS485.
Procedure:
1. Boot up the board and compile the modbus test tool First, boot up the board, and plug in a USB disk which contains the modbus test tool sample code file and open the terminal.
1.1 Access the USB disk and input the code below:
$ gcc modbus_test_v2.c -o modbus_test_v2 -lmodbus
1.2 Move the compiled application to the your appointed file in the system and access it (In this case, we make a file called modbustest.)
$ mv modbus_test_v2 /modbustest $ cd /modbustest
Use the command below to check the function description:
$ /.modbus_test_v2 Usage: ./modbus_test_v2 [-s|-c] [-r gpio_index] serial_port -s : server -c : client -r : rts-gpios : dm446c rts-gpios numbers: ttymxc0=128, ttymxc3=9
We use COM1 (ttymxc0/GPIO128) and COM4 (ttymxc3/GPIO9) on the NX8MM-35 for RS-485 communication, performing point-to-point Modbus RTU testing between the two ports. (Be sure to short J39 to switch the COM port to RS485 mode.)
2. Start to perform Modbus RTU testing on NX8MM-35
2.1 Open another terminal and input command below to set COM4 to server mode:
./modbus_test_v2 -s -r 9 /dev/ttymxc3
2.2 Use the existing terminal to set COM1 to client mode and send the signal to server(COM4)
./modbus_test_v2 -c -r 128 /dev/ttymxc0
After executing the command above, the server (COM4) successfully receives the Modbus RTU signal from the client (COM1), marking the completion of the test.