提交 d1c3dc64 authored 作者: Serhij S's avatar Serhij S

docs

上级 40f9deed
use parking_lot::MutexGuard;
use std::sync::Arc;
pub mod serial;
pub mod tcp;
pub mod serial; // Serial communications
pub mod tcp; // TCP communications
/// A versatile (TCP/serial) client
#[derive(Clone)]
pub struct Client(Arc<dyn Communicator + Send + Sync>);
impl Client {
/// Lock the client for exclusive access
pub fn lock(&self) -> MutexGuard<()> {
self.0.lock()
}
/// Reconnect the client in case of read/write problems
pub fn reconnect(&self) {
self.0.reconnect();
}
/// Write data to the client
pub fn write(&self, buf: &[u8]) -> Result<(), std::io::Error> {
self.0.write(buf)
}
/// Read data from the client
pub fn read_exact(&self, buf: &mut [u8]) -> Result<(), std::io::Error> {
self.0.read_exact(buf)
}
/// Get the protocol of the client
pub fn protocol(&self) -> Protocol {
self.0.protocol()
}
......@@ -27,7 +33,7 @@ impl Client {
pub enum Protocol {
Tcp,
Rtu,
Serial,
}
trait Communicator {
......
......@@ -12,6 +12,8 @@ use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, Instant};
/// Create a new serial client. The client will attempt to connect to the given address at the time
/// of the first request. The client will automatically reconnect if the connection is lost.
pub fn connect(path: &str, timeout: Duration, frame_delay: Duration) -> Result<Client> {
Ok(Client(Serial::create(path, timeout, frame_delay)?))
}
......@@ -184,7 +186,7 @@ impl Communicator for Serial {
.map_err(Into::into)
}
fn protocol(&self) -> Protocol {
Protocol::Rtu
Protocol::Serial
}
}
......
......@@ -9,6 +9,8 @@ use std::net::{SocketAddr, ToSocketAddrs};
use std::sync::Arc;
use std::time::Duration;
/// Create a new TCP client. The client will attempt to connect to the given address at the time of
/// the first request. The client will automatically reconnect if the connection is lost.
pub fn connect<A: ToSocketAddrs + fmt::Debug>(addr: A, timeout: Duration) -> Result<Client, Error> {
Ok(Client(Tcp::create(addr, timeout)?))
}
......
......@@ -316,6 +316,10 @@ where
{
/// creates a new EAPI connector instance with the name, automatically formatted as
/// `fieldbus.HOSTNAME.program`
///
/// # Panics
///
/// Will panic if failed to get the hostname
pub fn new_program(config: EAPIConfig<D, V>) -> Self {
Self::new(
format!(
......
......@@ -44,7 +44,7 @@ impl From<Protocol> for ModbusProto {
fn from(value: Protocol) -> Self {
match value {
Protocol::Tcp => ModbusProto::TcpUdp,
Protocol::Rtu => ModbusProto::Rtu,
Protocol::Serial => ModbusProto::Rtu,
}
}
}
......
......@@ -83,7 +83,7 @@ impl<const C: usize, const D: usize, const I: usize, const H: usize> ModbusServe
) -> Result<Self> {
let server = match protocol {
Protocol::Tcp => Server::Tcp(TcpListener::bind(path)?),
Protocol::Rtu => Server::Serial(comm::serial::open(&path.parse()?, timeout)?),
Protocol::Serial => Server::Serial(comm::serial::open(&path.parse()?, timeout)?),
};
Ok(Self {
storage: <_>::default(),
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论