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>
impl<S, TX, D> Bridge<S, TX, D>
Sourcepub fn builder() -> BridgeBuilder<(), (), NoDelay>
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();Sourcepub fn into_inner(self) -> (S, TX, D)
pub fn into_inner(self) -> (S, TX, D)
Source§impl<S, TX, D> Bridge<S, TX, D>
impl<S, TX, D> Bridge<S, TX, D>
Sourcepub fn accept<TS>(&mut self, stream: TS) -> Connection<'_, S, TX, TS, D>
pub fn accept<TS>(&mut self, stream: TS) -> Connection<'_, S, TX, TS, D>
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>
impl<S, TX, D> RefUnwindSafe for Bridge<S, TX, D>
impl<S, TX, D> Send for Bridge<S, TX, D>
impl<S, TX, D> Sync for Bridge<S, TX, D>
impl<S, TX, D> Unpin for Bridge<S, TX, D>
impl<S, TX, D> UnsafeUnpin for Bridge<S, TX, D>
impl<S, TX, D> UnwindSafe for Bridge<S, TX, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more