In modern industrial communication, Modbus protocol is widely used in various automation systems due to its simplicity and wide support. This protocol supports multiple communication methods, such as Serial Terminal Unit (RTU) and TCP/IP. The following article provides a detailed introduction to a Modbus protocol testing program using the libmodbus library, which verifies the functionality of the library by implementing different Modbus functions.
Complete code (Chinese comments):
Program Overview
The goal of this testing program is to verify all the main functions supported by the libmodbus library, including writing a single coil, reading coil status, writing multiple coils, writing a single register, reading a register, writing multiple registers, and reading and writing multiple registers. The program runs in two modes: TCP and RTU, which are selected through command-line parameters.
Environment Settings and Initial Conditions
The program first initializes the Modbus context (ctx) based on input parameters (TCP or RTU). For TCP mode, it connects to port 1502 on the local host. For RTU mode, it is set to communicate at 19200 baud rate through a specific serial port (such as/dev/ttyUSB0).
Data Structure and Initialization
In order to conduct testing, the program first allocates and initializes several arrays to store the bits and register values used in testing:
tab_rq_bits和tab_rp_bitsis used to store the coil (bit) states of requests and responses.tab_rq_registers,tab_rp_registers和tab_rw_rq_registersRegister values used to store requests and responses.
Test Loop
The program performs loop testing within the defined address range, increasing the address by one each time until it covers the range fromADDRESS_START 到 ADDRESS_END. At each address, the program performs the following operations:
- Write to a single coil: Verify if a single coil can be successfully written and read back.
- Write multiple coils: Verify if multiple coils can be successfully written and read back.
- Write a single registerVerify whether a single register can be successfully written and read back.
- Write multiple registersVerify whether multiple registers can be successfully written and read back.
- Read and write multiple registers simultaneouslyVerify whether multiple registers can be read and written simultaneously in one operation.
exception handling
After each Modbus operation, the program checks the returned result to confirm if the operation was successful. If the operation fails, the program will output an error message and increase the failure counter. The testing continues until all scheduled tests are completed.
Result output
After the test is completed, the program will output the test results, displaying the total number of successful or failed operations. This is determined by comparing request data and response data to ensure the integrity and accuracy of the data.
Cleaning and Closing
After the test is completed, the program releases all allocated resources, closes the Modbus connection, and exits.
Through such a testing program, developers can verify the performance and reliability of the Modbus library under different settings, ensuring its stable operation in practical applications. This not only helps developers gain a deeper understanding of the implementation of the Modbus protocol, but also provides a powerful tool for identifying potential issues.
Leave a Reply