Trait serial::SerialPortExt [] [src]

pub trait SerialPortExt: SerialPort {
    fn reconfigure<F: FnOnce(&mut Self::Settings) -> Result<()>>(&mut self, setup: F) -> Result<()> { ... }
}

An extension trait that provides convenience methods for serial ports.

Provided Methods

fn reconfigure<F: FnOnce(&mut Self::Settings) -> Result<()>>(&mut self, setup: F) -> Result<()>

Alter the serial port's configuration.

This method expects a function, which takes a mutable reference to the serial port's configuration settings. The serial port's current settings, read from the device, are yielded to the provided function. After the function returns, any changes made to the settings object will be written back to the device.

Errors

If this function encounters any kind of I/O error while reading or writing the device's configuration settings, a std::io::Error will be returned.

Example

The following is a function that toggles a serial port's settings between one and two stop bits:

use std::io;
use serial::prelude::*;

fn toggle_stop_bits<T: SerialPort>(port: &mut T) -> serial::Result<()> {
    port.reconfigure(|settings| {
        let stop_bits = match settings.stop_bits() {
            Some(serial::Stop1)        => serial::Stop2,
            Some(serial::Stop2) | None => serial::Stop1
        };

        settings.set_stop_bits(stop_bits);
        Ok(())
    })
}

Implementors