tokio_modbus/client/
tcp.rs1use std::{fmt, io, net::SocketAddr};
7
8use tokio::{
9 io::{AsyncRead, AsyncWrite},
10 net::TcpStream,
11};
12
13use super::*;
14
15pub async fn connect(socket_addr: SocketAddr) -> io::Result<Context> {
17 connect_slave(socket_addr, Slave::tcp_device()).await
18}
19
20pub async fn connect_slave(socket_addr: SocketAddr, slave: Slave) -> io::Result<Context> {
24 let transport = TcpStream::connect(socket_addr).await?;
25 let context = attach_slave(transport, slave);
26 Ok(context)
27}
28
29pub fn attach<T>(transport: T) -> Context
33where
34 T: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug + 'static,
35{
36 attach_slave(transport, Slave::tcp_device())
37}
38
39pub fn attach_slave<T>(transport: T, slave: Slave) -> Context
43where
44 T: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug + 'static,
45{
46 let client = crate::service::tcp::Client::new(transport, slave);
47 Context {
48 client: Box::new(client),
49 }
50}