r413d08_lib/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! A library for controlling the R413D08 8-channel relay module via Modbus.
3//!
4//! This crate provides two main ways to interact with the R413D08 relay module:
5//!
6//! 1. **High-Level, Safe Clients**: Stateful, thread-safe clients that are easy
7//! to share and use in concurrent applications. This is the recommended
8//! approach for most users. See [`tokio_sync_safe_client::SafeClient`] (blocking)
9//! and [`tokio_async_safe_client::SafeClient`] (`async`).
10//!
11//! 2. **Low-Level, Stateless Functions**: A set of stateless functions that
12//! directly map to the device's Modbus commands. This API offers maximum
13//! flexibility but requires manual management of the Modbus context. See
14//! the [`tokio_sync`] and [`tokio_async`] modules.
15//!
16//! ## Features
17//!
18//! - **Protocol Implementation**: Complete implementation of the R413D08 Modbus protocol.
19//! - **Stateful, Thread-Safe Clients**: For easy and safe concurrent use.
20//! - **Stateless, Low-Level Functions**: For maximum flexibility and control.
21//! - **Synchronous and Asynchronous APIs**: Both blocking and `async/await` APIs are available.
22//! - **Strongly-Typed API**: Utilizes Rust's type system for protocol correctness
23//! (e.g., `Port`, `Address`, `PortState`).
24//!
25//! ## Quick Start
26//!
27//! This example shows how to use the recommended high-level, synchronous `SafeClient`.
28//!
29//! ```no_run
30//! use r413d08_lib::{
31//! protocol::{Address, Port},
32//! tokio_sync_safe_client::SafeClient,
33//! };
34//! use tokio_modbus::client::sync::tcp;
35//! use tokio_modbus::Slave;
36//!
37//! fn main() -> Result<(), Box<dyn std::error::Error>> {
38//! // Connect to the device and create a stateful, safe client
39//! let socket_addr = "192.168.1.100:502".parse()?;
40//! let ctx = tcp::connect_slave(socket_addr, Slave(*Address::default()))?;
41//! let client = SafeClient::new(ctx);
42//!
43//! // Use the client to interact with the device
44//! client.set_port_open(Port::try_from(0)?)?;
45//! let status = client.read_ports()?;
46//!
47//! println!("Successfully turned on relay 0. Current status: {}", status);
48//!
49//! Ok(())
50//! }
51//! ```
52//!
53//! For more details, see the documentation for the specific client you wish to use.
54
55pub mod protocol;
56
57#[cfg(any(
58 feature = "tokio-rtu-sync",
59 feature = "tokio-tcp-sync",
60 feature = "tokio-rtu",
61 feature = "tokio-tcp"
62))]
63pub mod tokio_common;
64
65#[cfg_attr(
66 docsrs,
67 doc(cfg(any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")))
68)]
69#[cfg(any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync"))]
70pub mod tokio_sync;
71
72#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio-rtu", feature = "tokio-tcp"))))]
73#[cfg(any(feature = "tokio-rtu", feature = "tokio-tcp"))]
74pub mod tokio_async;
75
76#[cfg_attr(
77 docsrs,
78 doc(cfg(all(
79 feature = "safe-client-sync",
80 any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")
81 )))
82)]
83#[cfg(all(
84 feature = "safe-client-sync",
85 any(feature = "tokio-rtu-sync", feature = "tokio-tcp-sync")
86))]
87pub mod tokio_sync_safe_client;
88
89#[cfg_attr(
90 docsrs,
91 doc(cfg(all(
92 feature = "safe-client-async",
93 any(feature = "tokio-rtu", feature = "tokio-tcp")
94 )))
95)]
96#[cfg(all(
97 feature = "safe-client-async",
98 any(feature = "tokio-rtu", feature = "tokio-tcp")
99))]
100pub mod tokio_async_safe_client;