Crate tokio_serial_pacing

Crate tokio_serial_pacing 

Source
Expand description

This crate is a simple wrapper around

tokio_serial::SerialStream for use with tokio-modbus in RTU mode. The wrappers can ensure that an application obeys the inter frame delay of 3.5 characters between reading and writing.

The helper wait_time can attempt to calculate the proper delay from the serial port settings. In practice, the timers in tokio are not very exact, but since in theory, it’s always okay to have a longer delay, that is not considered a big concern.

the SerialPacing trait just wraps the “set_delay” function as shared functionality, and then SerialReadPacing and SerialWritePacing implement the functionality around AsyncRead and AsyncWrite traits.

Example

#[tokio::main(flavor="current_thread")]
async fn main() -> std::io::Result<()> {
  use tokio_serial::{SerialPort, SerialStream};
  use tokio_serial_pacing::{SerialPacing, SerialWritePacing};

  let (tx, mut rx) = SerialStream::pair().expect("Failed to open PTY");
  let mut rx: SerialWritePacing<SerialStream> = rx.into();
  rx.set_delay(std::time::Duration::from_millis(3));
  Ok(())
}

Structs§

SerialReadPacing
Implement pacing by waiting between Write and Read operations.
SerialWritePacing
Implement pacing by waiting between Read and Write operations.

Traits§

SerialPacing
Shared trait for SerialReadPacing and SerialWritePacing that implements set_delay

Functions§

wait_time
Calculate the Modbus RTU 3.5t wait time from a serial port