Trait serial_core::SerialPort [] [src]

pub trait SerialPort: Read + Write {
    fn timeout(&self) -> Duration;
    fn set_timeout(&mut self, timeout: Duration) -> Result<()>;
    fn configure(&mut self, settings: &PortSettings) -> Result<()>;
    fn reconfigure(
        &mut self,
        setup: &Fn(&mut SerialPortSettings) -> Result<()>
    ) -> Result<()>; fn set_rts(&mut self, level: bool) -> Result<()>; fn set_dtr(&mut self, level: bool) -> Result<()>; fn read_cts(&mut self) -> Result<bool>; fn read_dsr(&mut self) -> Result<bool>; fn read_ri(&mut self) -> Result<bool>; fn read_cd(&mut self) -> Result<bool>; }

A trait for serial port devices.

Serial port input and output is implemented through the std::io::Read and std::io::Write traits. A timeout can be set with the set_timeout() method and applies to all subsequent I/O operations.

The SerialPort trait exposes several common control signals. Each control signal is represented as a boolean, where true indicates that the signal is asserted.

The serial port will be closed when the value is dropped.

Required Methods

Returns the current timeout.

Sets the timeout for future I/O operations.

Configures a serial port device.

Errors

This function returns an error if the settings could not be applied to the underlying hardware:

  • NoDevice if the device was disconnected.
  • InvalidInput if a setting is not compatible with the underlying hardware.
  • Io for any other type of I/O error.

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

This function returns an error if the setup function returns an error or if there was an error while reading or writing the device's configuration settings:

  • NoDevice if the device was disconnected.
  • InvalidInput if a setting is not compatible with the underlying hardware.
  • Io for any other type of I/O error.
  • Any error returned by the setup function.

Example

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

use std::io;
use serial_core::prelude::*;

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

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

Sets the state of the RTS (Request To Send) control signal.

Setting a value of true asserts the RTS control signal. false clears the signal.

Errors

This function returns an error if the RTS control signal could not be set to the desired state on the underlying hardware:

  • NoDevice if the device was disconnected.
  • Io for any other type of I/O error.

Sets the state of the DTR (Data Terminal Ready) control signal.

Setting a value of true asserts the DTR control signal. false clears the signal.

Errors

This function returns an error if the DTR control signal could not be set to the desired state on the underlying hardware:

  • NoDevice if the device was disconnected.
  • Io for any other type of I/O error.

Reads the state of the CTS (Clear To Send) control signal.

This function returns a boolean that indicates whether the CTS control signal is asserted.

Errors

This function returns an error if the state of the CTS control signal could not be read from the underlying hardware:

  • NoDevice if the device was disconnected.
  • Io for any other type of I/O error.

Reads the state of the DSR (Data Set Ready) control signal.

This function returns a boolean that indicates whether the DSR control signal is asserted.

Errors

This function returns an error if the state of the DSR control signal could not be read from the underlying hardware:

  • NoDevice if the device was disconnected.
  • Io for any other type of I/O error.

Reads the state of the RI (Ring Indicator) control signal.

This function returns a boolean that indicates whether the RI control signal is asserted.

Errors

This function returns an error if the state of the RI control signal could not be read from the underlying hardware:

  • NoDevice if the device was disconnected.
  • Io for any other type of I/O error.

Reads the state of the CD (Carrier Detect) control signal.

This function returns a boolean that indicates whether the CD control signal is asserted.

Errors

This function returns an error if the state of the CD control signal could not be read from the underlying hardware:

  • NoDevice if the device was disconnected.
  • Io for any other type of I/O error.

Implementors