serialport-stream-rs
Async serial port I/O as futures::Stream, AsyncRead, and AsyncWrite. Uses POSIX termios on Unix and Win32 COMM APIs on Windows.
Async runtime agnostic — implements futures traits only; no Tokio/async-std dependency. Works with any executor that polls those futures (Tokio, async-std, futures_lite::future::block_on, etc.).
Installation
[]
= "0.3"
Optional diagnostic logs via the tracing crate:
= { = "0.3", = ["tracing"] }
Examples below also use futures-lite (blocking) or tokio.
Usage
Stream (blocking)
use new;
use stream;
Stream (Tokio)
use ;
async
Reading
A background thread fills one in-memory FIFO. There is no backpressure; the buffer can grow without bound.
| API | Each call returns |
|---|---|
Stream / try_next |
All bytes in the FIFO (buffer drained) |
AsyncRead |
Up to your buffer length; remainder stays in the FIFO |
Use one read style per port. Mixing Stream and AsyncRead can split messages across calls.
AsyncReadExt is re-exported (read, read_to_end, etc.). Example: cargo run --example tokio_async_read -- /dev/ttyUSB0 115200. Add --features tracing with --trace for diagnostic logs.
For tokio::io::AsyncRead, bridge with tokio_util::compat (tokio-util feature compat).
Writing
AsyncWrite / AsyncWriteExt are re-exported.
use ;
async
Example: cargo run --example tokio_async_rw -- /dev/ttyUSB0 115200. Add --features tracing with --trace for diagnostic logs.
For tokio::io::AsyncWrite, use tokio_util::compat as above.
Builder
Open with new(path, baud_rate), then chain options and call .open():
.data_bits,.parity,.stop_bits,.flow_control— default is 8N1, no flow control.dtr_on_open(bool)— drive DTR on open.clear(ClearBuffer::Input | Output | All)— purge driver buffers at open
Types DataBits, Parity, StopBits, FlowControl, and ClearBuffer are exported from serialport_stream.
Acknowledgements
Some of the platform I/O code is inspired by serialport-rs.
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.