--- title: RS485 Component description: Documentation for the RS485 component that provides Modbus RTU communication over RS-485 serial protocol keywords: [RS485, Modbus, RTU, serial, industrial communication, ESP32] --- # RS485 **Path**: [`src/RS485.cpp`](../src/RS485.cpp) **Revision History**: Initial documentation The RS485 component provides Modbus RTU master functionality for industrial communications over the RS-485 serial protocol. It enables communication with various industrial devices that support the Modbus RTU protocol and acts as an interface between these devices and the internal system components. ## REQUIREMENTS ### Hardware - RS-485 transceiver (typically integrated on ESP32 industrial boards) - Properly wired RS-485 network (A/B/GND connections) ### Software - ModbusClientRTU library for Modbus RTU communication - ModbusRTU and ModbusTypes dependencies - Configuration settings in config-modbus.h ## FEATURES - Modbus RTU master implementation - Integration with Modbus TCP for remote access to RTU devices - Device management for multiple slave devices - Register read/write operations - Register change notifications - Periodic device polling ## DEPENDENCIES - [Component](./Component.md) - Base class for all components - [ModbusTCP](./ModbusTCP.md) - For Modbus TCP to RTU bridging - [ModbusBlockView](./ModbusBlockView.md) - For register organization and access - [ArduinoLog](https://github.com/thijse/Arduino-Log) - For logging capabilities - [ModbusClientRTU](https://github.com/eModbus/eModbus) - For Modbus RTU communication ```mermaid graph TD RS485 --> Component RS485 --> ModbusRTU RS485 --> ModbusTCP RS485 --> ModbusBlockView RS485 --> Manager ``` ## BEHAVIOUR The RS485 component initializes during setup and then periodically polls connected devices in its loop function. It also handles Modbus TCP requests by translating them to RTU operations and manages the communication with the RTU slave devices. ```mermaid stateDiagram-v2 [*] --> Initialization Initialization --> Ready Ready --> ProcessingTcpRequests: TCP Request Received Ready --> PollingDevices: Timer Triggered ProcessingTcpRequests --> Ready: Response Sent PollingDevices --> Ready: Polling Complete Ready --> HandleRegisterChange: Register Changed HandleRegisterChange --> Ready ``` ## TODOS ### PERFORMANCE - Optimize polling frequency based on device response times - Implement more efficient register caching to reduce network traffic - Consider batch operations for multiple registers to improve throughput ### SECURITY - Implement proper error handling for malformed Modbus packets - Consider adding CRC validation for extra reliability - Implement timeout handling for unresponsive devices ### COMPLIANCE - Ensure compliance with Modbus RTU specification - Validate against industrial standards for RS-485 communication - Test with different vendor implementations for compatibility ### RECOMMENDATIONS - Use proper RS-485 termination resistors on the physical network - Configure appropriate timeouts based on network complexity and device count - Implement error recovery mechanisms for robust operation - Use shielded twisted pair cables for RS-485 connections in noisy environments ## EXAMPLE Below is an example of how the RS485 component might be constructed and mounted in an application: ```cpp #ifdef ENABLE_RS485 rs485 = new RS485( this // owner ); if (rs485) { components.push_back(rs485); Log.infoln(F("RS485 initialized.")); } else { Log.errorln(F("RS485 initialization failed.")); } #endif ``` ### References - [Modbus RTU Specification](https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf) - [ESP32 Serial Interface Documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/uart.html)