Skip to main content

oms_modbus/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3#![forbid(unsafe_code)]
4#![deny(rust_2018_idioms)]
5
6//! OMS Modbus — high-performance, transport-generic Modbus library in pure Rust.
7//!
8//! Complete Master (client) and Slave (server) for TCP, RTU, and ASCII —
9//! all through a single unified API. Passive bus-level [`WireTap`] with
10//! microsecond timestamps, auto-reconnect with USB serial resilience,
11//! and spec-compliant RS-485 bus timing.
12//!
13//! # Quick start
14//!
15//! ```no_run
16//! use oms_modbus::*;
17//! use std::net::{IpAddr, Ipv4Addr, SocketAddr};
18//! use std::sync::Arc;
19//! use std::time::Duration;
20//!
21//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
22//! let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);
23//! let server = tcp::TcpServer::bind(addr).await?;
24//! let bind_addr = server.local_addr()?;
25//! let store = Arc::new(SlaveStore::with_holding_registers(&[(0, 1234)]));
26//! tokio::spawn(async move { server.serve_forever(store).await.ok(); });
27//!
28//! let client = tcp::TcpClient::connect_with_timeout(
29//!     bind_addr, Duration::from_secs(3),
30//! ).await?;
31//! let regs = client.read_holding_registers(1, 0, 1).await?;
32//! # Ok(())
33//! # }
34//! ```
35//!
36//! # Key types
37//!
38//! | Type | Description |
39//! |------|-------------|
40//! | [`ModbusClient`] | Master (client) trait — TCP, RTU, ASCII share this API |
41//! | [`SlaveStore`] | Default Slave (server) — in-memory register/coil store |
42//! | [`Service`] | Trait for custom server backends |
43//! | [`ClientOptions`] | Builder: timeout, capture, reconnect, bus timing |
44//! | [`BusCapture`] | Recording + statistics — implements [`WireTap`] |
45//! | [`WireTap`] | Passive bus observer — implement for custom capture |
46//! | [`BusTiming`] | RS-485 frame spacing — per Modbus Serial Spec |
47//! | [`ModbusError`] | Structured error — `Display`, `detail()`, `label()` |
48//!
49//! # Examples
50//!
51//! See the [examples directory](https://github.com/orangehorsetech/oms-modbus/tree/master/examples)
52//! for 14 runnable examples covering TCP, RTU, ASCII, monitoring, reconnect,
53//! custom services, and bus timing.
54
55pub mod bus_timing;
56pub mod capture;
57pub mod client;
58pub mod codec;
59pub mod error;
60pub mod frame;
61pub mod options;
62pub mod server;
63pub use codec::calculate_crc;
64pub use codec::calculate_lrc;
65pub mod intercept;
66pub mod monitor;
67pub mod reconnect;
68pub mod slave_store;
69pub mod transport;
70pub mod wire_tap;
71pub use reconnect::ReconnectConfig;
72
73pub use bus_timing::BusTiming;
74pub use capture::BusCapture;
75pub use client::ModbusClient;
76pub use error::ModbusError;
77pub use frame::{Exception, ExceptionResponse, FunctionCode, Request, Response};
78pub use intercept::{format_timestamp, PacketCapture, PacketData, PacketRecord, TrafficStats};
79pub use options::ClientOptions;
80pub use server::Service;
81pub use server::{HookedService, ServerHook};
82pub use slave_store::SlaveStore;
83pub use wire_tap::{now_micros, WireTap};
84
85pub mod rtu {
86    pub use crate::transport::rtu::{with_options, RtuClient, RtuServer};
87}
88pub mod ascii {
89    pub use crate::transport::ascii::{with_options, AsciiClient, AsciiServer};
90}
91pub mod tcp {
92    pub use crate::transport::tcp::{
93        with_options, LengthMode, TcpClient, TcpConfig, TcpServer, TidMode,
94    };
95}