Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Sep 11, 2024
1 parent 7a53b95 commit 5aa9c86
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions examples/rtu-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

//! Asynchronous RTU client example
use std::time::Duration;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
use tokio_serial::SerialStream;
Expand All @@ -12,16 +14,30 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}

0 comments on commit 5aa9c86

Please sign in to comment.