Trait tokio_serial::SerialPort

source ·
pub trait SerialPort: Send + Read + Write {
Show 25 methods // Required methods fn name(&self) -> Option<String>; fn baud_rate(&self) -> Result<u32, Error>; fn data_bits(&self) -> Result<DataBits, Error>; fn flow_control(&self) -> Result<FlowControl, Error>; fn parity(&self) -> Result<Parity, Error>; fn stop_bits(&self) -> Result<StopBits, Error>; fn timeout(&self) -> Duration; fn set_baud_rate(&mut self, baud_rate: u32) -> Result<(), Error>; fn set_data_bits(&mut self, data_bits: DataBits) -> Result<(), Error>; fn set_flow_control( &mut self, flow_control: FlowControl ) -> Result<(), Error>; fn set_parity(&mut self, parity: Parity) -> Result<(), Error>; fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<(), Error>; fn set_timeout(&mut self, timeout: Duration) -> Result<(), Error>; fn write_request_to_send(&mut self, level: bool) -> Result<(), Error>; fn write_data_terminal_ready(&mut self, level: bool) -> Result<(), Error>; fn read_clear_to_send(&mut self) -> Result<bool, Error>; fn read_data_set_ready(&mut self) -> Result<bool, Error>; fn read_ring_indicator(&mut self) -> Result<bool, Error>; fn read_carrier_detect(&mut self) -> Result<bool, Error>; fn bytes_to_read(&self) -> Result<u32, Error>; fn bytes_to_write(&self) -> Result<u32, Error>; fn clear(&self, buffer_to_clear: ClearBuffer) -> Result<(), Error>; fn try_clone(&self) -> Result<Box<dyn SerialPort>, Error>; fn set_break(&self) -> Result<(), Error>; fn clear_break(&self) -> Result<(), Error>;
}
Expand description

A trait for serial port devices

This trait is all that’s necessary to implement a new serial port driver for a new platform.

Required Methods§

source

fn name(&self) -> Option<String>

Returns the name of this port if it exists.

This name may not be the canonical device name and instead be shorthand. Additionally it may not exist for virtual ports.

source

fn baud_rate(&self) -> Result<u32, Error>

Returns the current baud rate.

This may return a value different from the last specified baud rate depending on the platform as some will return the actual device baud rate rather than the last specified baud rate.

source

fn data_bits(&self) -> Result<DataBits, Error>

Returns the character size.

This function returns None if the character size could not be determined. This may occur if the hardware is in an uninitialized state or is using a non-standard character size. Setting a baud rate with set_char_size() should initialize the character size to a supported value.

source

fn flow_control(&self) -> Result<FlowControl, Error>

Returns the flow control mode.

This function returns None if the flow control mode could not be determined. This may occur if the hardware is in an uninitialized state or is using an unsupported flow control mode. Setting a flow control mode with set_flow_control() should initialize the flow control mode to a supported value.

source

fn parity(&self) -> Result<Parity, Error>

Returns the parity-checking mode.

This function returns None if the parity mode could not be determined. This may occur if the hardware is in an uninitialized state or is using a non-standard parity mode. Setting a parity mode with set_parity() should initialize the parity mode to a supported value.

source

fn stop_bits(&self) -> Result<StopBits, Error>

Returns the number of stop bits.

This function returns None if the number of stop bits could not be determined. This may occur if the hardware is in an uninitialized state or is using an unsupported stop bit configuration. Setting the number of stop bits with set_stop-bits() should initialize the stop bits to a supported value.

source

fn timeout(&self) -> Duration

Returns the current timeout.

source

fn set_baud_rate(&mut self, baud_rate: u32) -> Result<(), Error>

Sets the baud rate.

§Errors

If the implementation does not support the requested baud rate, this function may return an InvalidInput error. Even if the baud rate is accepted by set_baud_rate(), it may not be supported by the underlying hardware.

source

fn set_data_bits(&mut self, data_bits: DataBits) -> Result<(), Error>

Sets the character size.

source

fn set_flow_control(&mut self, flow_control: FlowControl) -> Result<(), Error>

Sets the flow control mode.

source

fn set_parity(&mut self, parity: Parity) -> Result<(), Error>

Sets the parity-checking mode.

source

fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<(), Error>

Sets the number of stop bits.

source

fn set_timeout(&mut self, timeout: Duration) -> Result<(), Error>

Sets the timeout for future I/O operations.

source

fn write_request_to_send(&mut self, level: bool) -> Result<(), Error>

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.
source

fn write_data_terminal_ready(&mut self, level: bool) -> Result<(), Error>

Writes to the Data Terminal Ready pin

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.
source

fn read_clear_to_send(&mut self) -> Result<bool, 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.
source

fn read_data_set_ready(&mut self) -> Result<bool, Error>

Reads the state of the 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.
source

fn read_ring_indicator(&mut self) -> Result<bool, Error>

Reads the state of the 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.
source

fn read_carrier_detect(&mut self) -> Result<bool, Error>

Reads the state of the 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.
source

fn bytes_to_read(&self) -> Result<u32, Error>

Gets the number of bytes available to be read from the input buffer.

§Errors

This function may return the following errors:

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

fn bytes_to_write(&self) -> Result<u32, Error>

Get the number of bytes written to the output buffer, awaiting transmission.

§Errors

This function may return the following errors:

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

fn clear(&self, buffer_to_clear: ClearBuffer) -> Result<(), Error>

Discards all bytes from the serial driver’s input buffer and/or output buffer.

§Errors

This function may return the following errors:

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

fn try_clone(&self) -> Result<Box<dyn SerialPort>, Error>

Attempts to clone the SerialPort. This allow you to write and read simultaneously from the same serial connection. Please note that if you want a real asynchronous serial port you should look at mio-serial or tokio-serial.

Also, you must be very careful when changing the settings of a cloned SerialPort : since the settings are cached on a per object basis, trying to modify them from two different objects can cause some nasty behavior.

§Errors

This function returns an error if the serial port couldn’t be cloned.

source

fn set_break(&self) -> Result<(), Error>

Start transmitting a break

source

fn clear_break(&self) -> Result<(), Error>

Stop transmitting a break

Trait Implementations§

source§

impl Debug for dyn SerialPort

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Implementations on Foreign Types§

source§

impl SerialPort for SerialStream

source§

fn name(&self) -> Option<String>

Return the name associated with the serial port, if known.

source§

fn baud_rate(&self) -> Result<u32, Error>

Returns the current baud rate.

This function returns None if the baud rate could not be determined. This may occur if the hardware is in an uninitialized state. Setting a baud rate with set_baud_rate() should initialize the baud rate to a supported value.

source§

fn data_bits(&self) -> Result<DataBits, Error>

Returns the character size.

This function returns None if the character size could not be determined. This may occur if the hardware is in an uninitialized state or is using a non-standard character size. Setting a baud rate with set_char_size() should initialize the character size to a supported value.

source§

fn flow_control(&self) -> Result<FlowControl, Error>

Returns the flow control mode.

This function returns None if the flow control mode could not be determined. This may occur if the hardware is in an uninitialized state or is using an unsupported flow control mode. Setting a flow control mode with set_flow_control() should initialize the flow control mode to a supported value.

source§

fn parity(&self) -> Result<Parity, Error>

Returns the parity-checking mode.

This function returns None if the parity mode could not be determined. This may occur if the hardware is in an uninitialized state or is using a non-standard parity mode. Setting a parity mode with set_parity() should initialize the parity mode to a supported value.

source§

fn stop_bits(&self) -> Result<StopBits, Error>

Returns the number of stop bits.

This function returns None if the number of stop bits could not be determined. This may occur if the hardware is in an uninitialized state or is using an unsupported stop bit configuration. Setting the number of stop bits with set_stop-bits() should initialize the stop bits to a supported value.

source§

fn timeout(&self) -> Duration

Returns the current timeout. This parameter is const and equal to zero and implemented due to required for trait completeness.

source§

fn set_baud_rate(&mut self, baud_rate: u32) -> Result<(), Error>

Sets the baud rate.

§Errors

If the implementation does not support the requested baud rate, this function may return an InvalidInput error. Even if the baud rate is accepted by set_baud_rate(), it may not be supported by the underlying hardware.

source§

fn set_data_bits(&mut self, data_bits: DataBits) -> Result<(), Error>

Sets the character size.

source§

fn set_flow_control(&mut self, flow_control: FlowControl) -> Result<(), Error>

Sets the flow control mode.

source§

fn set_parity(&mut self, parity: Parity) -> Result<(), Error>

Sets the parity-checking mode.

source§

fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<(), Error>

Sets the number of stop bits.

source§

fn set_timeout(&mut self, _: Duration) -> Result<(), Error>

Sets the timeout for future I/O operations. This parameter is ignored but required for trait completeness.

source§

fn write_request_to_send(&mut self, level: bool) -> Result<(), Error>

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.
source§

fn write_data_terminal_ready(&mut self, level: bool) -> Result<(), Error>

Writes to the Data Terminal Ready pin

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.
source§

fn read_clear_to_send(&mut self) -> Result<bool, 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.
source§

fn read_data_set_ready(&mut self) -> Result<bool, Error>

Reads the state of the 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.
source§

fn read_ring_indicator(&mut self) -> Result<bool, Error>

Reads the state of the 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.
source§

fn read_carrier_detect(&mut self) -> Result<bool, Error>

Reads the state of the 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.
source§

fn bytes_to_read(&self) -> Result<u32, Error>

Gets the number of bytes available to be read from the input buffer.

§Errors

This function may return the following errors:

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

fn bytes_to_write(&self) -> Result<u32, Error>

Get the number of bytes written to the output buffer, awaiting transmission.

§Errors

This function may return the following errors:

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

fn clear(&self, buffer_to_clear: ClearBuffer) -> Result<(), Error>

Discards all bytes from the serial driver’s input buffer and/or output buffer.

§Errors

This function may return the following errors:

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

fn try_clone(&self) -> Result<Box<dyn SerialPort>, Error>

Cloning is not supported for SerialStream objects

This logic has never really completely worked. Cloned file descriptors in asynchronous code is a semantic minefield. Are you cloning the file descriptor? Are you cloning the event flags on the file descriptor? Both? It’s a bit of a mess even within one OS, let alone across multiple OS’s

Maybe it can be done with more work, but until a clear use-case is required (or mio/tokio gets an equivalent of the unix AsyncFd for async file handles, see https://github.com/tokio-rs/tokio/issues/3781 and https://github.com/tokio-rs/tokio/pull/3760#issuecomment-839854617) I would rather not have any code available over a kind-of-works-maybe impl. So I’ll leave this code here for now but hard-code it disabled.

source§

fn set_break(&self) -> Result<(), Error>

Start transmitting a break

source§

fn clear_break(&self) -> Result<(), Error>

Stop transmitting a break

source§

impl<T> SerialPort for &mut T
where T: SerialPort,

source§

fn name(&self) -> Option<String>

source§

fn baud_rate(&self) -> Result<u32, Error>

source§

fn data_bits(&self) -> Result<DataBits, Error>

source§

fn flow_control(&self) -> Result<FlowControl, Error>

source§

fn parity(&self) -> Result<Parity, Error>

source§

fn stop_bits(&self) -> Result<StopBits, Error>

source§

fn timeout(&self) -> Duration

source§

fn set_baud_rate(&mut self, baud_rate: u32) -> Result<(), Error>

source§

fn set_data_bits(&mut self, data_bits: DataBits) -> Result<(), Error>

source§

fn set_flow_control(&mut self, flow_control: FlowControl) -> Result<(), Error>

source§

fn set_parity(&mut self, parity: Parity) -> Result<(), Error>

source§

fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<(), Error>

source§

fn set_timeout(&mut self, timeout: Duration) -> Result<(), Error>

source§

fn write_request_to_send(&mut self, level: bool) -> Result<(), Error>

source§

fn write_data_terminal_ready(&mut self, level: bool) -> Result<(), Error>

source§

fn read_clear_to_send(&mut self) -> Result<bool, Error>

source§

fn read_data_set_ready(&mut self) -> Result<bool, Error>

source§

fn read_ring_indicator(&mut self) -> Result<bool, Error>

source§

fn read_carrier_detect(&mut self) -> Result<bool, Error>

source§

fn bytes_to_read(&self) -> Result<u32, Error>

source§

fn bytes_to_write(&self) -> Result<u32, Error>

source§

fn clear(&self, buffer_to_clear: ClearBuffer) -> Result<(), Error>

source§

fn try_clone(&self) -> Result<Box<dyn SerialPort>, Error>

source§

fn set_break(&self) -> Result<(), Error>

source§

fn clear_break(&self) -> Result<(), Error>

Implementors§