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
impl Serial
Sourcepub fn open(port_name: &str) -> Result<Serial>
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(())
}
Sourcepub fn open_with_settings(port_name: &str, settings: Settings) -> Result<Serial>
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(())
}
Sourcepub fn name(&self) -> Option<String>
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.
Sourcepub fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()>
pub fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()>
Set the baud rate.
Sourcepub fn baud_rate(&self) -> Result<u32>
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.
Sourcepub fn set_data_bits(&mut self, data_bits: DataBits) -> Result<()>
pub fn set_data_bits(&mut self, data_bits: DataBits) -> Result<()>
Set the number of data bits.
Sourcepub fn set_parity(&mut self, parity: Parity) -> Result<()>
pub fn set_parity(&mut self, parity: Parity) -> Result<()>
Set the parity checking mode
Sourcepub fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<()>
pub fn set_stop_bits(&mut self, stop_bits: StopBits) -> Result<()>
Set the number of stop bits.
Sourcepub fn set_flow_control(&mut self, flow_control: FlowControl) -> Result<()>
pub fn set_flow_control(&mut self, flow_control: FlowControl) -> Result<()>
Set the flow control.
Sourcepub fn flow_control(&self) -> Result<FlowControl>
pub fn flow_control(&self) -> Result<FlowControl>
Get the flow control.
Sourcepub fn set_timeout(&mut self, timeout: u64) -> Result<()>
pub fn set_timeout(&mut self, timeout: u64) -> Result<()>
Set the I/O timeout.
Sourcepub fn write(&mut self, text: &str) -> Result<usize>
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.
Sourcepub fn write_format(
&mut self,
text: &str,
text_format: TextFormat,
) -> Result<usize>
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(())
}
Sourcepub fn read(&mut self) -> Result<&[u8]>
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(())
}
Sourcepub fn read_str(&mut self) -> Result<String>
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.
Sourcepub fn read_str_until(&mut self, desired: &str) -> Result<String>
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.
Sourcepub fn read_min_str(&mut self, min_length: usize) -> Result<String>
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.
Sourcepub fn read_min_str_until(
&mut self,
min_length: usize,
desired: &str,
) -> Result<String>
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.
Sourcepub fn read_str_with_format(&mut self, format: TextFormat) -> Result<String>
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.
Sourcepub fn read_min_str_with_format(
&mut self,
min_length: usize,
format: TextFormat,
) -> Result<String>
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.
Sourcepub fn read_with_timeout(&mut self, timeout: Duration) -> Result<&[u8]>
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.
Sourcepub fn read_str_with_timeout(&mut self, timeout: Duration) -> Result<String>
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.
Sourcepub fn read_min_str_with_timeout(
&mut self,
min_length: usize,
timeout: Duration,
) -> Result<String>
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.
Sourcepub fn read_str_until_with_timeout(
&mut self,
desired: &str,
timeout: Duration,
) -> Result<String>
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.
Sourcepub fn read_str_with_format_and_timeout(
&mut self,
format: TextFormat,
timeout: Duration,
) -> Result<String>
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.
Sourcepub fn read_min_str_with_format_and_timeout(
&mut self,
min_length: usize,
format: TextFormat,
timeout: Duration,
) -> Result<String>
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.
Sourcepub fn check(
&mut self,
text: &str,
desired_response: &str,
) -> Result<(bool, String)>
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(())
}
Sourcepub fn check_read(&mut self, desired_response: &str) -> Result<(bool, String)>
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.
Sourcepub fn check_with_settings(
&mut self,
text: &str,
desired_response: &str,
settings: &CheckSettings,
) -> Result<(bool, String)>
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.
Sourcepub fn check_read_with_settings(
&mut self,
desired_response: &str,
settings: &CheckSettings,
) -> Result<(bool, String)>
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.