firmware-base/docs-c/core/SerialMessage.md
2025-06-04 16:50:34 +02:00

3.5 KiB

title description keywords
SerialMessage - Serial Communication Component A component that handles serial communication over a stream, providing methods for reading and parsing command messages. SerialMessage, serial communication, command parsing, ESP32, modbus

SerialMessage

Path: src/modbus/SerialMessage.h

Revision History: Initial documentation

SerialMessage is a component that handles serial communication over a provided Stream. It is responsible for reading incoming data from the serial port, parsing it into structured command messages, and forwarding these messages to its owner component. The class periodically checks for new messages on the configured serial interface.

REQUIREMENTS

  • A valid Stream object (like Serial, Serial1, etc.) for communication
  • Sufficient memory for the receive buffer (SERIAL_RX_BUFFER_SIZE, defaults to 256 bytes)

PROVIDES

  • SerialMessage class: Main component for handling serial communication

FEATURES

  • Periodic checking for new serial commands (configurable interval)
  • Parsing of raw serial data into structured CommandMessage objects
  • Event-based message handling through owner component notification
  • Configurable message delimiter (default: newline)
  • Debug capabilities for troubleshooting communication issues

DEPENDENCIES

graph TD
    SerialMessage --> Component
    SerialMessage --> CommandMessage
    SerialMessage --> Vector
    SerialMessage --> ArduinoLog
    SerialMessage --> xtypes
    SerialMessage --> config

BEHAVIOUR

The SerialMessage component operates on a time-based polling mechanism:

graph TD
    Start([Start]) --> Setup[Setup SerialMessage]
    Setup --> Loop[Loop]
    Loop --> CheckInterval{Time to check?}
    CheckInterval -->|No| Loop
    CheckInterval -->|Yes| ReadSerial[Read from serial]
    ReadSerial --> DataAvailable{Data available?}
    DataAvailable -->|No| UpdateTime[Update lastRead time]
    DataAvailable -->|Yes| ParseMessage[Parse to CommandMessage]
    ParseMessage --> ValidMessage{Valid message?}
    ValidMessage -->|No| UpdateTime
    ValidMessage -->|Yes| NotifyOwner[Notify owner component]
    NotifyOwner --> UpdateTime
    UpdateTime --> Loop

TODOS

PERFORMANCE

  • Consider implementing interrupt-driven serial reading rather than polling for high-throughput applications
  • Optimize buffer handling for memory-constrained environments
  • Evaluate the impact of the polling interval on application responsiveness

SECURITY

  • Add input validation to prevent buffer overflow attacks
  • Consider implementing message authentication for secure applications
  • Evaluate the need for encryption for sensitive data transmission

COMPLIANCE

  • Ensure compliance with relevant industrial communication standards when used in Modbus-485 applications
  • Verify error handling mechanisms meet application reliability requirements

RECOMMENDATIONS

  • Set an appropriate SERIAL_COMMAND_PARSE_INTERVAL based on your application's requirements
  • For debugging communication issues, enable DEBUG_SERIAL_MESSAGES in your build configuration
  • When extending this component, consider implementing additional message validation logic
  • For applications with multiple serial interfaces, create separate SerialMessage instances for each interface