2025-01-10 15:20:26
In the field of industrial automation, communication between devices is of great importance, and the MODBUS-RTU protocol is like a "universal language" that enables devices from different manufacturers to "communicate" with each other and work together. Today, let's analyze this amazing protocol in depth.
I. Overview of the MODBUS-RTU Protocol
Birth and Application Scenarios of the Protocol
The MODBUS protocol was originally released by Modicon (now a brand under Schneider Electric) in 1979 to solve the communication problems between different devices in industrial control. After years of development and improvement, MODBUS-RTU has been widely used in various industrial scenarios, such as factory automation production lines, intelligent building control systems, power monitoring systems, etc. Whether it is a PLC (Programmable Logic Controller), a sensor, a frequency converter, or other intelligent devices, as long as they support the MODBUS-RTU protocol, they can be easily connected to a unified communication network for data interaction.
Features and Advantages
It has significant advantages such as simplicity, openness, and low cost. Its simplicity allows developers and engineers to quickly get started and implement communication between devices; its openness enables numerous manufacturers to apply it to their products, promoting the interconnection of devices; and its low cost reduces the overall construction cost of industrial control systems and improves cost performance.
II. Analysis of the MODBUS-RTU Protocol Frame Format
Start and End Bits
The message frame of the MODBUS-RTU protocol starts with an idle interval of at least 3.5 character times as the start bit, indicating the beginning of a frame of data. Similarly, the end bit is also an idle interval of at least 3.5 character times, marking the end of a frame of data. This way of using time intervals as the start and end of a frame not only ensures data integrity but also reflects the simplicity of the protocol design.
Address Field
Immediately following the start bit is the address field, which occupies one byte. This address is used to identify the address of the slave device. The master station uses this address to specify which slave station to communicate with. In a MODBUS network, each slave station has a unique address, usually in the range of 0 - 247, where 0 is the broadcast address, and the master station can send messages to all slave stations through the broadcast address.
Function Code Field
After the address field is the function code field, which also occupies one byte. The function code determines the type of operation of the frame of data. For example, reading input registers (function code 0x04), writing a single coil (function code 0x05), reading holding registers (function code 0x03), etc. Different function codes correspond to different operations, and the rich variety of function codes enables the MODBUS-RTU protocol to meet various industrial control requirements.
Data Field
Depending on the function code, the length and content of the data field will vary. It contains the specific data to be transmitted between the master station and the slave station, such as the register address, the value to be written, the number of registers to be read, etc. The flexibility of the data field allows the protocol to adapt to various complex data interaction situations.
Checksum Field
Finally, there is the checksum field. The commonly used check method is CRC (Cyclic Redundancy Check), which occupies two bytes. The CRC check calculates a specific value for the previous address field, function code field, and data field to generate a checksum and attaches it to the end of the frame. After receiving the data, the receiver will perform the same CRC check calculation. If the calculation result is consistent with the received checksum, the data is considered to be transmitted correctly; otherwise, it indicates that an error may have occurred during the data transmission.
III. Examples of the MODBUS-RTU Protocol Communication Process
Master Station Reading Data from Slave Station (Taking Reading Holding Registers as an Example)
1. The master station first constructs a MODBUS-RTU request frame. In the address field, it fills in the address of the slave station to be accessed (suppose it is 0x01), in the function code field, it fills in 0x03 (indicating reading holding registers), and in the data field, it specifies the starting address of the holding registers to be read (such as 0x0000) and the number of registers (such as 0x0005). Then it calculates the CRC checksum and attaches it to the end of the frame, and sends the entire request frame to the slave station.
2. After receiving the request frame, the slave station first checks whether the address matches its own address and then verifies the CRC. If both are correct, the slave station reads the corresponding data from its own holding registers according to the information in the request function code and data field, constructs a response frame. The address field and function code field of the response frame are the same as those of the request frame, the data field contains the register values read, and then it calculates the CRC checksum and attaches it to the end of the response frame, and sends the response frame back to the master station.
3. After receiving the response frame from the slave station, the master station also performs address and CRC verification. After the verification passes, it can obtain the holding register data of the slave station from the data field of the response frame, completing a data reading operation.
Master Station Writing Data to Slave Station (Taking Writing a Single Coil as an Example)
1. The master station constructs a request frame, fills in the slave station address (such as 0x02) in the address field, fills in 0x05 (indicating writing a single coil) in the function code field, and the data field contains the coil address to be written (such as 0x000A) and the value to be written (such as 0xFF00 indicating closing the coil, 0x0000 indicating opening the coil). After calculating the CRC checksum and attaching it to the end of the frame, it sends the frame to the slave station.
2. After receiving the request frame, the slave station performs address and CRC verification. After passing, it updates the value of the corresponding coil according to the request information and constructs a response frame. The address field and function code field of the response frame are the same as those of the request frame, and the data field can return information such as the result of the writing operation according to the situation. After calculating the CRC checksum and attaching it to the end of the response frame, it sends the response frame back to the master station. After receiving the response frame and passing the verification, the master station knows whether the writing operation is successful.
The MODBUS-RTU protocol plays an important role in the field of industrial communication with its simple and efficient design, rich functions, and wide applicability. Through an in-depth analysis of its protocol, we can better understand and apply it, making the communication between industrial devices smoother and more reliable.