How ceremcem/modbus_lib works
Main Components
• Modbus Core Library (modbus_lib.c & modbus_lib.h):
– Central processing logic for the Modbus protocol.
– Handles telegram buffering, message parsing, and dispatches commands.
– Contains the initialization routine (modbus_lib_init) where configuration is set up.
• CRC Calculation Module (modbus_crc.c & modbus_crc.h):
– Provides functions to perform CRC checks on Modbus telegrams.
• Data Types Definitions (modbus_lib_types.h):
– Contains type definitions or enumerations used across the library.
• Platform Abstraction / Port File (port.h):
– Acts as an abstraction layer to handle platform-specific details (e.g., enabling portability across microcontrollers).
• User-Implemented Handlers:
– Read Handler (modbus_lib_read_handler) and Write Handler (modbus_lib_write_handler) that the end-user provides to process application-specific data.
– Transport functions such as modbus_lib_transport_write, which tie directly into the hardware’s UART transmit functionality.
• Integration with Hardware:
– The sample usage provided (USART2_dataHandler and USART2_idleHandler functions) illustrates how the library’s append and telegram-end functions are triggered from interrupt handlers or ISR-equivalents in an embedded environment.
Relationships and Flow
• Microcontroller’s UART interface (or similar serial port) from which data is received.
• Data Flow within the Library:
• Role of the CRC module by linking the “Modbus Core Library” to the “CRC Calculation Module” during message validation.
• User Callbacks and Error Handling:
• That once a complete telegram is received (after timeout via modbus_lib_end_of_telegram), the library dispatches the message to the user-defined Read/Write Handlers.
• Outgoing Data:
Architectural Patterns and Design Principles
• Modularity: Separate functional concerns such as protocol handling, CRC computation, and platform-specific porting.
• A block for the “Hardware UART Interface” connecting to interrupt handlers (USART2_dataHandler and USART2_idleHandler).
• A block for the “Modbus Core” (modbus_lib.c) that receives data via modbus_lib_append_data, processes complete telegrams with modbus_lib_end_of_telegram, and uses the CRC module for data validation.
• A block for “User-Implemented Callbacks” encapsulating the Read and Write handlers.
• A block for “CRC Calculation Module” that interacts with the core processing flow.
• A block for “Transport Write Function” (modbus_lib_transport_write) which sends data back via UART.
• Data entrance (UART → Data Handler → Append Data).
• Telegram completion and processing (Buffer → Processing → Handler invocation).
• Outgoing data path (Processing → Transport Write → UART).
• Notations of configuration initialization (modbus_lib_init & ModbusConfig_t) within the “Application Main” block.