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

docs

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