# cwalter-at/freemodbus architecture

> Architecture diagram of the GitHub repository cwalter-at/freemodbus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cwalter-at/freemodbus
- Repository: https://github.com/cwalter-at/freemodbus

## Overview

FreeModbus is an embedded Modbus application-protocol library supporting ASCII, RTU, and TCP transports. An application initializes and polls the stack; the selected transport receives and validates a frame, the core dispatches its function code, and handlers use application callbacks for register data before a response is sent. The repository also includes multiple platform-specific port implementations under demo/, but the sampled evidence establishes only the generic port interface. Relationships beyond explicit excerpts or indexed references are omitted.

Main entry points: [`modbus/mb.c`](https://github.com/cwalter-at/freemodbus/blob/master/modbus/mb.c) (Stack API and Dispatch), [`modbus/rtu/mbrtu.c`](https://github.com/cwalter-at/freemodbus/blob/master/modbus/rtu/mbrtu.c) (RTU Framing), [`modbus/ascii/mbascii.c`](https://github.com/cwalter-at/freemodbus/blob/master/modbus/ascii/mbascii.c) (ASCII Framing), [`modbus/include/mbport.h`](https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbport.h) (Port API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Protocol Core"]
  node_core_api["Stack API and Dispatch<br/>[mb.c]"]
  node_protocol_defs["Protocol Definitions<br/>[mbproto.h]"]
  node_frame_defs["Frame Interface<br/>[mbframe.h]"]
  node_config["Feature Configuration<br/>[mbconfig.h]"]
end

subgraph group_transport["Frame Transports"]
  node_ascii["ASCII Framing<br/>[mbascii.c]"]
  node_rtu["RTU Framing<br/>[mbrtu.c]"]
  node_crc["RTU CRC<br/>[mbcrc.c]"]
  node_tcp["TCP Framing<br/>[mbtcp.c]"]
end

subgraph group_functions["Function Handlers"]
  node_coil_handlers["Coil Functions<br/>[mbfunccoils.c]"]
  node_holding_handlers["Holding Registers<br/>[mbfuncholding.c]"]
  node_file_handlers["File Functions<br/>[mbfuncfile.c]"]
  node_other_handlers["Other Functions"]
end

subgraph group_porting["Port Interface"]
  node_port_interface["Port API<br/>[mbport.h]"]
end

node_application(("Modbus Application"))
node_register_app(("Register Callbacks"))

node_application -->|"initializes and polls"| node_core_api
node_core_api -->|"selects framing"| node_ascii
node_core_api -->|"selects framing"| node_rtu
node_core_api -->|"selects framing"| node_tcp
node_core_api -->|"dispatches function"| node_coil_handlers
node_core_api -->|"dispatches function"| node_holding_handlers
node_core_api -->|"dispatches function"| node_file_handlers
node_core_api -->|"dispatches function"| node_other_handlers
node_ascii -->|"reuses receive buffer"| node_rtu
node_rtu -->|"checks frame CRC"| node_crc
node_ascii -->|"uses port services"| node_port_interface
node_rtu -->|"uses port services"| node_port_interface
node_tcp -->|"uses TCP services"| node_port_interface
node_holding_handlers -->|"reads or writes registers"| node_register_app
node_core_api -->|"uses function codes"| node_protocol_defs
node_core_api -->|"uses frame callbacks"| node_frame_defs
node_core_api -->|"configures enabled features"| node_config

click node_core_api "https://github.com/cwalter-at/freemodbus/blob/master/modbus/mb.c"
click node_protocol_defs "https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbproto.h"
click node_frame_defs "https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbframe.h"
click node_config "https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbconfig.h"
click node_ascii "https://github.com/cwalter-at/freemodbus/blob/master/modbus/ascii/mbascii.c"
click node_rtu "https://github.com/cwalter-at/freemodbus/blob/master/modbus/rtu/mbrtu.c"
click node_crc "https://github.com/cwalter-at/freemodbus/blob/master/modbus/rtu/mbcrc.c"
click node_tcp "https://github.com/cwalter-at/freemodbus/blob/master/modbus/tcp/mbtcp.c"
click node_coil_handlers "https://github.com/cwalter-at/freemodbus/blob/master/modbus/functions/mbfunccoils.c"
click node_holding_handlers "https://github.com/cwalter-at/freemodbus/blob/master/modbus/functions/mbfuncholding.c"
click node_file_handlers "https://github.com/cwalter-at/freemodbus/blob/master/modbus/functions/mbfuncfile.c"
click node_other_handlers "https://github.com/cwalter-at/freemodbus/tree/master/modbus/functions"
click node_port_interface "https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbport.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_core_api,node_protocol_defs,node_frame_defs,node_config toneBlue
class node_ascii,node_rtu,node_crc,node_tcp toneAmber
class node_coil_handlers,node_holding_handlers,node_file_handlers,node_other_handlers toneMint
class node_port_interface toneRose
class node_application,node_register_app toneIndigo
```

## Components

### Protocol Core

- [Stack API and Dispatch](https://github.com/cwalter-at/freemodbus/blob/master/modbus/mb.c): `modbus/mb.c`
- [Protocol Definitions](https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbproto.h): `modbus/include/mbproto.h`
- [Frame Interface](https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbframe.h): `modbus/include/mbframe.h`
- [Feature Configuration](https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbconfig.h): `modbus/include/mbconfig.h`

### Frame Transports

- [ASCII Framing](https://github.com/cwalter-at/freemodbus/blob/master/modbus/ascii/mbascii.c): `modbus/ascii/mbascii.c`
- [RTU Framing](https://github.com/cwalter-at/freemodbus/blob/master/modbus/rtu/mbrtu.c): `modbus/rtu/mbrtu.c`
- [RTU CRC](https://github.com/cwalter-at/freemodbus/blob/master/modbus/rtu/mbcrc.c): `modbus/rtu/mbcrc.c`
- [TCP Framing](https://github.com/cwalter-at/freemodbus/blob/master/modbus/tcp/mbtcp.c): `modbus/tcp/mbtcp.c`

### Function Handlers

- [Coil Functions](https://github.com/cwalter-at/freemodbus/blob/master/modbus/functions/mbfunccoils.c): `modbus/functions/mbfunccoils.c`
- [Holding Registers](https://github.com/cwalter-at/freemodbus/blob/master/modbus/functions/mbfuncholding.c): `modbus/functions/mbfuncholding.c`
- [File Functions](https://github.com/cwalter-at/freemodbus/blob/master/modbus/functions/mbfuncfile.c): `modbus/functions/mbfuncfile.c`
- [Other Functions](https://github.com/cwalter-at/freemodbus/tree/master/modbus/functions): `modbus/functions`

### Port Interface

- [Port API](https://github.com/cwalter-at/freemodbus/blob/master/modbus/include/mbport.h): `modbus/include/mbport.h`

### Other components

- **Modbus Application**
- **Register Callbacks**

## Connections

- Modbus Application → Stack API and Dispatch: initializes and polls
- Stack API and Dispatch → ASCII Framing: selects framing (evidence: `modbus/mb.c`)
- Stack API and Dispatch → RTU Framing: selects framing (evidence: `modbus/mb.c`)
- Stack API and Dispatch → TCP Framing: selects framing (evidence: `modbus/mb.c`)
- Stack API and Dispatch → Coil Functions: dispatches function (evidence: `modbus/mb.c`)
- Stack API and Dispatch → Holding Registers: dispatches function (evidence: `modbus/mb.c`)
- Stack API and Dispatch → File Functions: dispatches function (evidence: `modbus/mb.c`)
- Stack API and Dispatch → Other Functions: dispatches function (evidence: `modbus/mb.c`)
- ASCII Framing → RTU Framing: reuses receive buffer (evidence: `modbus/ascii/mbascii.c`)
- RTU Framing → RTU CRC: checks frame CRC (evidence: `modbus/rtu/mbrtu.c`)
- ASCII Framing → Port API: uses port services (evidence: `modbus/ascii/mbascii.c`)
- RTU Framing → Port API: uses port services (evidence: `modbus/rtu/mbrtu.c`)
- TCP Framing → Port API: uses TCP services (evidence: `modbus/tcp/mbtcp.c`)
- Holding Registers → Register Callbacks: reads or writes registers (evidence: `modbus/functions/mbfuncholding.c`)
- Stack API and Dispatch → Protocol Definitions: uses function codes (evidence: `modbus/mb.c`)
- Stack API and Dispatch → Frame Interface: uses frame callbacks (evidence: `modbus/mb.c`)
- Stack API and Dispatch → Feature Configuration: configures enabled features (evidence: `modbus/mb.c`)
