# ceremcem/modbus_lib architecture

> Architecture diagram of the GitHub repository ceremcem/modbus_lib, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ceremcem/modbus_lib
- Repository: https://github.com/ceremcem/modbus_lib

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Hardware Layer
    subgraph "Hardware Layer"
        UI["UART Hardware Interface"]:::hardware
        ISR1["USART2_dataHandler (ISR)"]:::handler
        ISR2["USART2_idleHandler (ISR)"]:::handler
    end

    %% Application Layer
    subgraph "Application Layer"
        App["Application Main (modbus_lib_init, ModbusConfig_t)"]:::app
        User["User-Implemented Callbacks (Read/Write)"]:::user
    end

    %% Modbus RTU Library Layer
    subgraph "Modbus RTU Library"
        Modbus_Core["modbus_lib.c: Protocol Engine"]:::core
        CRC_Module["modbus_crc.c: CRC Functions"]:::crc
        DataTypes["modbus_lib_types.h: Data Types"]:::types
        Platform_Port["port.h: Platform Abstraction"]:::port
        Transport["modbus_lib_transport_write: Response Transmission"]:::transport
    end

    %% Data Flow Connections
    UI -->|"data_reception"| ISR1
    UI -->|"idle_detection"| ISR2
    ISR1 -->|"append_data"| Modbus_Core
    ISR2 -->|"end_of_telegram"| Modbus_Core
    App -->|"initialize"| Modbus_Core
    Modbus_Core -->|"CRC_check"| CRC_Module
    Modbus_Core -->|"depends_on"| DataTypes
    Modbus_Core -->|"depends_on"| Platform_Port
    Modbus_Core -->|"process_telegram"| User
    Modbus_Core -->|"send_response"| Transport
    Transport -->|"UART_transmit"| UI

    %% Click Events for Component Mapping
    click Modbus_Core "https://github.com/ceremcem/modbus_lib/blob/master/modbus_lib.c"
    click CRC_Module "https://github.com/ceremcem/modbus_lib/blob/master/modbus_crc.c"
    click DataTypes "https://github.com/ceremcem/modbus_lib/blob/master/modbus_lib_types.h"
    click Platform_Port "https://github.com/ceremcem/modbus_lib/blob/master/port.h"

    %% Style Classes
    classDef hardware fill:#f2d7d5,stroke:#a93226,stroke-width:2px;
    classDef handler fill:#fdebd0,stroke:#d35400,stroke-width:2px;
    classDef core fill:#d6eaf8,stroke:#2471a3,stroke-width:2px;
    classDef crc fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef types fill:#fcf3cf,stroke:#f1c40f,stroke-width:2px;
    classDef port fill:#ebdef0,stroke:#8e44ad,stroke-width:2px;
    classDef transport fill:#d1f2eb,stroke:#16a085,stroke-width:2px;
    classDef app fill:#fadbd8,stroke:#e74c3c,stroke-width:2px;
    classDef user fill:#d6eaf8,stroke:#2980b9,stroke-width:2px;
```
