Reference
This article offers a detailed exploration of Modbus communication protocols, covering key parameters, protocol formats, and register tables with examples to aid understanding.
Communication Protocol Description
1、Default communication parameters
| Data bit | stop bit | parity | baud rate |
|---|---|---|---|
| 8 | 1 | No | default 115200 |
2、Modbus protocol parameters
| Mode | check | sensor address | reading function code | writing function code |
|---|---|---|---|---|
| Modbus-RTU | CRC-16/Modbus | can be set, and the default value is 0x01 | 0x03 | 0x06 |
3、Modbus protocol format
The user machine is the master device and the sensor is the slave device.
Host send (read):
| Name | Device address | Function code 0x03 | Register address | Number of registers | CRC16 check |
|---|---|---|---|---|---|
| Length(Byte) | 1 | 1 | 2 | 2 | 2 |
Slave response (read):
| Name | Device address | Function code 0x03 | Number of bytes returned | Data area | CRC16 check |
|---|---|---|---|---|---|
| Length(Byte) | 1 | 1 | 1 | N | 2 |
Host send (write):
| Name | Device address | Function code 0x03 | Register address | Data area | CRC16 check |
|---|---|---|---|---|---|
| Length(Byte) | 1 | 1 | 2 | 2 | 2 |
Slave response (write):
| Name | Device address | Function code 0x03 | Register address | Data area | CRC16 check |
|---|---|---|---|---|---|
| Length(Byte) | 1 | 1 | 2 | 2 | 2 |
4、Modbus register
Register data is high byte before and low byte after.
- Modbus register table 1
| Permission | Address | Function | Data Type | Description |
|---|---|---|---|---|
| Read-only | 0x0100 | Processing value | Unsigned integer, 16 bits | Start ranging after receiving the instruction, and output the distance value in mm with a response time of about 190 ~ 750 ms (different ranges vary) |
| Read-only | 0x0101 | Real-time value | Unsigned integer, 16-bit | The module starts ranging once after receiving the instruction, and outputs the real-time distance value in mm, and the response time is about 15 ~ 140 ms (different ranges vary) |
| Read-only | 0x0102 | Temperature | Unsigned integer, 16-bit | Unit: 0.1℃, resolution: 0.5℃, response time about 5 ~ 140 ms (range varies) |
| Read-only | 0x010A | Echo Time | Unsigned integer, 16-bit | The module starts ranging once after receiving the instruction, and outputs the real-time echo time in us. This value is divided by 5.75 to get the distance value in mm, and the response time is about 5 ~ 140 ms (different ranges vary) |
- Modbus register table 2
| Permission | Address | Function | Data Type | Description |
|---|---|---|---|---|
| Read/write | 0x0200 | Slave address | Unsigned integer, 16 bits | Range: 0x01~0xFE (default 0x01), 0xFF is the broadcast address |
| Read/write | 0x0201 | Baud rate | Unsigned integer, 16 bits | Serial baud rate (default 115200), in bps, which will take effect immediately after setting. The baud rates corresponding to the register values are as follows: 0x0002: 4800, 0x0003: 9600, 0x0004: 14400, 0x0005: 10. |
| Read and write | 0x0205 | Switch output polarity | Unsigned integer, 16 bits | Set the switch output polarity, only the switch mode is valid; 0x00: negative output, less than the threshold value, low output; 0x01: Positive output, less than the threshold output high (default) |
| Read and write | 0x0206 | Set the value of switch value | Unsigned integer, 16 bits | Set the threshold value of switch value, unit: mm, range: 30 ~ 5000 mm, only the switch mode is valid |
| Read and write | 0x0208 | Detect angle level | Unsigned integer, 16 bits | The angle level can be set to level 1 ~ 4 (default level 4); The greater the grade, the greater the detection angle, the more sensitive the induction, and vice versa. 1- The single angle is about 40, the horizontal angle of double angle is about 45, and the vertical angle is about 60; 2- Single angle is about 45, double angle is about 50, and vertical angle is about 65; 3- The single angle is about 55, the horizontal angle of double angle is about 55, and the vertical angle is about 70; 4- Single angle is about 65, double angle is about 65, and vertical angle is about 80; |
| Read and write | 0x0209 | Output distance value data unit | Unsigned integer, 16 bits | Controlled/automatic output protocol distance value unit, 0x00-mm, 0x01-us (this value is divided by 5.75 to get the distance value in mm units), which is only valid for UART automatic and UART controlled modes |
| Read and write | 0x021A | Power noise reduction level | Unsigned integer, 16-bit | Power noise reduction level is divided into 1-5 levels (default is 1), which is suitable for different power supply scenarios; The higher the grade, the greater the noise suppression, and the overall angle will also be affected. The higher the grade, the greater the angle will be affected. Description of different grades: 1- Suitable for battery-powered occasions; 2- Suitable for occasions with certain high-frequency noise such as USB power supply; 3- Suitable for long-distance USB power supply; 4- Suitable for switching power supply; 5- Suitable for switching power supply and complicated environmental interference, generally not recommended; |
| Read and write | 0x021F | Range level | Unsigned integer, 16 bits | Range level 1 ~ 5 (default is 5), range: 1- about 50cm, 2- about 150cm, 3- about 250cm, 4- about 350cm, 5- about 500cm |
- Influence of Baud Rate on Single Packet Communication Duration
| Serial number | Baud rate | Communication duration |
|---|---|---|
| 1 | 4800 | 16ms |
| 2 | 9600 | 8ms |
| 3 | 14400 | 5.6ms |
| 4 | 19200 | 4ms |
| 5 | 38400 | 2.4ms |
| 6 | 57600 | 1.6ms |
| 7 | 76800 | 0.8ms |
| 8 | 115200 | 0.6ms |
The higher the baud rate, the shorter the single packet communication time.
5、Examples of Modbus communication
Example 1: Reading Processing Value Data
Host: 01 03 01 00 00 01 85 F6
Slave: 01 03 02 02 F2 38 A1
Explain: The address of the sensor is 0x01, and the processing distance value is 0x02F2, which is converted into decimal 754 mm..
Example 2: Reading Real-time Value Data
Host: 01 03 01 01 00 01 D4 36
Slave: 01 03 02 02 EF F8 A8
Explain: The address of the sensor is 0x01, and the real-time distance value is 0x02EF, which is converted into decimal 751mm.
Example 3: Reading Temperature Value Data
Host: 01 03 01 02 00 01 24 36
Slave: 01 03 02 01 2C B8 09
Explain: The sensor address is 0x01, the real-time temperature value is 0x012C, and it is 30.0℃ when converted into decimal.
Example 4: Modify the slave address
Host: 01 06 02 00 05 48 71
Slave: 01 06 02 00 05 48 71
Explain: The sensor address was changed from 0x01 to 0x05.
Example 5: Reading Baud Rate
Host: 01 03 02 01 00 01 D4 72
Slave: 01 03 02 00 03 F8 45
Explain: Read the baud rate, which is 9600bps.
Example 6: Setting Baud Rate
Host: 01 06 02 01 00 03 99 B3
Slave: 01 06 02 01 00 03 99 B3
Explain: Set the baud rate to 9600bps.
Installation opening suggestion

Was this article helpful?
