use crate::{
client_builder::ClientBuilder, client_session::ClientSession, rtu::ModbusRtu, NoDelay,
};
use embedded_hal::digital::OutputPin;
pub struct Client<S, TX, D = NoDelay> {
pub(crate) rtu: ModbusRtu<S, TX>,
pub(crate) rtu_timeout_ms: Option<u32>,
pub(crate) tcp_timeout_ms: Option<u32>,
pub(crate) delay: D,
}
impl<S, TX, D> Client<S, TX, D> {
pub fn builder() -> ClientBuilder<(), (), NoDelay> {
ClientBuilder::new()
}
pub(crate) fn from_parts(
serial: S,
tx_en: TX,
delay: D,
rtu_timeout_ms: Option<u32>,
tcp_timeout_ms: Option<u32>,
) -> Self {
Self {
rtu: ModbusRtu::new(serial, tx_en),
rtu_timeout_ms,
tcp_timeout_ms,
delay,
}
}
pub fn into_inner(self) -> (S, TX, D) {
let (s, tx) = self.rtu.into_inner();
(s, tx, self.delay)
}
}
#[cfg(feature = "async")]
impl<S, TX, D> Client<S, TX, D>
where
S: embedded_io_async::Read + embedded_io_async::Write,
TX: OutputPin,
{
pub fn connect<TS>(&mut self, stream: TS) -> ClientSession<'_, S, TX, TS, D>
where
TS: embedded_io_async::Read + embedded_io_async::Write,
{
ClientSession::new(self, stream)
}
}
#[cfg(feature = "sync")]
impl<S, TX, D> Client<S, TX, D>
where
S: embedded_io::Read + embedded_io::Write,
TX: OutputPin,
{
pub fn connect<TS>(&mut self, stream: TS) -> ClientSession<'_, S, TX, TS, D>
where
TS: embedded_io::Read + embedded_io::Write,
{
ClientSession::new(self, stream)
}
}