Modbus read-write coil register communication test program based on libmodbus library

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Modbus read-write coil register communication test program based on libmodbus library缩略图

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_bitstab_rp_bitsis used to store the coil (bit) states of requests and responses.
  • tab_rq_registers, tab_rp_registerstab_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_STARTADDRESS_END. At each address, the program performs the following operations:

  1. Write to a single coil: Verify if a single coil can be successfully written and read back.
  2. Write multiple coils: Verify if multiple coils can be successfully written and read back.
  3. Write a single registerVerify whether a single register can be successfully written and read back.
  4. Write multiple registersVerify whether multiple registers can be successfully written and read back.
  5. 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.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.