[][src]Struct serial_unit_testing::serial::Serial

pub struct Serial { /* fields omitted */ }

A serial port connection.

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

Methods

impl Serial[src]

pub fn open(port_name: &str) -> Result<Serial, Error>[src]

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;

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

    Ok(())
}

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

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;

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

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

    Ok(())
}

pub fn write(&mut self, text: &str) -> Result<(), Error>[src]

Write text to the serial port.

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

pub fn write_format(
    &mut self,
    text: &str,
    text_format: TextFormat
) -> Result<(), Error>
[src]

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;

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

    Ok(())
}

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

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;

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

    Ok(())
}

pub fn read_str(&mut self) -> Result<String, Error>[src]

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.

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

Read a string with minimum length.

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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;

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

    Ok(())
}

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

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.

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

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.

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

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 Send for Serial

impl !Sync for Serial

impl Unpin for Serial

impl !UnwindSafe for Serial

impl !RefUnwindSafe for Serial

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]