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

3.7 KiB

title description keywords
Modbus485 Component A full-featured Modbus RTU master client implementation for ESP-32 devices, adding extensive error handling and logging.
Modbus
RS485
ESP32
industrial
communication
RTU

Modbus485

Path: src/modbus485.h

Revision History:

  • Initial documentation

A robust Modbus RTU communication component supporting RS-485 electrical interface for ESP-32 devices. This implementation features comprehensive error handling, logging, and state management, making it suitable for industrial applications requiring reliable communication.

REQUIREMENTS

  • Hardware:
    • RS-485 transceiver (e.g., MAX485)
    • UART TX pin
    • UART RX pin
    • DE/RE pin for direction control
  • Software:
    • HardwareSerial
    • ArduinoLog or similar for logging

FEATURES

  • Full Modbus RTU master client implementation
  • Configurable baud rate, parity, and stop bits
  • Support for multiple read and write function codes
  • Automatic transaction ID management
  • Comprehensive error handling and reporting
  • Response timeout management
  • Debug logging
  • Modbus frame validation (CRC16)
  • State machine design for reliable operation
  • Supports single and block operations for coils, discrete inputs, holding registers and input registers

DEPENDENCIES

graph TD
    Modbus485 --> Component
    Modbus485 --> EventManager
    Modbus485 --> ArduinoLog

BEHAVIOUR

stateDiagram-v2
    [*] --> IDLE

    IDLE --> WAITING_TO_SEND: request initiated
    WAITING_TO_SEND --> SENDING: line ready
    SENDING --> WAITING_FOR_RESPONSE: frame sent
    WAITING_FOR_RESPONSE --> PROCESSING_RESPONSE: response received
    WAITING_FOR_RESPONSE --> TIMEOUT_ERROR: timeout
    
    PROCESSING_RESPONSE --> IDLE: success
    PROCESSING_RESPONSE --> ERROR: validation failed
    
    TIMEOUT_ERROR --> IDLE: reset
    ERROR --> IDLE: reset

TODOS

PERFORMANCE

  • Consider implementing a response parser to handle partial responses
  • Add transaction queuing to handle multiple concurrent requests
  • Optimize memory usage for constrained devices
  • Add retry mechanism for failed requests

SECURITY

  • Implement message authentication for critical operations
  • Consider encryption for sensitive data transmission
  • Add access control mechanisms for write operations
  • Implement session timeouts for maintaining connection state

COMPLIANCE

  • Ensure full compliance with Modbus RTU specification
  • Validate against Modbus conformance test suite
  • Document compatibility with specific Modbus devices

RECOMMENDATIONS

  • Use proper shielded cables for RS-485 communication
  • Implement proper termination resistors on the RS-485 bus
  • Consider using optically isolated RS-485 transceivers in noisy environments
  • Monitor response times and adjust timeouts accordingly
  • Implement application-level heartbeats for critical connections

EXAMPLE

#ifdef PIN_RS485_DE
  modbus485 = new Modbus485(
      this,            // owner
      PIN_RS485_TX,    // TX pin
      PIN_RS485_RX,    // RX pin
      PIN_RS485_DE,    // DE/RE pin
      RS485_BAUDRATE,  // baud rate
      RS485_CONFIG     // UART configuration
  );
  
  if (modbus485) {
    components.push_back(modbus485);
    Log.infoln(F("Modbus485 initialized. TX:%d, RX:%d, DE:%d, Baud:%d"),
               PIN_RS485_TX, PIN_RS485_RX, PIN_RS485_DE, RS485_BAUDRATE);
  } else {
    Log.errorln(F("Modbus485 initialization failed."));
  }
#endif

References