From 5aa9c86161c72e7d1d866e0f08e0e9957496cc49 Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Mon, 9 Sep 2024 21:23:41 +0200 Subject: [PATCH] test --- examples/rtu-client.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/examples/rtu-client.rs b/examples/rtu-client.rs index bf896542..16daab2e 100644 --- a/examples/rtu-client.rs +++ b/examples/rtu-client.rs @@ -3,6 +3,8 @@ //! Asynchronous RTU client example +use std::time::Duration; + #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { use tokio_serial::SerialStream; @@ -12,16 +14,30 @@ async fn main() -> Result<(), Box> { let tty_path = "/dev/ttyUSB0"; let slave = Slave(0x17); - let builder = tokio_serial::new(tty_path, 19200); - let port = SerialStream::open(&builder).unwrap(); - - let mut ctx = rtu::attach_slave(port, slave); - println!("Reading a sensor value"); - let rsp = ctx.read_holding_registers(0x082B, 2).await??; - println!("Sensor value is: {rsp:?}"); - - println!("Disconnecting"); - ctx.disconnect().await?; + for _ in 1..10 { + let builder = tokio_serial::new(tty_path, 19200); + let port = SerialStream::open(&builder).unwrap(); + + let mut ctx = rtu::attach_slave(port, slave); + println!("Reading a sensor value"); + match tokio::time::timeout( + Duration::from_millis(1000), + ctx.read_holding_registers(0x082B, 2), + ) + .await + { + Ok(res) => { + let rsp = res??; + println!("Sensor value is: {rsp:?}"); + } + Err(_) => { + println!("Timed out"); + } + } + + println!("Disconnecting"); + //ctx.disconnect().await?; + } Ok(()) }