Voltage Modbus
High-Performance Modbus TCP/RTU/ASCII Library for Rust
Author: Evan Liu evan.liu@voltageenergy.com Version: 0.2.0 License: MIT
A comprehensive, high-performance Modbus TCP/RTU/ASCII implementation in pure Rust designed for industrial automation, IoT applications, and smart grid systems.
โจ Features
- ๐ High Performance: Async/await support with Tokio for maximum throughput
- ๐ง Complete Protocol Support: Modbus TCP, RTU, and ASCII protocols
- ๐ก๏ธ Memory Safe: Pure Rust implementation with zero unsafe code
- โก Zero-Copy Operations: Optimized for minimal memory allocations
- ๐ Concurrent Processing: Multi-client server support
- ๐ Built-in Monitoring: Comprehensive statistics and metrics
- ๐ญ Production Ready: Extensive testing and error handling
- ๐ฏ Smart Architecture: Generic client design eliminates code duplication
- ๐งฉ Modular Design: Clean separation of transport and application layers
๐ Supported Function Codes
| Code | Function | Client | Server |
|---|---|---|---|
| 0x01 | Read Coils | โ | โ |
| 0x02 | Read Discrete Inputs | โ | โ |
| 0x03 | Read Holding Registers | โ | โ |
| 0x04 | Read Input Registers | โ | โ |
| 0x05 | Write Single Coil | โ | โ |
| 0x06 | Write Single Register | โ | โ |
| 0x0F | Write Multiple Coils | โ | โ |
| 0x10 | Write Multiple Registers | โ | โ |
๐ Quick Start
Add this to your Cargo.toml:
[]
= "0.2.0"
= { = "1.0", = ["full"] }
Client Examples
TCP Client
use ;
use Duration;
async
RTU Client
use ;
use Duration;
async
Server Example
use ;
use Arc;
use Duration;
async
๐ Documentation
- API Reference - Complete API documentation
- Crates.io - Package information
- GitHub Repository - Source code and issues
๐๏ธ Architecture
Protocol Layer Insight
The library implements a key architectural insight: Modbus TCP and RTU share identical application layer messages (PDU), differing only in transport encapsulation:
TCP Frame: [MBAP Header (7 bytes)] + [PDU (Function Code + Data)]
RTU Frame: [Slave ID (1 byte)] + [PDU (Function Code + Data)] + [CRC (2 bytes)]
This enables code reuse through a generic client design:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Application Layer โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ ModbusTcpClient โ โ ModbusRtuClient โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ GenericModbusClient<T> โโ
โ โ (Shared Application Logic) โโ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Transport Layer โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ TcpTransport โ โ RtuTransport โ โ
โ โ (TCP Sockets) โ โ (Serial Ports) โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Core Modules
error- Error types and result handlingprotocol- Modbus protocol definitions and message handlingtransport- Network transport layer for TCP, RTU, and ASCII communicationclient- Generic and protocol-specific client implementationsserver- Modbus server implementations with concurrent supportregister_bank- Thread-safe register storage for server applicationsutils- Utility functions, data conversion, and performance monitoring
๐งช Examples and Testing
Run Examples
# Start the demo server
# Run TCP client demo
# Test RTU functionality
# Run performance benchmarks
# Test all function codes
Test Coverage
# Run all tests
# Run with output
# Integration tests
# Documentation tests
Test Results:
- โ 34 unit tests passed
- โ 9 integration tests passed
- โ 22 documentation tests passed
- โ All Modbus function codes tested
- โ Error handling and recovery tested
- โ Concurrent client connections tested
๐ Performance
Benchmarks
| Metric | Value |
|---|---|
| Latency | < 1ms (local) |
| Throughput | 1000+ requests/sec |
| Concurrent Connections | 50+ clients |
| Memory Usage | < 10MB (baseline) |
| CPU Usage | < 5% (idle) |
Optimization Features
- Async I/O: Non-blocking operations with Tokio
- Zero-Copy Operations: Minimal memory allocations
- Generic Architecture: Code reuse eliminates duplication
- Lock-Free Operations: Where possible
- Configurable Timeouts: Adaptive timeout management
๐ง Configuration
Advanced Client Configuration
use ;
use Duration;
// TCP with custom timeout
let mut tcp_client = with_timeout.await?;
// RTU with full configuration
let mut rtu_client = with_config?;
Server Configuration
use ;
use Arc;
let config = ModbusTcpServerConfig ;
๐ ๏ธ Development
Building from Source
Development Tools
# Check code
# Format code
# Run linter
# Generate documentation
๐ Installation
From Crates.io
From Source
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
- Clone the repository
- Install Rust (latest stable)
- Install dependencies:
cargo build - Run tests:
cargo test - Check formatting:
cargo fmt --check - Run linter:
cargo clippy
๐ Changelog
See CHANGELOG.md for detailed release notes.
Recent Updates (v0.2.0)
- โจ Generic Client Architecture: Eliminated code duplication between TCP/RTU clients
- ๐ฏ Improved API: Cleaner, more intuitive client interfaces
- ๐ง Enhanced RTU Support: Full RTU client and server implementations
- ๐ Better Testing: Comprehensive test coverage with 43 total tests
- ๐๏ธ Architectural Refinement: Clean separation of transport and application layers
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Modbus Organization for the protocol specification
- Tokio Team for the excellent async runtime
- Rust Community for the amazing ecosystem
๐ Support
- Documentation: https://docs.rs/voltage_modbus
- Package: https://crates.io/crates/voltage_modbus
- Issues: https://github.com/voltage-llc/voltage_modbus/issues
- Discussions: https://github.com/voltage-llc/voltage_modbus/discussions
- Email: evan.liu@voltageenergy.com
Built with โค๏ธ by Evan Liu for the Rust and Industrial Automation communities.