Crate embedded_serial [] [src]

Traits to describe Serial port (UART) functionality.

This crate contains traits that are suitable for embedded development. This allows developers to produce crates that depend upon generic UART functionality (for example, an AT command interface), allowing the application developer to combine the crate with the specific UART available on their board.

It is similar to the C idea of using the functions getc and putc to decouple the IO device from the library, but in a more Rustic fashion.

Here's an example with the BlockingTx trait.

struct SomeStruct<T> { uart: T };
impl<T> SomeStruct<T> where T: embedded_serial::BlockingTx {
    fn new(uart: T) -> SomeStruct<T> {
        SomeStruct { uart: uart }
    }

    fn write_data(&mut self) -> Result<(), <T as embedded_serial::BlockingTx>::Error> {
        self.uart.puts(b"AT\n")?;
        Ok(())
    }
}

Here's an example with the BlockingTxWithTimeout trait.

struct SomeStruct<T> { uart: T };
impl<T> SomeStruct<T> where T: embedded_serial::BlockingTxWithTimeout {
    fn new(uart: T) -> SomeStruct<T> {
        SomeStruct { uart: uart }
    }

    fn write_data(&mut self, timeout: &<T as embedded_serial::BlockingTxWithTimeout>::Timeout) -> Result<bool, <T as embedded_serial::BlockingTxWithTimeout>::Error> {
        let len = self.uart.puts_to(b"AT\n", timeout)?;
        Ok(len == 3)
    }
}

Here's an example with the NonBlockingTx trait. You would call the write_data function until it returned Ok(true).

struct SomeStruct<T> {
    sent: Option<usize>,
    uart: T
};
impl<T> SomeStruct<T> where T: embedded_serial::NonBlockingTx {
    fn new(uart: T) -> SomeStruct<T> {
        SomeStruct { uart: uart, sent: Some(0) }
    }

    fn write_data(&mut self) -> Result<bool, <T as embedded_serial::NonBlockingTx>::Error> {
        let data = b"AT\n";
        if let Some(len) = self.sent {
            match self.uart.puts_try(&data[len..]) {
                (_, Ok(Some(()))) => { self.sent = None; Ok(true) },
                (sent, Ok(None)) => {
                    let total = len + sent;
                    if total == data.len() {
                        self.sent = None
                    } else {
                        self.sent = Some(total)
                    };
                    Ok(false)
                }
                (sent, Err(e)) => {
                    let total = len + sent;
                    if total == data.len() {
                        self.sent = None
                    } else {
                        self.sent = Some(total)
                    };
                    Err(e)
                }
            }
        } else {
            Ok(true)
        }
    }
}

Traits

BlockingRx

Implementors of this trait offer octet based serial data reception using a blocking API and requiring a mutable reference to self.

BlockingRxWithTimeout

Implementors of this trait offer octet based serial data reception using a blocking API with an upper bound on blocking time, and requiring a mutable reference to self.

BlockingTx

Implementors of this trait offer octet based serial data transmission using a blocking API and requiring a mutable reference to self.

BlockingTxWithTimeout

Implementors of this trait offer octet based serial data transmission using a blocking API with an upper bound on blocking time, and requiring a mutable reference to self.

NonBlockingRx

Implementors of this trait offer octet based serial data reception using a non-blocking API, and requiring a mutable reference to self.

NonBlockingTx

Implementors of this trait offer octet based serial data transmission using a non-blocking API and requiring a mutable reference to self.