voltage_modbus/
lib.rs

1//! # Voltage Modbus - High-Performance Modbus TCP/RTU/ASCII Library
2//! 
3//! **Author:** Evan Liu <evan.liu@voltageenergy.com>  
4//! **Version:** 0.2.0  
5//! **License:** MIT
6//! 
7//! A comprehensive, high-performance Modbus TCP/RTU/ASCII implementation in pure Rust
8//! designed for industrial automation, IoT applications, and smart grid systems.
9//! 
10//! ## Features
11//! 
12//! - **🚀 High Performance**: Async/await support with Tokio for maximum throughput
13//! - **🔧 Complete Protocol Support**: Modbus TCP, RTU, and ASCII protocols
14//! - **🛡️ Memory Safe**: Pure Rust implementation with zero unsafe code
15//! - **⚡ Zero-Copy Operations**: Optimized for minimal memory allocations
16//! - **🔄 Concurrent Processing**: Multi-client server support
17//! - **📊 Built-in Monitoring**: Comprehensive statistics and metrics
18//! - **🏭 Production Ready**: Extensive testing and error handling
19//! 
20//! ## Supported Function Codes
21//! 
22//! | Code | Function | Client | Server |
23//! |------|----------|--------|--------|
24//! | 0x01 | Read Coils | ✅ | ✅ |
25//! | 0x02 | Read Discrete Inputs | ✅ | ✅ |
26//! | 0x03 | Read Holding Registers | ✅ | ✅ |
27//! | 0x04 | Read Input Registers | ✅ | ✅ |
28//! | 0x05 | Write Single Coil | ✅ | ✅ |
29//! | 0x06 | Write Single Register | ✅ | ✅ |
30//! | 0x0F | Write Multiple Coils | ✅ | ✅ |
31//! | 0x10 | Write Multiple Registers | ✅ | ✅ |
32//! 
33//! ## Quick Start
34//! 
35//! ### Client Example
36//! 
37//! ```rust,no_run
38//! use voltage_modbus::{ModbusTcpClient, ModbusClient, ModbusResult};
39//! use std::time::Duration;
40//! 
41//! #[tokio::main]
42//! async fn main() -> ModbusResult<()> {
43//!     // Connect to Modbus TCP server
44//!     let mut client = ModbusTcpClient::with_timeout("127.0.0.1:502", Duration::from_secs(5)).await?;
45//!     
46//!     // Read holding registers
47//!     let values = client.read_holding_registers(1, 0, 10).await?;
48//!     println!("Read registers: {:?}", values);
49//!     
50//!     // Write single register
51//!     client.write_single_register(1, 100, 0x1234).await?;
52//!     
53//!     client.close().await?;
54//!     Ok(())
55//! }
56//! ```
57//! 
58//! ### Server Example
59//! 
60//! ```rust,no_run
61//! use voltage_modbus::{ModbusTcpServer, ModbusTcpServerConfig, ModbusServer, ModbusRegisterBank};
62//! use std::sync::Arc;
63//! use std::time::Duration;
64//! 
65//! #[tokio::main]
66//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
67//!     // Create server configuration
68//!     let config = ModbusTcpServerConfig {
69//!         bind_address: "127.0.0.1:502".parse().unwrap(),
70//!         max_connections: 50,
71//!         request_timeout: Duration::from_secs(30),
72//!         register_bank: Some(Arc::new(ModbusRegisterBank::new())),
73//!     };
74//!     
75//!     // Start server
76//!     let mut server = ModbusTcpServer::with_config(config)?;
77//!     server.start().await?;
78//!     
79//!     // Server is now running...
80//!     Ok(())
81//! }
82//! ```
83//! 
84//! ## Architecture
85//! 
86//! ```text
87//! ┌─────────────────┐    ┌─────────────────┐
88//! │   Application   │    │   Application   │
89//! └─────────────────┘    └─────────────────┘
90//!          │                       │
91//! ┌─────────────────┐    ┌─────────────────┐
92//! │  Modbus Client  │    │  Modbus Server  │
93//! └─────────────────┘    └─────────────────┘
94//!          │                       │
95//! ┌─────────────────┐    ┌─────────────────┐
96//! │   Protocol      │    │ Register Bank   │
97//! │   (TCP/RTU)     │    │   (Storage)     │
98//! └─────────────────┘    └─────────────────┘
99//!          │                       │
100//! ┌─────────────────┐    ┌─────────────────┐
101//! │   Transport     │◄──►│   Transport     │
102//! │   (Async I/O)   │    │   (Async I/O)   │
103//! └─────────────────┘    └─────────────────┘
104//! ```
105
106/// Core error types and result handling
107/// 
108/// Author: Evan Liu <evan.liu@voltageenergy.com>
109pub mod error;
110
111/// Modbus protocol definitions and message handling
112/// 
113/// Author: Evan Liu <evan.liu@voltageenergy.com>
114pub mod protocol;
115
116/// Network transport layer for TCP and RTU communication
117/// 
118/// Author: Evan Liu <evan.liu@voltageenergy.com>
119pub mod transport;
120
121/// Modbus client implementations
122/// 
123/// Author: Evan Liu <evan.liu@voltageenergy.com>
124pub mod client;
125
126/// Modbus server implementations
127/// 
128/// Author: Evan Liu <evan.liu@voltageenergy.com>
129pub mod server;
130
131/// Thread-safe register storage for server applications
132/// 
133/// Author: Evan Liu <evan.liu@voltageenergy.com>
134pub mod register_bank;
135
136/// Utility functions and performance monitoring
137/// 
138/// Author: Evan Liu <evan.liu@voltageenergy.com>
139pub mod utils;
140
141// Re-export main types for convenience
142pub use error::{ModbusError, ModbusResult};
143pub use protocol::{ModbusRequest, ModbusResponse, ModbusFunction};
144pub use transport::{ModbusTransport, TcpTransport, RtuTransport, AsciiTransport, TransportStats};
145pub use client::{ModbusClient, ModbusTcpClient};
146pub use server::{ModbusServer, ModbusTcpServer, ModbusTcpServerConfig, ServerStats};
147pub use register_bank::{ModbusRegisterBank, RegisterBankStats};
148pub use utils::{PerformanceMetrics, OperationTimer};
149
150/// Default timeout for operations (5 seconds)
151pub const DEFAULT_TIMEOUT_MS: u64 = 5000;
152
153/// Maximum number of coils that can be read/written in a single request
154pub const MAX_COILS_PER_REQUEST: u16 = 2000;
155
156/// Maximum number of registers that can be read/written in a single request  
157pub const MAX_REGISTERS_PER_REQUEST: u16 = 125;
158
159/// Maximum Modbus TCP frame size (MBAP header + PDU)
160pub const MAX_TCP_FRAME_SIZE: usize = 260;
161
162/// Maximum Modbus RTU frame size
163pub const MAX_RTU_FRAME_SIZE: usize = 256;
164
165/// Modbus TCP default port
166pub const DEFAULT_TCP_PORT: u16 = 502;
167
168/// Library version
169pub const VERSION: &str = env!("CARGO_PKG_VERSION");
170
171/// Get library information
172pub fn info() -> String {
173    format!("Voltage Modbus v{} - High-performance Modbus TCP/RTU/ASCII library by Evan Liu", VERSION)
174}