serialport-stream 0.2.0

Async TryStream for serialport reading utilizing serialport-rs using platform-specific I/O
Documentation

serialport-stream-rs

Implements futures::Stream and futures::io::AsyncRead utilizing serialport-rs. Initial poll starts background thread which will indefinitely wait for data in event, error or drop. There is no backpressure handling; the crate will buffer incoming data indefinitely.

Installation

Add this to your Cargo.toml:

[dependencies]
serialport-stream = "0.2.0"
futures-lite = "2.0"

Usage

Basic Usage

use serialport_stream::{new, SerialPortStream};
use futures_lite::stream;

fn read_serial() -> std::io::Result<()> {
    // Create a serial port stream using the builder API
    let stream = new("COM3", 115200)
        .dtr_on_open(true)
        .open()?;

    for event in stream::block_on(stream) {
        let bytes = event?;
        println!("bytes {bytes:?}");
    }

    Ok(())
}

Using with Tokio

use serialport_stream::new;
use futures_lite::stream::StreamExt;

#[tokio::main]
async fn main() -> std::io::Result<()> {
     let mut stream = new("/dev/ttyUSB0", 9600)
        .open()?;

    while let Ok(Some(result)) = stream.try_next().await {
        println!("Received: {:?}", result);
    }

    Ok(())
}

AsyncRead works the same way: cargo run --example tokio_async_read -- /dev/ttyUSB0 115200 (examples/tokio_async_read.rs).

Asynchronous byte reads (AsyncRead)

The stream implements futures::io::AsyncRead. Combine it with AsyncReadExt for helpers such as read and read_to_end. Data comes from the same internal buffer as futures::Stream; use one primary read style per open stream.

For Tokio’s tokio::io::AsyncRead, bridge via tokio_util::compat (add tokio-util with the compat feature to your crate).

API Overview

Builder Functions

  • new(path, baud_rate) - Create a new builder
  • .data_bits(DataBits) - Set data bits (5, 6, 7, 8)
  • .flow_control(FlowControl) - Set flow control (None, Software, Hardware)
  • .parity(Parity) - Set parity (None, Odd, Even)
  • .stop_bits(StopBits) - Set stop bits (One, Two)
  • .dtr_on_open(bool) - Control DTR signal on open
  • .open() - Open the port and create the stream

SerialPortStream Methods

  • Implements futures::Stream — asynchronous streaming (Result<Vec<u8>, io::Error> items)
  • Implements futures::io::AsyncRead — partial reads from the same receive FIFO; re-exported with AsyncReadExt
  • Other methods expose serial control lines (write_request_to_send, modem status reads, buffers, breaks, etc.)

License

This project is licensed under either of:

at your option.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

This crate builds upon serialport-rs for cross-platform serial port access.