Trait serial_core::SerialDevice [] [src]

pub trait SerialDevice: Read + Write {
    type Settings: SerialPortSettings;
    fn read_settings(&self) -> Result<Self::Settings>;
    fn write_settings(&mut self, settings: &Self::Settings) -> Result<()>;
    fn timeout(&self) -> Duration;
    fn set_timeout(&mut self, timeout: Duration) -> 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 implementing serial devices.

This trait is meant to be used to implement new serial port devices. To use a serial port device, the SerialPort trait should be used instead. Any type that implements the SerialDevice trait will automatically implement the SerialPort trait as well.

To implement a new serial port device, it's necessary to define a type that can manipulate the serial port device's settings (baud rate, parity mode, etc). This type is defined by the Settings associated type. The current settings should be determined by reading from the hardware or operating system for every call to read_settings(). The settings can then be manipulated in memory before being commited to the device with write_settings().

Types that implement SerialDevice must also implement std::io::Read and std::io::Write. The read() and write() operations of these traits should honor the timeout that has been set with the most recent successful call to set_timeout(). This timeout value should also be accessible by calling the timeout() method.

A serial port device should also provide access to some basic control signals: RTS, DTR, CTS, DSR, RI, and CD. The values for the control signals are represented as boolean values, with true indicating the the control signal is active.

Lastly, types that implement SerialDevice should release any acquired resources when dropped.

Associated Types

A type that implements the settings for the serial port device.

The Settings type is used to retrieve and modify the serial port's settings. This type should own any native structures used to manipulate the device's settings, but it should not cause any changes in the underlying hardware until written to the device with write_settings().

Required Methods

Returns the device's current settings.

This function attempts to read the current settings from the hardware. The hardware's current settings may not match the settings that were most recently written to the hardware with write_settings().

Errors

This function returns an error if the settings could not be read from the underlying hardware:

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

Applies new settings to the serial device.

This function attempts to apply all settings to the serial device. Some settings may not be supported by the underlying hardware, in which case the result is dependent on the implementation. A successful return value does not guarantee that all settings were appliied successfully. To check which settings were applied by a successful write, applications should use the read_settings() method to obtain the latest configuration state from the 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.

Returns the current timeout.

Sets the timeout for future I/O operations.

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