How slowtec/tokio-modbus works
Project Type and Purpose
• The project is a library (not a standalone application) that provides both client and server implementations of the Modbus protocol over TCP and RTU.
• It is built in Rust and leverages Tokio for asynchronous operations, while also offering synchronous API variants.
• The README highlights support for different features (e.g., “rtu”, “tcp”, “rtu-server” etc.) and offers example usages in the examples folder. This indicates a clear separation of concerns and configurable behavior via Cargo features.
File Structure
• The top-level directories and files (e.g., Cargo.toml, README.md, LICENSES, .github workflows, etc.) show that it is a well-maintained open source Rust library.
• The src directory is structured into multiple key submodules:
• src/client: Contains implementations for the client APIs with separate modules for RTU and TCP, and even a dedicated “sync” subdirectory for blocking implementations.
• src/server: Contains modules for the server implementations, including RTU and TCP variants plus an “rtu_over_tcp” for that case.
• src/codec and src/frame: Handle the encoding/decoding and framing of Modbus messages respectively, which are critical for protocol compliance.
• src/service: Likely used to bridge or wrap common functionalities between client/server operations.
• Additional modules like src/slave and src/prelude offer abstraction and shared functionality throughout the library.
• The examples directory provides multiple example programs to demonstrate the usage of various API modes – highlighting the intended extensibility.
Key Components and Their Relationships
• Tokio Runtime: As the core asynchronous runtime environment supporting non-blocking operations.
• Client Module:
▪ Asynchronous Clients: Separate nodes for TCP and RTU clients.
▪ Synchronous Clients: Segregated under “sync” for blocking operations.
• Server Module:
▪ Asynchronous Servers: Including variations for TCP, RTU, and RTU-over-TCP.
• Protocol Encoding Layers:
▪ Codec Module: Handles low-level encoding/decoding of Modbus protocol messages.
▪ Frame Module: Deals with framing and possibly message boundaries.
• Service Module: This can be shown as an intermediary that ties client/server operations together or abstracts communication details.
• Interactions:
• The client components send and receive Modbus requests/responses via underlying layers (codec and frame).
• The server components accept incoming communication, process requests, and provide responses, also utilizing codec and frame modules.
Architectural Patterns/Design Principles
• Separation of Concerns: Each module (client, server, codec, frame, service) is designed for a specific aspect of the protocol handling.
• Modularity and Extensibility: The use of Cargo features indicates that functionally distinct components (asynchronous vs synchronous, TCP vs RTU) can be enabled or disabled as needed.
• Clear API Boundaries: The library provides clear divisions between client and server roles, abstracting the differences between blocking and non-blocking operations.
Elements
• That both client and server are built upon the Tokio event loop.
• Network interfaces (TCP/RTU connections)
• API implementations (async vs sync)
• Utility layers (codec, frame, service).
• Under the Client branch, show separate nodes (or submodules) for asynchronous (TCP/RTU) and synchronous modes.
• Optionally, add a separate section for “Examples” to indicate that there are sample implementations that demonstrate these interactions.