Crate framed_serial [] [src]

Add frames to serial connections. Useful for embedded devices. Can be built with no_std.

The main type of interest is FramedConnection, which takes ownership of a serial connection and allows sending and receiving complete frames.

To use with the standard library, put this in your Cargo.toml:

[dependencies]
framed-serial = "0.1"

To use in an embedded device with no_std, put this in your Cargo.toml:

[dependencies]
framed-serial = {version = "0.1", default-features = false, features = ["collections"]}

Example usage:

#[cfg(feature = "std")]
extern crate serial;
extern crate framed_serial;
#[cfg(feature = "std")]
use serial::SerialPort;

#[cfg(feature = "std")]
fn wait_for_frame() -> Result<(),framed_serial::Error> {

    let device = match std::env::var("DEVICE") {
        Ok(val) => val,
        Err(_) => "/dev/ttyACM0".to_string(),
    };
    println!("opening device {}", device);
    let mut raw = serial::open(&device).expect("open serial port");

    // Async processing depends on this being short.
    raw.set_timeout(std::time::Duration::from_millis(100)).expect("set_timeout");

    let my_ser = framed_serial::SerialWrap::new(raw);
    let mut conn = framed_serial::FramedConnection::new(my_ser);

    // Loop until we get a frame. This requires a connected device
    // sending with FramedConnection.
    loop {
        let tick_state = conn.tick()?;
        if tick_state.recv_is_done {
            let data = conn.get_frame()?;
            println!("{:?}", data);
            break;
        }
    }
    Ok(())
}

// This example requires std to compile. To run successfully, it further requires a connected
// serial device on /dev/ttyACM0 implementing `FramedConnection`. Use conditional compilation
// to run only if the `device_connected` feature was specified at compile time.

#[cfg(feature = "device_connected")]
fn main() {
    wait_for_frame().unwrap();
}
#[cfg(not(feature = "device_connected"))]
fn main() {
    // Do nothing if device is not connected.
}

Structs

Error

Error type.

FramedConnection

Wrapper around a serial port to provide framed connections.

SerialWrap

Implment the traits required for a FramedConnection based on a serial::SerialPort.

TickProgress

The result of a tick(). Check for progress indication.

Constants

SENTINEL

A marker which appears only rarely in stream, used to catch frame start.