Struct Serial

Source
pub struct Serial { /* private fields */ }
Expand description

A serial port connection.

This struct handles the complete communication with a serial device regardless of the platform.

Implementations§

Source§

impl Serial

Source

pub fn open(port_name: &str) -> Result<Serial>

Open a new connection with default settings.

The port name is platform specific, e.g. starts with COM on Windows and /dev/tty or similar on UNIX systems.

Default settings are:

  • Baud rate: 9600
  • Timeout: 1000 (ms)
  • Data bits: 8
  • Parity: None,
  • Stop bits: 1,
  • Flow control: None
§Example
use serial_unit_testing::serial::Serial;
use serial_unit_testing::error::Result;

fn main() -> Result<()> {
    let mut serial = Serial::open("/dev/ttyACM0")?;
    serial.write("Hello World!")?;

    Ok(())
}
Source

pub fn open_with_settings(port_name: &str, settings: Settings) -> Result<Serial>

Open a new connection with given settings.

The port name is platform specific, e.g. starts with COM on Windows and /dev/tty or similar on UNIX systems.

§Example
use serial_unit_testing::serial::Serial;
use serial_unit_testing::serial::settings::Settings;
use serial_unit_testing::error::Result;

fn main() -> Result<()> {
    let mut settings = Settings::default();
    settings.baud_rate = 115200;

    let mut serial = Serial::open_with_settings("/dev/ttyACM0", &settings)?;
    serial.write("Hello World!")?;

    Ok(())
}
Source

pub fn settings(&self) -> Settings

Get the current serial settings.

Source

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

Get the port name if any exists.

Virtual ports may not have a name and the name may be shortened.

Source

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

Set the baud rate.

Source

pub fn baud_rate(&self) -> Result<u32>

Get the baud rate.

This will return the actual device baud rate, which may differ from the last specified value.

Source

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

Set the number of data bits.

Source

pub fn data_bits(&self) -> Result<DataBits>

Get the number of data bits.

Source

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

Set the parity checking mode

Source

pub fn parity(&self) -> Result<Parity>

Get the parity checking mode

Source

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

Set the number of stop bits.

Source

pub fn stop_bits(&self) -> Result<StopBits>

Get the number of stop bits.

Source

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

Set the flow control.

Source

pub fn flow_control(&self) -> Result<FlowControl>

Get the flow control.

Source

pub fn set_timeout(&mut self, timeout: u64) -> Result<()>

Set the I/O timeout.

Source

pub fn timeout(&self) -> u64

Get the I/O timeout.

Source

pub fn write(&mut self, text: &str) -> Result<usize>

Write text to the serial port.

This is the same as using Serial::write_format with TextFormat::Text as format specifier.

Source

pub fn write_format( &mut self, text: &str, text_format: TextFormat, ) -> Result<usize>

Write data in the given format.

For a list of supported formats see TextFormat. TextFormat::Text is the same as using Serial::write.

§Example
use serial_unit_testing::serial::Serial;
use serial_unit_testing::utils::TextFormat;
use serial_unit_testing::error::Result;

fn main() -> Result<()> {
    let mut serial = Serial::open("/dev/ttyACM0")?;
    serial.write_format("0a5f", TextFormat::Hex)?;

    Ok(())
}
Source

pub fn read(&mut self) -> Result<&[u8]>

Read any amount of data.

At least one byte of data must be read to return data. The method fails when no data could be read in the timeout duration.

§Example
use serial_unit_testing::serial::Serial;
use serial_unit_testing::error::Result;

fn main() -> Result<()> {
    let mut serial = Serial::open("/dev/ttyACM0")?;
    let data = serial.read().unwrap();

    Ok(())
}
Source

pub fn read_str(&mut self) -> Result<String>

Read a string.

At least one character must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_str_until(&mut self, desired: &str) -> Result<String>

Read a string until desired substring is found.

At least one character and desired substring must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_min_str(&mut self, min_length: usize) -> Result<String>

Read a string with minimum length until desired substring is found.

At least the amount of characters given by min_length must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_min_str_until( &mut self, min_length: usize, desired: &str, ) -> Result<String>

Read a string with minimum length.

At least the amount of characters given by min_length and desired substring must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_str_with_format(&mut self, format: TextFormat) -> Result<String>

Read a string as given format.

The bytes received will be formatted into the given string format.

At least one character must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_min_str_with_format( &mut self, min_length: usize, format: TextFormat, ) -> Result<String>

Read a string as given format.

The bytes received will be formatted into the given string format.

At least the amount of characters (not bytes) given by min_length must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_with_timeout(&mut self, timeout: Duration) -> Result<&[u8]>

Read any amount of data in given timeout duration.

This function can be used to use a different timeout for a single read. Otherwise see the timeout property of serial.

At least one byte of data must be read to return data. The method fails when no data could be read in the timeout duration.

Source

pub fn read_str_with_timeout(&mut self, timeout: Duration) -> Result<String>

Read a string in given timeout duration.

This function can be used to use a different timeout for a single read. Otherwise see the timeout property of serial.

At least one character must be read to return successfully. The method fails when no data could be read in the timeout duration.

Source

pub fn read_min_str_with_timeout( &mut self, min_length: usize, timeout: Duration, ) -> Result<String>

Read a string with minimum length in given timeout duration.

This function can be used to use a different timeout for a single read. Otherwise see the timeout property of serial.

At least the amount of characters given by min_length must be read to return successfully. The method fails when no characters could be read in the given timeout duration.

Source

pub fn read_str_until_with_timeout( &mut self, desired: &str, timeout: Duration, ) -> Result<String>

Read a string until desired substring is found in given timeout duration.

This function can be used to use a different timeout for a single read. Otherwise see the timeout property of serial.

At least one character and desired substring must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_str_with_format_and_timeout( &mut self, format: TextFormat, timeout: Duration, ) -> Result<String>

Read a string as given format in given timeout duration.

The bytes received will be formatted into the given string format. This function can be used to use a different timeout for a single read. Otherwise see the timeout property of serial.

At least one character must be read to return successfully. The method fails when no characters could be read in the timeout duration.

Source

pub fn read_min_str_with_format_and_timeout( &mut self, min_length: usize, format: TextFormat, timeout: Duration, ) -> Result<String>

Read a string with minimum length as given format in given timeout duration.

The bytes received will be formatted into the given string format. This function can be used to use a different timeout for a single read. Otherwise see the timeout property of serial.

At least the amount of characters given by min_length must be read to return successfully. The method fails when no characters could be read in the given timeout duration.

Source

pub fn check( &mut self, text: &str, desired_response: &str, ) -> Result<(bool, String)>

Send text to the serial and check if the response matches the desired response.

The check will return early if the beginning of the responses does not match.

Returns whether the actual response matches the desired response and the actual response. Fails with an timeout error or internal serial error.

§Example
use serial_unit_testing::serial::Serial;
use serial_unit_testing::error::Result;

fn main() -> Result<()> {
    let mut serial = Serial::open("/dev/ttyACM0")?;
    let (result, actual_response) = serial.check("hello", "world")?;

    Ok(())
}
Source

pub fn check_read(&mut self, desired_response: &str) -> Result<(bool, String)>

Check if a response matches the desired response.

The check will return early if the beginning of the responses does not match.

Returns whether the actual response matches the desired response and the actual response. Fails with an timeout error or internal serial error.

Source

pub fn check_with_settings( &mut self, text: &str, desired_response: &str, settings: &CheckSettings, ) -> Result<(bool, String)>

Send text to the serial and check if the response matches the desired response with given settings.

The check will return early if the beginning of the responses does not match.

Returns whether the actual response matches the desired response and the actual response. Fails with an timeout error or internal serial error.

Source

pub fn check_read_with_settings( &mut self, desired_response: &str, settings: &CheckSettings, ) -> Result<(bool, String)>

Check if a response matches desired response with given settings.

The check will return early if the beginning of the responses does not match.

Returns whether the actual response matches the desired response and the actual response. Fails with an timeout error or internal serial error.

Auto Trait Implementations§

§

impl Freeze for Serial

§

impl !RefUnwindSafe for Serial

§

impl Send for Serial

§

impl !Sync for Serial

§

impl Unpin for Serial

§

impl !UnwindSafe for Serial

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.