firmware-base/docs-c/components/RS485.md
2025-06-04 13:45:12 +02:00

3.7 KiB

title description keywords
RS485 Component - Modbus RTU Interface RS485 component provides a Modbus RTU interface for industrial applications
ESP32
RS485
Modbus
RTU
industrial
interface
communication

RS485

Path: src/RS485.cpp

Revision History: Initial documentation

The RS485 component provides a Modbus RTU interface for industrial communications. It acts as a gateway between Modbus TCP and Modbus RTU, managing serial communications with connected hardware devices and exposing them to the higher-level systems.

REQUIREMENTS

  • Hardware:
    • RS485 transceiver (typically connected to a UART port)
    • GPIO pin for DE/RE (Data Enable/Receive Enable) control
    • UART pins (RXD1_PIN, TXD1_PIN)
  • Software:
    • ModbusClientRTU library
    • ModbusRTU implementation
    • Configuration in config-modbus.h

FEATURES

  • Manages communication with multiple Modbus RTU slave devices
  • Bridges between Modbus TCP and Modbus RTU protocols
  • Monitors and reports register changes from devices
  • Provides error handling and reporting
  • Supports device discovery and monitoring
  • Dynamic registration of device capabilities with TCP server
  • Periodic polling of RTU devices

DEPENDENCIES

graph TD
    RS485 --> Component
    RS485 --> ModbusRTU
    RS485 --> ModbusTypes
    RS485 --> RTUutils
    RS485 --> RS485Devices

BEHAVIOUR

stateDiagram-v2
    [*] --> Initialize
    Initialize --> Running: Setup successful
    Initialize --> Error: Setup failed
    
    Running --> ProcessLoop: RS485_LOOP_INTERVAL_MS
    ProcessLoop --> HandleRegisterChange: On device register change
    ProcessLoop --> HandleDeviceStatus: Device status updated
    ProcessLoop --> Running: Return to main loop
    
    HandleRegisterChange --> ForwardToTCP: Notify TCP interface
    HandleRegisterChange --> Running
    
    Error --> [*]

TODOS

PERFORMANCE

  • Consider optimizing polling frequency based on device importance or activity
  • Implement batched read operations for devices with consecutive register addresses
  • Add performance metrics tracking for RS485 bus utilization and response times

SECURITY

  • Implement validation of received data to prevent buffer overflows
  • Add authentication mechanisms for critical device communications
  • Consider encryption for sensitive data transmission

COMPLIANCE

  • Ensure full compliance with Modbus RTU specification
  • Consider implementing Modbus exceptions handling according to standard
  • Document protocol compatibility with various industrial equipment standards

RECOMMENDATIONS

  • Create a configuration tool to easily manage device mappings
  • Implement automatic baudrate detection for connected devices
  • Add diagnostic features for bus monitoring and troubleshooting
  • Consider implementing a fallback mechanism for unreliable connections

EXAMPLE

This example shows how to initialize and mount the RS485 component in a main application:

#ifdef ENABLE_RS485
  rs485 = new RS485(this); // 'this' is the owner component
  if (rs485) {
    components.push_back(rs485);
    Log.infoln(F("RS485 component initialized."));
    
    // Register RS485 as ModbusTCP data provider, if TCP is available
#ifdef ENABLE_MODBUS_TCP
    if (modbusTCP) {
      rs485->mb_tcp_register(modbusTCP);
    }
#endif
  } else {
    Log.errorln(F("RS485 initialization failed."));
  }
#endif

References