# simonvetter/modbus architecture

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

- Interactive diagram: https://gitdiagram.com/simonvetter/modbus
- Repository: https://github.com/simonvetter/modbus

## Overview

##### Project type and purpose
- This is a Go library (“Go modbus stack”) implementing the Modbus protocol.
- It provides both a client API (for master devices) and a server API (for slave devices), plus a CLI tool (modbus-cli) and example programs.
- Its goal is to let Go programs communicate with Modbus devices over serial (RTU) or network (TCP, TLS, UDP) transports.

##### File structure
- Top-level files:
- client.go, server.go – core client and server entrypoints
- transport.go – transport interface
- crc.go, encoding.go, tls_utils.go, logger.go – shared utilities
- *.go and *_test.go – implementation and unit tests
- cmd/modbus-cli.go – CLI application
- examples/… – runnable examples for TCP, TLS, serial
- Transport implementations:
- tcp_transport.go, tls_utils.go, udp.go, rtu_transport.go, serial.go
- Build and dependency files: go.mod, go.sum
- No database or persistent storage – stateless protocol library.

##### README for additional insights
- Describes supported Modbus variants (RTU, TCP, TLS, UDP, tunneled RTU).
- Shows how to instantiate a client with a URL-based configuration and how to open/close connections.
- Calls out use of goburrow/serial for serial port access.
- Mentions logging via standard log.Logger.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CLI Layer
    subgraph "CLI Layer"
        CLIApp["CLI Application\n(modbus-cli)"]:::client
    end

    %% Client-Side Components
    subgraph "Client-Side" 
        Client["Client Library\n(client.go)"]:::client
        URLParser["URL Parser"]:::orange
        TransportFactory["Transport Factory"]:::orange
        Framer["Framing & Encoding\n(crc.go, encoding.go)"]:::orange
        Logger["Logging Utility\n(logger.go)"]:::external
    end

    %% Server-Side Components
    subgraph "Server-Side"
        Server["Server Library\n(server.go)"]:::server
        Handler["Function Code Handlers"]:::server
    end

    %% Transport Layer and Implementations
    subgraph "Transport Layer"
        TransportIface["Transport Interface\n(transport.go)"]:::orange
        subgraph "Implementations"
            TCP["TCP Transport\n(tcp_transport.go)"]:::orange
            TLS["TLS Utilities/Transport\n(tls_utils.go)"]:::orange
            UDP["UDP Transport\n(udp.go)"]:::orange
            RTU["RTU Transport\n(rtu_transport.go)"]:::orange
            SerialDrv["Serial Driver Wrapper\n(serial.go)"]:::external
        end
    end

    %% Examples
    subgraph "Examples"
        TCPEx["TCP Server Example\n(examples/tcp_server.go)"]:::external
        TLSClientEx["TLS Client Example\n(examples/tls_client.go)"]:::external
        TLSServerEx["TLS Server Example\n(examples/tls_server.go)"]:::external
    end

    %% External Entities
    ExtDevice["Modbus Device"]:::external

    %% Connections
    CLIApp -->|"uses"| Client
    Client -->|"parses URL"| URLParser
    URLParser -->|"selects"| TransportFactory
    TransportFactory --> TransportIface
    TransportIface --> TCP
    TransportIface --> TLS
    TransportIface --> UDP
    TransportIface --> RTU
    RTU --> SerialDrv

    Client --> Framer
    Server --> Framer
    Client --> Logger
    Server --> Logger

    Client -->|"sends frames"| TransportIface
    TransportIface -->|"wire"| ExtDevice
    ExtDevice -->|"response"| TransportIface
    TransportIface --> Framer
    Framer --> Server
    Server -->|"response frames"| TransportIface
    TransportIface --> Framer
    Framer --> Client

    TCPEx --> Server
    TLSClientEx --> Client
    TLSServerEx --> Server

    %% Click Events
    click CLIApp "https://github.com/simonvetter/modbus/blob/master/cmd/modbus-cli.go"
    click Client "https://github.com/simonvetter/modbus/blob/master/client.go"
    click Server "https://github.com/simonvetter/modbus/blob/master/server.go"
    click TransportIface "https://github.com/simonvetter/modbus/blob/master/transport.go"
    click TCP "https://github.com/simonvetter/modbus/blob/master/tcp_transport.go"
    click TLS "https://github.com/simonvetter/modbus/blob/master/tls_utils.go"
    click UDP "https://github.com/simonvetter/modbus/blob/master/udp.go"
    click RTU "https://github.com/simonvetter/modbus/blob/master/rtu_transport.go"
    click SerialDrv "https://github.com/simonvetter/modbus/blob/master/serial.go"
    click Framer "https://github.com/simonvetter/modbus/blob/master/crc.go"
    click Logger "https://github.com/simonvetter/modbus/blob/master/logger.go"
    click TCPEx "https://github.com/simonvetter/modbus/blob/master/examples/tcp_server.go"
    click TLSClientEx "https://github.com/simonvetter/modbus/blob/master/examples/tls_client.go"
    click TLSServerEx "https://github.com/simonvetter/modbus/blob/master/examples/tls_server.go"

    %% Styles
    classDef client fill:#b7e4c7,stroke:#333,stroke-width:1px
    classDef server fill:#a0c4ff,stroke:#333,stroke-width:1px
    classDef orange fill:#ffd6a5,stroke:#333,stroke-width:1px
    classDef external fill:#d3d3d3,stroke:#333,stroke-width:1px
```
