I'm learning the MODBUS protocol. I'll first try it on Linux devices. My first step is to learn the core data exchange mechanisms and transport layer options.
Transport layer options are TCP/IP and serial. I'm going to start with serial communication. This works with any UART (USB, RS-232, RS-485). I'm using RS-485.
image source: left: me, right: edited from one of many sources of the same image
For the Linux exercise, I'm using two AVNET SmartEdge IIOT Gateways. They have an RS-XXX port that I configured as RS-485 on both, and connected using the schematic above. The SmartEdge user guide tells me that the character device for the RS-XXX is /dev/ttySC0.
To use LIBMODBUS, you need to have the libmodbus package installed on your Linux box. If you want to build the code on that Linux box, you also need to have the libmodbus-dev package. I'm cross-compiling from a windows PC, so my Linux device only needs libmodbus. On my windows machine, I copied the library from the Linux file system, and got the MODBUS header files from github.
To build the executables on Linux, here's the command line: |
As example, I used a gateway and sensor example from Julien Grossholtz. My development environment is Eclipse on Windows, with an ARM cross-compile GCC toolchain.
The sensor code is a demo. It reads two attributes from the processor and puts that in the MODBUS payload. The original example uses two temperature zone measurements. My AVNET device (a Pi derivative) only has one temperature zone, so I selected processor frequency as a second data point.
/Simple function that grab temperature and frequency on my Pi and copy it to the mapping //structure from libmodbus ( this code is for test purpose ). void update_telemetry(modbus_mapping_t *mb_mapping) { FILE *f1,*f2; uint32_t temp,freq; size_t s; f1 = fopen("/sys/class/thermal/thermal_zone0/temp","r"); fscanf(f1,"%"PRIu32,&temp); temp = temp/1000; mb_mapping->tab_registers[0] =(uint16_t) temp; f2 = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq","r"); fscanf(f2,"%"PRIu32,&freq); freq = freq/1000; mb_mapping->tab_registers[1] =(uint16_t) freq; fclose(f1); fclose(f2); }
The only other changes I made was the character device filename and the speed. In both files.
#define UART_PATH "/dev/ttySC0" // #define UART_PATH "/dev/ttyUSB1" #define BAUD (9600) // ... //Set uart configuration and store it into the modbus context structure ctx = modbus_new_rtu(UART_PATH, BAUD, 'N', 8, 1);
Here is a screenshot with both programs running (and being debugged), each on a different Gateway.
You may spot that the output shows 2 identical temperatures. That's because I hadn't incorporated the CPU frequency in the sensor code yet (I used zone0 temperature for the two MODBUS register values). Here is the data exchange after that modification.
Some highlights on the example:
- It uses RTU (binary) data protocol. MODBUS has a second one: ASCII.
- The MODBUS address of the sensor device is 0
- Two data points (registers) are exchanged.