pub struct SafeClient { /* private fields */ }safe-client-sync and (crate features tokio-rtu-sync or tokio-tcp-sync) only.Expand description
A thread-safe, synchronous client for an R413D08 relay module.
This client encapsulates a tokio_modbus::client::sync::Context within an
Arc<Mutex<...>>, allowing it to be safely shared and cloned across multiple
threads. All device operations are internally serialized,
preventing concurrent access issues.
It also provides a safer set_address method that automatically updates
the client’s internal slave ID after successfully changing the device’s
Modbus address, preventing desynchronization errors.
§Example
use r413d08_lib::{
protocol::Port,
tokio_sync_safe_client::SafeClient,
};
use std::thread;
use tokio_modbus::client::sync::tcp;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let socket_addr = "127.0.0.1:502".parse()?;
let ctx = tcp::connect(socket_addr)?;
let client = SafeClient::new(ctx);
// Clone the client to share it between threads
let client_clone = client.clone();
thread::spawn(move || {
// Use the client in another thread
client_clone.set_port_open(Port::try_from(1).unwrap()).unwrap();
});
// Use the client in the main thread
let status = client.read_ports()?;
println!("Port status: {}", status);
Ok(())
}Implementations§
Source§impl SafeClient
impl SafeClient
Sourcepub fn new(ctx: Context) -> Self
pub fn new(ctx: Context) -> Self
Creates a new SafeClient instance.
§Arguments
ctx: A synchronous Modbus client context, already connected.
Creates a new SafeClient from an existing Arc<Mutex<Context>>.
This allows multiple SafeClient instances to share the exact same
underlying connection context.
Clones and returns the underlying Arc<Mutex<Context>>.
This allows the shared context to be used by other parts of an application that may need direct access to the Modbus context.
Sourcepub fn read_ports(&self) -> Result<PortStates, Error>
pub fn read_ports(&self) -> Result<PortStates, Error>
Reads the current status (Open/Close) of all ports.
Sourcepub fn set_port_open(&self, port: Port) -> Result<(), Error>
pub fn set_port_open(&self, port: Port) -> Result<(), Error>
Sets the specified port to the Open state.
Sourcepub fn set_all_open(&self) -> Result<(), Error>
pub fn set_all_open(&self) -> Result<(), Error>
Sets all ports to the Open state.
Sourcepub fn set_port_close(&self, port: Port) -> Result<(), Error>
pub fn set_port_close(&self, port: Port) -> Result<(), Error>
Sets the specified port to the Close state.
Sourcepub fn set_all_close(&self) -> Result<(), Error>
pub fn set_all_close(&self) -> Result<(), Error>
Sets all ports to the Close state.
Sourcepub fn set_port_toggle(&self, port: Port) -> Result<(), Error>
pub fn set_port_toggle(&self, port: Port) -> Result<(), Error>
Toggles the current state of the specified port.
Sourcepub fn set_port_latch(&self, port: Port) -> Result<(), Error>
pub fn set_port_latch(&self, port: Port) -> Result<(), Error>
Latches the specified port (opens it and closes all others).
Sourcepub fn set_port_momentary(&self, port: Port) -> Result<(), Error>
pub fn set_port_momentary(&self, port: Port) -> Result<(), Error>
Activates the specified port momentarily.
Sourcepub fn set_port_delay(&self, port: Port, delay: u8) -> Result<(), Error>
pub fn set_port_delay(&self, port: Port, delay: u8) -> Result<(), Error>
Activates the specified port with a delayed close.
Sourcepub fn read_address(&self) -> Result<Address, Error>
pub fn read_address(&self) -> Result<Address, Error>
Reads the configured Modbus device address.
It’s recommended to use the broadcast address for this operation, ensuring only one device is on the bus.
Sourcepub fn set_address(&self, address: Address) -> Result<(), Error>
pub fn set_address(&self, address: Address) -> Result<(), Error>
Sets a new Modbus device address.
This method is safer than the stateless equivalent. Upon successfully changing the device’s address, it automatically updates the client’s internal slave ID to match. This keeps the client synchronized with the device state, preventing subsequent communication errors.
Trait Implementations§
Source§impl Clone for SafeClient
impl Clone for SafeClient
Source§fn clone(&self) -> SafeClient
fn clone(&self) -> SafeClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more