Detailed explanation of writing multiple registers in Modbus RTU

1. Protocol Overview
Modbus RTU is a serial communication protocol used for inter device communication in industrial automation. The main characteristics are simplicity and stability. In this mode, data is transmitted in binary form.
2. Write multiple registers
When we need to modify multiple parameters on a device (such as a motor driver or sensor), we can use the "write multiple registers" function.
3. Request format
A standard Modbus RTU request to write multiple registers includes the following parts:
- Device address1 byte, identifying the target device to be communicated with.
- function code1 byte, write the function code for multiple registers as
0x10。 - Starting register address: 2 bytes, specifying the address of the first register to be written.
- number of registers: 2 bytes, specifying the total number of registers to be written.
- Byte count1 byte represents the total number of bytes of data to be written in the future.
- dataAccording to the number of registers that need to be written, each register is 2 bytes.
- CRC check2 bytes, used to verify the integrity of the message.
4. Examples
Assuming we need to address0x01Write to the device from the register address0x3000Start with four consecutive registers. The data we need to write is:0x1234, 0x5678, 0x9ABC, 0xDEF0。
The constructed message is:
01 (设备地址) 10 (功能码) 30 00 (起始寄存器地址) 00 04 (寄存器数量) 08 (字节计数,4寄存器*2字节/寄存器) 12 34 56 78 9A BC DE F0 (数据) ?? ?? (CRC校验, 实际值需要使用crc16-modbus来计算)
Please note that the CRC check value is calculated based on the entire message (excluding the CRC). You need to use the CRC algorithm of Modbus RTU to obtain this value and add it to the end of the message.
5. Send and wait
After completing the message, send it through an appropriate serial communication interface (such as RS-485) and wait for a response from the slave device. The response format is similar to the request, but does not include byte counting and data parts.
嘉
嘉