From d39ebd4563d0b2b50b53ac033b834ccdf763b30d Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Thu, 17 Oct 2024 14:17:35 +0200 Subject: [PATCH] rtu-server: Add instructions to example to make it work (#295) --- examples/rtu-server.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/rtu-server.rs b/examples/rtu-server.rs index cc2a6ed..00b0f98 100644 --- a/examples/rtu-server.rs +++ b/examples/rtu-server.rs @@ -32,10 +32,14 @@ impl tokio_modbus::server::Service for Service { #[tokio::main] async fn main() -> Result<(), Box> { - let builder = tokio_serial::new("/dev/ttyUSB0", 19200); - let server_serial = tokio_serial::SerialStream::open(&builder).unwrap(); + println!("Run the following command and then copy&paste the /dev/pts/? device addresses into the code in this example:"); + println!("socat -dd pty,raw,echo=0 pty,raw,echo=0"); - println!("Starting up server..."); + println!("Connecting server"); + let server_builder = tokio_serial::new("/dev/pts/6", 19200); + let server_serial = tokio_serial::SerialStream::open(&server_builder).unwrap(); + + println!("Starting up server"); let _server = thread::spawn(move || { let rt = tokio::runtime::Runtime::new().unwrap(); let server = Server::new(server_serial); @@ -50,10 +54,12 @@ async fn main() -> Result<(), Box> { // Give the server some time for stating up thread::sleep(Duration::from_secs(1)); - println!("CLIENT: Connecting client..."); - let client_serial = tokio_serial::SerialStream::open(&builder).unwrap(); + println!("Connecting client"); + let client_builder = tokio_serial::new("/dev/pts/7", 19200); + let client_serial = tokio_serial::SerialStream::open(&client_builder).unwrap(); let mut ctx = rtu::attach(client_serial); - println!("CLIENT: Reading input registers..."); + + println!("CLIENT: Reading input registers"); let rsp = ctx.read_input_registers(0x00, 7).await?; println!("CLIENT: The result is '{rsp:#x?}'"); assert_eq!(rsp.unwrap(), vec![0x0, 0x0, 0x77, 0x0, 0x0, 0x0, 0x0]); @@ -67,5 +73,6 @@ async fn main() -> Result<(), Box> { assert!(matches!(response, Err(ExceptionCode::IllegalDataAddress))); println!("CLIENT: Done."); + Ok(()) }