Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
temperature humidity sensor convertor – TR RS485 Modbus protocol
#1
Code:
================================================================================
  RS485 TEMPERATURE & HUMIDITY SENSOR PROTOCOL SPECIFICATION
================================================================================

Communication: RS485 (Modbus-based Custom Protocol)

================================================================================
1. OVERVIEW
================================================================================

This document describes the custom Modbus-based protocol for communicating with
temperature and humidity sensors over RS485. The protocol supports up to 32
sensors per slave device, allowing for distributed sensor networks.

Key Features:
- Modbus RTU framing with CRC16
- Custom function code for sensor data reading
- Support for 1-32 sensors per slave address
- Temperature and humidity readings with 0.1 degree/percent precision
- Explicit encoding for unavailable sensor data

================================================================================
2. PROTOCOL PARAMETERS
================================================================================

Modbus Function Code: 0x0A (READ_SENSOR_DATA)
Baud Rate: As configured by RS485 driver
Data Format: 8N1 (8 data bits, no parity, 1 stop bit)
CRC: Modbus CRC16 (polynomial 0xA001)

Sensor Address Range: 1-32 (0x01-0x20)
Slave Address Range: 1-247 (0x01-0xF7, standard Modbus range)

================================================================================
3. REQUEST FORMAT
================================================================================

The master sends a request to read sensor data from a specific sensor.

Request Frame Structure:
┌──────────────┬──────────────┬────────────────┬────────────────┬──────────┐
│ Byte Index   │ 0            │ 1              │ 2              │ 3        │
├──────────────┼──────────────┼────────────────┼────────────────┼──────────┤
│ Field        │ Slave Addr   │ Function Code  │ Sensor ID High │ Sensor ID│
│              │              │                │                │ Low      │
├──────────────┼──────────────┼────────────────┼────────────────┼──────────┤
│ Value        │ 0x01-0xF7    │ 0x0A           │ 0x00           │ 0x01-0x20│
└──────────────┴──────────────┴────────────────┴────────────────┴──────────┘
┌──────────────┬──────────────┐
│ Byte Index   │ 4-5          │
├──────────────┼──────────────┤
│ Field        │ CRC16        │
├──────────────┼──────────────┤
│ Value        │ Lo-Hi bytes  │
└──────────────┴──────────────┘

Total Length: 6 bytes

Field Descriptions:
- Slave Address: The Modbus slave device address (1-247)
- Function Code: Always 0x0A for sensor data reading
- Sensor ID High: Always 0x00 (reserved for future use)
- Sensor ID Low: Sensor number (1-32) on the slave device
- CRC16: Modbus CRC16 checksum (low byte first, then high byte)

Example Request (Read sensor 1 from slave 0x01):
  Request: 01 0A 00 01 [CRC_LOW] [CRC_HIGH]

================================================================================
4. RESPONSE FORMAT
================================================================================

The slave responds with temperature and humidity data for the requested sensor.

Response Frame Structure:
┌─────────────┬────────────┬──────────────┬────────────┬──────────────────┐
│ Byte Index  │ 0          │ 1            │ 2          │ 3-7              │
├─────────────┼────────────┼──────────────┼────────────┼──────────────────┤
│ Field       │ Slave Addr │ Function Code│ Byte Count │ Temperature Data │
│             │            │              │            │ (5 bytes)        │
├─────────────┼────────────┼──────────────┼────────────┼──────────────────┤
│ Value       │ 0x01-0xF7  │ 0x0A         │ 0x0A (10)  │ See section 5    │
└─────────────┴────────────┴──────────────┴────────────┴──────────────────┘
┌─────────────┬──────────────────┬──────────┐
│ Byte Index  │ 8-12             │ 13-14    │
├─────────────┼──────────────────┼──────────┤
│ Field       │ Humidity Data    │ CRC16    │
│             │ (5 bytes)        │          │
├─────────────┼──────────────────┼──────────┤
│ Value       │ See section 5    │ Lo-Hi    │
└─────────────┴──────────────────┴──────────┘

Total Length: 15 bytes

Field Descriptions:
- Slave Address: Echo of the slave address from request
- Function Code: Echo of 0x0A
- Byte Count: Always 0x0A (10 decimal) - 5 bytes temp + 5 bytes humidity
- Temperature Data: 5-byte encoded temperature value
- Humidity Data: 5-byte encoded humidity value
- CRC16: Modbus CRC16 checksum (low byte first, then high byte)

Minimum Valid Response Length: 14 bytes
Expected Response Length: 15 bytes (with CRC)

================================================================================
5. DATA ENCODING
================================================================================

Both temperature and humidity use the same 5-byte encoding format:

5.1 Data Structure
┌──────────────┬──────────┬──────────┬──────────┬──────────┬──────────┐
│ Byte Index   │ 0        │ 1        │ 2        │ 3        │ 4        │
├──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┤
│ Field        │ Sign     │ Hundreds │ Tens     │ Ones     │ Decimal  │
├──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┤
│ Value Range  │ 0 or 1   │ 0-9      │ 0-9      │ 0-9      │ 0-9      │
└──────────────┴──────────┴──────────┴──────────┴──────────┴──────────┘

5.2 Field Definitions
- Sign: 0 = positive, 1 = negative
- Hundreds: Hundreds digit (0-9)
- Tens: Tens digit (0-9)
- Ones: Ones digit (0-9)
- Decimal: First decimal place (0-9, represents 0.0-0.9)

5.3 Value Calculation
Final Value = Sign × (Hundreds × 100 + Tens × 10 + Ones + Decimal × 0.1)

Where Sign is: +1 if sign byte is 0, -1 if sign byte is 1

5.4 Data Not Available Indicator
When sensor data is not available or invalid, ALL 5 bytes are set to 0xFF:
  0xFF 0xFF 0xFF 0xFF 0xFF

The receiver should check if any byte is NOT 0xFF to determine if data is valid.
If all bytes are 0xFF, the data should be treated as unavailable (-100.0 default).

5.5 Encoding Examples

Example 1: Temperature = 25.3°C
  Bytes: 00 00 02 05 03
  Calculation: +(0×100 + 2×10 + 5 + 3×0.1) = +25.3

Example 2: Temperature = -5.7°C
  Bytes: 01 00 00 05 07
  Calculation: -(0×100 + 0×10 + 5 + 7×0.1) = -5.7

Example 3: Humidity = 65.0%
  Bytes: 00 00 06 05 00
  Calculation: +(0×100 + 6×10 + 5 + 0×0.1) = +65.0

Example 4: Data not available
  Bytes: FF FF FF FF FF
  Interpretation: No valid data (-100.0 default value)

Example 5: Temperature = 100.5°C
  Bytes: 00 01 00 00 05
  Calculation: +(1×100 + 0×10 + 0 + 5×0.1) = +100.5

================================================================================
6. COMPLETE TRANSACTION EXAMPLE
================================================================================

Master requests data from sensor 1 on slave 0x01:

REQUEST (6 bytes):
  01 0A 00 01 XX XX
  └─ Slave address: 0x01
     └─ Function code: 0x0A
        └─ Sensor ID high: 0x00
           └─ Sensor ID low: 0x01
              └─ CRC16 (calculated over bytes 0-3)

RESPONSE (15 bytes):
  01 0A 0A 00 00 02 05 03 00 00 06 05 00 XX XX
  └─ Slave address: 0x01
     └─ Function code: 0x0A
        └─ Byte count: 0x0A (10 bytes)
           └─ Temperature: 00 00 02 05 03 (+25.3°C)
                          └─ Humidity: 00 00 06 05 00 (+65.0%)
                                      └─ CRC16

Decoded Result:
  Temperature: +25.3°C
  Humidity: +65.0%

================================================================================
7. ERROR HANDLING
================================================================================

7.1 Invalid Response Conditions
The receiver should reject responses in the following cases:

1. Slave address mismatch: Response slave address ≠ request slave address
2. Function code mismatch: Response function code ≠ 0x0A
3. Insufficient length: Response length < 14 bytes
4. Invalid byte count: Byte count field ≠ 0x0A (10)
5. CRC error: Calculated CRC ≠ received CRC

7.2 Data Validation
After successful frame validation, check each sensor value:

- If all 5 bytes are 0xFF: Data not available, use default (-100.0)
- If at least one byte is NOT 0xFF: Decode the value normally
- Value range checking should be performed at application level

7.3 Timeout Handling
Recommended timeout: 50-100ms after sending request
If no response within timeout: Retry or report communication error

7.4 Modbus Exception Responses
Standard Modbus exception codes may be returned:
  [Slave Addr] [0x8A] [Exception Code] [CRC]

Where 0x8A = 0x0A + 0x80 (function code with MSB set)

Common exception codes:
- 0x01: Illegal Function (function code not supported)
- 0x02: Illegal Data Address (sensor address out of range)
- 0x03: Illegal Data Value (invalid request parameters)
- 0x04: Slave Device Failure (sensor read error)

================================================================================
8. IMPLEMENTATION NOTES
================================================================================

8.1 Multi-Sensor Support
- A single slave device can support 1-32 sensors (addresses 1-32)
- Each sensor must be initialized before reading
- Sensor addresses are independent of slave addresses
- Multiple slaves can exist on the same RS485 bus

8.2 Polling Frequency
- Recommended polling interval: 1-5 seconds per sensor
- Add delays between consecutive requests to prevent bus collisions
- Minimum delay between requests: 50ms

8.3 Bus Configuration
- RS485 requires proper termination resistors (120Ω)
- Maximum bus length depends on baud rate (typically 1200m at 9600 baud)
- Maximum number of devices: 32-247 (depending on hardware)

8.4 Data Caching
- The master typically caches the last received values
- Cached values are updated when new responses arrive
- Applications read from cache rather than directly from bus

8.5 CRC Calculation
- Use standard Modbus CRC16 algorithm
- Polynomial: 0xA001 (reversed 0x8005)
- Initial value: 0xFFFF
- CRC bytes transmitted: low byte first, then high byte

================================================================================
9. PROTOCOL LIMITATIONS
================================================================================

- Maximum sensors per slave: 32
- Temperature/humidity precision: 0.1 units
- Maximum value range: -999.9 to +999.9
- No timestamp or sensor identification in response
- No batch reading (must query each sensor individually)
- No sensor configuration commands in this protocol version

================================================================================
END OF SPECIFICATION
================================================================================
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)