Reference
This article delves into the ModBus-RTU communication protocol for sensors, detailing its parameters, data frame formats, example communications, and methods for accurate measurement.
Communication Protocol Description
1. Basic communication parameters
| Interface | Encoding | Data bits | Parity bits | Stop bits | Error checking | Baud rate |
|---|---|---|---|---|---|---|
| RS485 | 8-bit binary | 8 | None | 1 | CRC | 2400bit/s, 4800bit/s, 9600 bit/s configurable, default 9600bit/s |
2. Data frame format definition
Using ModBus-RTU communication protocol, the format is as follows:
- Time for initial structure ≥4 bytes
- Address code = 1 byte
- Function code = 1 byte
- Data area = N bytes
- Error checking = 16-bit CRC code
- Time to end structure ≥4 bytes
- Address code: It is the address of the sensor, which is unique in the communication network (factory default 0x01).
- Function code: Function indication of the command sent by the host. This sensor reads the register function code 0x03 and writes the register function code 0x06
- Data area: The data area is specific communication data. Note that the high byte of 16bits data is first!
- CRC code: two-byte check code.
Host query frame structure:
| Address code | Function code | Register starting address | Register length | Check code low bit | Check code high bit |
|---|---|---|---|---|---|
| 1byte | 1byte | 2byte | 2byte | 1byte | 1byte |
Slave response frame structure:
| Address code | Function code | Number of valid bytes | Data area 1 | Data area 2 | Nth data area | Check code |
|---|---|---|---|---|---|---|
| 1byte | 1byte | 1byte | 2byte | 2byte | 2byte | 2byte |
3. Communication protocol examples and explanations
Example: Read the temperature and humidity value of device address 0x01
Inquiry frame (hexadecimal):
| Address code | Function code | Register starting address | Register length | Check code low bit | Check code high bit |
|---|---|---|---|---|---|
| 0x01 | 0x03 | 0x00 0x00 | 0x00 0x02 | 0xC4 | 0x0B |
Response frame (hexadecimal): (For example, the temperature is -9.7℃ and the humidity is 48.6%RH)
| Address code | Function code | Return the number of valid bytes | Humidity value | Temperature value | Low bit of check code | High bit of check code |
|---|---|---|---|---|---|---|
| 0x01 | 0x03 | 0x04 | 0x01 0xE6 | 0xFF 0x9F | 0x1B | 0xA0 |
Humidity calculation:
- Humidity: 1E6 H (hex) = 48.6%RH
Temperature calculation:
- When the temperature is lower than 0°C, the temperature data is uploaded in complement form.
- Temperature: FF9F H (hex) = -97 => Temperature = -9.7°C
4. Register address
| Register address | PLC or configuration address | Content | Operation | Definition description |
|---|---|---|---|---|
| 0000H | 40001 (decimal) | Moisture content | Read-only | Real-time value of moisture content (expanded 10 times) |
| 0001H | 40002 (decimal) | Temperature value | Read-only | Temperature real-time value (expanded 10 times) |
| 0050H | 40081 (decimal) | Temperature calibration value | Read and write | Integer (expanded 10 times) |
| 0051H | 40082 (decimal) | Moisture content calibration value | Read and write | Integer (expanded 10 times) |
| 07D0H | 42001 (decimal) | Device address | Read and write | 1-254 (factory default 1) |
| 07D1H | 42002 (decimal) | Device baud rate | Read and write | 0 represents 2400 1 represents 4800 2 represents 9600 |
Other Supplementary Information
1、Quick test method
Select a suitable measurement location, avoid stones, and ensure that the steel needle does not touch hard objects. Throw away the topsoil according to the required measurement depth and maintain the original tightness of the soil below. Hold the sensor tightly and insert it vertically into the soil. Do not move left and right when measuring. It is recommended to measure multiple times within a small range of a measuring point and average it.
2、Buried measurement method
Dig a pit with a diameter >20cm vertically, insert the sensor steel needle horizontally into the pit wall at a predetermined depth, fill the pit tightly, and after a period of stabilization, measurements and recordings can be made for days, months or even longer.
3、Things to note
- The steel needle must be fully inserted into the soil during measurement.
- Avoid strong sunlight directly shining on the sensor, which may cause the temperature to be too high. When using in the field, be careful to prevent lightning strikes.
- Do not violently bend the steel needle, do not pull the sensor lead wire forcefully, and do not drop or violently impact the sensor.
- The sensor protection level is IP68, and the entire sensor can be immersed in water.
- Due to the presence of radio frequency electromagnetic radiation in the air, it is not advisable to leave it powered on for a long time in the air.
Was this article helpful?
