ModbusRTU - Industrial Modbus-485 RTU Interface
ModbusRTU
Section titled “ModbusRTU”Path: src/modbus/ModbusRTU.h
Revision History: Initial documentation
ModbusRTU is a comprehensive class that provides a robust interface for Modbus RTU communication over RS-485. It is designed for industrial applications on ESP32, offering efficient queue management, adaptive timeouts, and extensive error handling capabilities.
REQUIREMENTS
Section titled “REQUIREMENTS”-
Hardware:
- ESP32 microcontroller
- RS-485 transceiver (with DE/RE pin for direction control)
- Serial interface for communication
-
Software:
- Arduino framework
- ModbusClientRTU library
- HardwareSerial for serial communication
- ArduinoLog for logging
PROVIDES
Section titled “PROVIDES”-
Enumerations:
E_InitState
: Tracks initialization states (INIT_NOT_STARTED
,INIT_SERIAL_STARTED
,INIT_CLIENT_STARTED
,INIT_READY
,INIT_FAILED
)
-
Classes:
ModbusRTU
: Main class for Modbus RTU communicationManager
: Device management class for handling multiple Modbus slave devices
FEATURES
Section titled “FEATURES”- Non-blocking initialization and operation
- Flexible queue management with priority levels
- Read/write operations for coils and registers (single and multiple)
- Automatic value caching with synchronization status tracking
- Comprehensive error handling and retry mechanisms
- Adaptive minimum operation interval to adjust for network conditions
- Filter chain for operation validation and duplicate prevention
- Callback system for register changes, write completions, and error handling
- Status reporting and debugging utilities
DEPENDENCIES
Section titled “DEPENDENCIES”BEHAVIOUR
Section titled “BEHAVIOUR”PERFORMANCE
Section titled “PERFORMANCE”- Consider buffer pooling for multiple register operations to reduce memory fragmentation
- Implement batch processing for adjacent registers to reduce number of transactions
- Explore more granular timeouts for different device types or operation types
SECURITY
Section titled “SECURITY”- Implement authentication mechanisms for secure Modbus communication
- Add validation for incoming data values to prevent exploitation
- Consider adding packet encryption for sensitive industrial applications
COMPLIANCE
Section titled “COMPLIANCE”- Ensure full compliance with Modbus RTU protocol specification
- Add support for additional function codes to cover all Modbus operations
- Implement proper exception code handling according to the standard
RECOMMENDATIONS
Section titled “RECOMMENDATIONS”- Monitor operation success rates and adjust retry limits based on network reliability
- Configure adaptive timeouts based on device response characteristics
- Implement device-specific wrappers for common industrial equipment
- Use high priority flag for critical operations (safety or timing-sensitive commands)