# stephane/libmodbus architecture

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

- Interactive diagram: https://gitdiagram.com/stephane/libmodbus
- Repository: https://github.com/stephane/libmodbus

## Overview

##### Main Components
a. Libmodbus Core Library:
- This is the heart of the project, containing generic Modbus functionality found in files such as src/modbus.c and src/modbus.h.
b. Protocol-Specific Modules:
- RTU Module: Found in files like src/modbus-rtu.c and src/modbus-rtu.h, handling serial port communication.
- TCP Module: Found in files like src/modbus-tcp.c and src/modbus-tcp.h, handling Ethernet-based communication.
- Win32 Specific Code: Located in src/win32, which provides build and configuration specifics for Windows environments.
c. Testing Suite:
- Tests directory contains various test applications (e.g., unit-test-client.c and unit-test-server.c) to verify both TCP and RTU interactions.
d. Documentation and Build Infrastructure:
- Build scripts and configuration files (configure.ac, autogen.sh, Makefile.am) that facilitate cross-platform compiling.
e. External Integration Points:
- The library exposes a pkg-config file (libmodbus.pc.in) helping users integrate it into their projects.
- It interacts with external systems through Modbus protocol implementations.

##### Relationships and Interactions
- The core API acts as an abstraction layer that both protocol-specific modules (RTU/TCP) implement upper-level functions.
- Client applications interact with the libmodbus API to send/receive messages by using either the RTU or TCP module depending on the connection.
- The test suite simulates modbus server/client interactions and verifies proper functioning of the underlying API.
- The build and configuration modules ensure the library can be properly compiled across various platforms (Linux, Mac OS X, Windows, RTOS like Embox).

##### Architectural Patterns and Design Principles
- Separation of concerns: The library clearly separates protocol-specific functionality from core modbus handling.
- Modularity: Both RTU and TCP implementations are encapsulated in their own source files, and the cross-platform support (e.g., win32 directory) is isolated.
- Testability: A dedicated tests directory demonstrates unit tests and integration tests, echoing the importance of continuous testing.
- Portability/Cross-Platform: Use of autotools and specific build guides (Windows, OS X, Embox) indicates a design aimed at portability.

##### Relevant Technologies
- C Language for core implementation.
- Autotools (automake, autoconf, libtool) for build system.
- Testing scripts and unit tests for validation.
- MkDocs (with the mkdocs.yml file) for serving up local documentation.
- External dependencies like pkg-config for integration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["External Devices/Clients"]:::external

    B["Libmodbus Core API"]:::core

    A -->|"calls"| B

    subgraph "Protocol Modules"
        C["RTU Module"]:::protocol
        D["TCP Module"]:::protocol
        E["Win32 Integration"]:::win32
    end

    B -->|"used by"| C
    B -->|"used by"| D
    B -->|"integrates"| E

    I["Serial Ports"]:::external
    J["Ethernet Interfaces"]:::external
    C -->|"handles"| I
    D -->|"handles"| J

    subgraph "Supporting Infrastructure"
        F["Testing Suite"]:::test
        G["Documentation & Assets"]:::docs
        H["Build & Configuration Infrastructure"]:::build
    end

    F ---|"validates"| B
    G ---|"documents"| B
    H ---|"builds"| B

    %% Click Events for Component Mapping

    click B "https://github.com/stephane/libmodbus/blob/master/src/modbus.c"
    click B "https://github.com/stephane/libmodbus/blob/master/src/modbus.h"

    click C "https://github.com/stephane/libmodbus/blob/master/src/modbus-rtu.c"
    click C "https://github.com/stephane/libmodbus/blob/master/src/modbus-rtu.h"
    click C "https://github.com/stephane/libmodbus/blob/master/src/modbus-rtu-private.h"

    click D "https://github.com/stephane/libmodbus/blob/master/src/modbus-tcp.c"
    click D "https://github.com/stephane/libmodbus/blob/master/src/modbus-tcp.h"
    click D "https://github.com/stephane/libmodbus/blob/master/src/modbus-tcp-private.h"

    click E "https://github.com/stephane/libmodbus/tree/master/src/win32"

    click F "https://github.com/stephane/libmodbus/tree/master/tests"

    click G "https://github.com/stephane/libmodbus/tree/master/docs"

    click H "https://github.com/stephane/libmodbus/blob/master/autogen.sh"
    click H "https://github.com/stephane/libmodbus/blob/master/configure.ac"
    click H "https://github.com/stephane/libmodbus/blob/master/Makefile.am"
    click H "https://github.com/stephane/libmodbus/blob/master/libmodbus.pc.in"

    %% Styles
    classDef external fill:#F9E79F,stroke:#6E2C00,stroke-width:2px;
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef protocol fill:#A9DFBF,stroke:#145A32,stroke-width:2px;
    classDef win32 fill:#F5B7B1,stroke:#78281F,stroke-width:2px;
    classDef test fill:#D6EAF8,stroke:#2874A6,stroke-width:2px;
    classDef docs fill:#FCF3CF,stroke:#7D6608,stroke-width:2px;
    classDef build fill:#FADBD8,stroke:#922B21,stroke-width:2px;
```
