#read-write #serial #tokio #operations #ensure #wrapper #pacing

tokio_serial_pacing

A pacing wrapper for SerialStream to ensure silence between Read & Write operations

2 releases

0.1.1 Mar 27, 2024
0.1.0 Mar 26, 2024

#506 in Hardware support

Download history 219/week @ 2024-03-24 58/week @ 2024-03-31

277 downloads per month

MIT license

23KB
330 lines

Serial pacing wrappers around tokio_serial::SerialStream

This can be used on anything that implements AsyncRead + AsyncWrite, but is currently only tested for serial ports.

I built this as the Modbus RTU specification requires a certain time of silence between Reads and Writes to be in spec. (It also requires a specific inter-character timing that I will just ignore)

This code is NOT at all specific enough about the timer timeouts, as tokio timeouts are not high precision enough to guarantee anything. However, it works well enough to ensure at least a certain amount of time goes between read & write operations.

Due to reasons (...) I've also implemented the inverse, Waiting after a write before reading, although I have no idea why that would be necessary.

Example

use std::time::Duration;
use tokio_serial::{SerialPort, SerialStream};
use tokio_serial_pacing::{SerialPacing, SerialWritePacing};

#[tokio::main(flavor="current_thread")]
async fn main() -> std::io::Result<()> {
  let (tx, mut rx) = SerialStream::pair().expect("Failed to open PTY");
  let mut rx: SerialWritePacing<SerialStream> = rx.into();
  rx.set_delay(Duration::from_millis(3));
  Ok(())
}

Dependencies

~5–16MB
~176K SLoC