#read-write #tokio #serial-port #timing #async-io #operations #wrapper

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

#1147 in Hardware support

Download history 4/week @ 2024-07-12 37/week @ 2024-07-19 101/week @ 2024-07-26 32/week @ 2024-08-02 10/week @ 2024-08-09 68/week @ 2024-08-16 39/week @ 2024-08-23 1/week @ 2024-08-30 54/week @ 2024-09-06 53/week @ 2024-09-13 36/week @ 2024-09-20 14/week @ 2024-09-27 3/week @ 2024-10-04 29/week @ 2024-10-11

82 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–17MB
~158K SLoC