You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This document describes the non-standard aspects of Waveshare's Modbus RTU implementation and the workarounds implemented in our custom RTU module to handle these quirks.
4
+
5
+
## Overview
6
+
7
+
Waveshare produces a variety of Modbus RTU devices including relay modules, analog I/O modules, and other industrial control components. While these devices are advertised as Modbus RTU compatible, they implement several non-standard behaviors that require special handling for reliable communication.
8
+
9
+
## Non-Standard Behaviors
10
+
11
+
### 1. CRC Calculation Variations
12
+
13
+
Standard Modbus RTU uses CRC-16 with polynomial 0xA001 (reversed 0x8005) and initial value 0xFFFF. Waveshare devices exhibit the following CRC variations:
14
+
15
+
-**Byte Order Swapping**: Some devices return CRC bytes in big-endian order instead of the standard little-endian order
16
+
-**Alternative Initial Values**: Some devices use 0x0000 as the initial CRC value instead of 0xFFFF
17
+
-**Alternative Polynomials**: Some devices use 0x8408 as the polynomial
18
+
-**Reversed Data Bytes**: Some devices calculate CRC on reversed data bytes
19
+
20
+
Our implementation tries multiple CRC calculation methods to accommodate these variations.
21
+
22
+
### 2. Function Code Handling
23
+
24
+
Waveshare devices often respond with different function codes than what was requested:
25
+
26
+
- Responding to function code 0x03 (Read Holding Registers) with 0x04 (Read Input Registers) or vice versa
27
+
- Using custom function codes in the range 0x41-0x44 and 0x65-0x68
28
+
- Sometimes responding with function code 0x00 (zero)
29
+
- Off-by-one errors in function codes (e.g., responding to 0x03 with 0x02 or 0x04)
30
+
31
+
Our implementation includes mappings for these non-standard function code responses.
32
+
33
+
### 3. Unit ID Handling
34
+
35
+
Waveshare devices sometimes respond with:
36
+
37
+
- Unit ID 0 (broadcast address) regardless of the requested unit ID
38
+
- Unexpected unit IDs that don't match the request
39
+
- Multiple devices responding on the same bus with different unit IDs
40
+
41
+
Our implementation allows processing responses despite unit ID mismatches in certain cases.
42
+
43
+
### 4. Timing and Response Characteristics
44
+
45
+
Waveshare devices have specific timing requirements:
46
+
47
+
-**Variable Response Timing**: Devices may need longer delays between request and response
48
+
-**Chunked Responses**: Some devices send data in chunks with small delays between chunks
49
+
-**Buffer Clearing Requirements**: Devices may require more thorough buffer clearing between requests
50
+
-**Exponential Backoff**: Devices may respond better with progressively longer delays between retries
51
+
52
+
Our implementation uses adaptive timing and exponential backoff for retries.
53
+
54
+
## Implemented Workarounds
55
+
56
+
### CRC Validation
57
+
58
+
```python
59
+
# Try multiple CRC calculation methods
60
+
# 1. Standard CRC calculation (little-endian)
61
+
# 2. Swapped byte order (big-endian)
62
+
# 3. Alternative initial value (0x0000)
63
+
# 4. Alternative polynomial (0x8408)
64
+
# 5. Reversed data bytes
65
+
```
66
+
67
+
### Function Code Compatibility
68
+
69
+
```python
70
+
# Compatible function code pairs for Waveshare devices
- Try different baud rates (9600 is most common for Waveshare)
96
+
- Ensure proper grounding and wiring
97
+
- Try shorter cable lengths
98
+
- Add a small delay (10-50ms) between requests
99
+
100
+
### Function Code Mismatches
101
+
102
+
If function code mismatches occur:
103
+
- Verify the device supports the requested function
104
+
- Check the device documentation for supported function codes
105
+
- Try alternative function codes (e.g., use 0x04 instead of 0x03)
106
+
107
+
### Timeout Issues
108
+
109
+
If timeout errors persist:
110
+
- Increase the timeout value (default is 1 second)
111
+
- Try a lower baud rate
112
+
- Increase the number of retries
113
+
- Add longer delays between retries
114
+
115
+
## Device-Specific Notes
116
+
117
+
### Relay Modules
118
+
119
+
- Often respond with function code 0x00 for read coil operations
120
+
- May require multiple write attempts for reliable operation
121
+
- Sometimes report success even when the operation failed
122
+
123
+
### Analog Input Modules
124
+
125
+
- May use non-standard register mapping
126
+
- Often require specific data formats for configuration
127
+
- May have timing-sensitive calibration procedures
128
+
129
+
### RS485 Adapters
130
+
131
+
- USB-to-RS485 adapters may require specific drivers
132
+
- Some adapters have poor buffer handling requiring longer delays
133
+
- Automatic flow control may interfere with Modbus timing
134
+
135
+
## Conclusion
136
+
137
+
While Waveshare Modbus RTU devices don't fully comply with the standard protocol, our custom implementation handles these quirks to provide reliable communication. The module includes extensive logging to help diagnose issues and implements multiple fallback mechanisms for robust operation.
0 commit comments