SafeClient

Struct SafeClient 

Source
pub struct SafeClient { /* private fields */ }
Available on crate feature 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

Source

pub fn new(ctx: Context) -> Self

Creates a new SafeClient instance.

§Arguments
  • ctx: A synchronous Modbus client context, already connected.
Source

pub fn from_shared(ctx: Arc<Mutex<Context>>) -> Self

Creates a new SafeClient from an existing Arc<Mutex<Context>>.

This allows multiple SafeClient instances to share the exact same underlying connection context.

Source

pub fn clone_shared(&self) -> Arc<Mutex<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.

Source

pub fn read_ports(&self) -> Result<PortStates, Error>

Reads the current status (Open/Close) of all ports.

Source

pub fn set_port_open(&self, port: Port) -> Result<(), Error>

Sets the specified port to the Open state.

Source

pub fn set_all_open(&self) -> Result<(), Error>

Sets all ports to the Open state.

Source

pub fn set_port_close(&self, port: Port) -> Result<(), Error>

Sets the specified port to the Close state.

Source

pub fn set_all_close(&self) -> Result<(), Error>

Sets all ports to the Close state.

Source

pub fn set_port_toggle(&self, port: Port) -> Result<(), Error>

Toggles the current state of the specified port.

Source

pub fn set_port_latch(&self, port: Port) -> Result<(), Error>

Latches the specified port (opens it and closes all others).

Source

pub fn set_port_momentary(&self, port: Port) -> Result<(), Error>

Activates the specified port momentarily.

Source

pub fn set_port_delay(&self, port: Port, delay: u8) -> Result<(), Error>

Activates the specified port with a delayed close.

Source

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.

Source

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

Source§

fn clone(&self) -> SafeClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.