Skip to main content

Bridge

Struct Bridge 

Source
pub struct Bridge<S, TX, D = NoDelay> { /* private fields */ }
Expand description

Modbus RTU/TCP bridge.

Owns the serial port (S) and RS-485 TX-enable pin (TX). TCP connections are supplied one at a time via accept. Only one Connection can be active at a time — the bridge is mutably borrowed for the connection’s lifetime.

The optional third parameter D is a delay provider for I/O timeouts. It defaults to NoDelay; configure it via BridgeBuilder::delay.

§Examples

use modbus_bridge::{Bridge, BridgeError, BridgeEvent};

let mut bridge = Bridge::builder()
    .rtu(uart, tx_en_pin)
    .build();

loop {
    let socket = tcp_stack.listen(502).await.unwrap();
    let mut conn = bridge.accept(socket);
    loop {
        match conn.next().await {
            Ok(BridgeEvent::Transaction(t)) => log::info!("modbus: {t}"),
            Ok(BridgeEvent::Warning(w))     => log::warn!("modbus: {w}"),
            Err(BridgeError::TcpClosed)     => break,
            Err(e)                          => { log::error!("{e}"); break; }
        }
    }
    conn.into_stream().close();
}

Implementations§

Source§

impl<S, TX, D> Bridge<S, TX, D>

Source

pub fn builder() -> BridgeBuilder<(), (), NoDelay>

Returns a BridgeBuilder for constructing a Bridge.

§Examples
use modbus_bridge::Bridge;

let bridge = Bridge::builder()
    .rtu(uart, tx_en)
    .build();
Source

pub fn into_inner(self) -> (S, TX, D)

Consumes the bridge and returns the inner serial port, TX-enable pin, and delay provider.

§Examples
let (uart, tx_en, _delay) = bridge.into_inner();
Source§

impl<S, TX, D> Bridge<S, TX, D>
where S: Read + Write, TX: OutputPin,

Source

pub fn accept<TS>(&mut self, stream: TS) -> Connection<'_, S, TX, TS, D>
where TS: Read + Write,

Creates a Connection for an incoming TCP client.

Takes ownership of stream and mutably borrows the bridge for the lifetime of the returned Connection.

§Examples
let mut conn = bridge.accept(socket);
loop {
    match conn.next().await {
        Ok(event) => { /* handle event */ }
        Err(_)    => break,
    }
}
let socket = conn.into_stream();
socket.close();

Auto Trait Implementations§

§

impl<S, TX, D> Freeze for Bridge<S, TX, D>
where D: Freeze, S: Freeze, TX: Freeze,

§

impl<S, TX, D> RefUnwindSafe for Bridge<S, TX, D>

§

impl<S, TX, D> Send for Bridge<S, TX, D>
where D: Send, S: Send, TX: Send,

§

impl<S, TX, D> Sync for Bridge<S, TX, D>
where D: Sync, S: Sync, TX: Sync,

§

impl<S, TX, D> Unpin for Bridge<S, TX, D>
where D: Unpin, S: Unpin, TX: Unpin,

§

impl<S, TX, D> UnsafeUnpin for Bridge<S, TX, D>

§

impl<S, TX, D> UnwindSafe for Bridge<S, TX, D>
where D: UnwindSafe, S: UnwindSafe, TX: UnwindSafe,

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> 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, 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.