Reference
The article serves as a comprehensive guide to the Modbus communication protocol, explaining default communication parameters, protocol formats, register tables, and practical examples of host-sensor interactions, highlighting reading and writing operations, baud rate configurations, and sensor address modifications for efficient data exchange.
Communication Protocol Description
Modbus 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 ~ 30~3000mm, 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- angle is about 30, 2- angle is about 40, 3- angle is about 50, and 4- angle is about 60 |
| 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 grade | Unsigned integer, 16 bits | Range grade 1- 4 (default is 4), range: 1-about 50cm, real-time value response time 15-80 ms, and processed value response time 190-500 ms; 2- about 150cm, the real-time value response time is 20-90 ms, and the processing value response time is 230-550 ms; 3- about 250cm, the real-time value response time is 25-100 ms, and the processing value response time is 250-600 ms; 4- about 350cm, the real-time value response time is 35-110 ms, and the processing value response time is 280-650 ms; |
- 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.
Dimensional Drawing

Installation opening suggestion

Was this article helpful?
