Personal tools

Run a modbus RTU example on NX8MM-35

From ICOP tech wiki

Jump to: navigation, search

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:

  1. Master-Slave (Client-Server) Architecture: A master (client) device initiates communication, while slave (server) devices respond.
  2. Simple and Open Protocol: Easy to implement and widely supported in industrial systems.
  3. Multiple Communication Modes:
    1. Modbus RTU (Remote Terminal Unit): Uses binary data format over serial communication (RS-232, RS-485).
    2. Modbus ASCII: Uses human-readable ASCII characters for serial communication.
    3. 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:

  1. i.MX series products like NX8MM-35 / PN8M-090T / EBOX-IMX8MM
    • In this tips, we use NX8MM-35 as an example.
  2. 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.
  3. Modbus test tool sample code file: Download link
  4. NX8MM-35 with the test fixture connectoing 2xRS485.

NX8MM-35485485.png

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 

Step1-1gcc modbus.png

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

Step1-2 cdmodbus.png

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

NX8MM-35485485.png

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

Step2-1 modbusserver.png

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

Step2-2 modbusclient.png

After executing the command above, the server (COM4) successfully receives the Modbus RTU signal from the client (COM1), marking the completion of the test.

Step2-3complete.png