[−][src]Module embedded_hal_mock::serial
Serial mock implementations
You can set expectations for serial read and write transactions on a mock
Serial device. Note that, in the embedded_hal
crate, there
are the non-blocking serial traits, and there is a blocking
variant. You can use the same mock for both interfaces.
Usage: non-blocking serial traits
extern crate embedded_hal; extern crate embedded_hal_mock; // Note that we're using the non-blocking serial traits use embedded_hal::serial::{Read, Write}; use embedded_hal_mock::serial::{ Mock as SerialMock, Transaction as SerialTransaction, }; // Configure expectations let expectations = [ SerialTransaction::read(0x0A), SerialTransaction::read(0xFF), SerialTransaction::write([1, 2]), // (1) SerialTransaction::flush(), ]; let mut serial = SerialMock::new(&expectations); // Expect two reads let word = serial.read().unwrap(); assert_eq!(word, 0x0A); let word = serial.read().unwrap(); assert_eq!(word, 0xFF); // When designing against the non-blocking serial // trait, we expect two separate writes. These could be // expressed as two separate transactions, too. See (1) above. serial.write(1).unwrap(); serial.write(2).unwrap(); // Finally, we expect a flush serial.flush().unwrap(); // When you believe there are no more calls on the mock, // call done() to assert there are no pending transactions. serial.done();
Usage: blocking serial trait
extern crate embedded_hal; extern crate embedded_hal_mock; // Note that we're using the blocking serial write trait use embedded_hal::blocking::serial::Write; use embedded_hal::serial::Read; use embedded_hal_mock::serial::{ Mock as SerialMock, Transaction as SerialTransaction, }; // Configure expectations let expectations = [ SerialTransaction::read(0x0A), SerialTransaction::read(0xFF), SerialTransaction::write([1, 2]), // (2) SerialTransaction::flush(), ]; let mut serial = SerialMock::new(&expectations); // Expect two reads let word = serial.read().unwrap(); assert_eq!(word, 0x0A); let word = serial.read().unwrap(); assert_eq!(word, 0xFF); // We use the blocking write here, and we assert that // two words are written. See (2) above. serial.bwrite_all(&[1, 2]).unwrap(); // Finally, we expect a flush. Note that this is // a *blocking* flush from the blocking serial trait. serial.bflush().unwrap(); // When you believe there are no more calls on the mock, // call done() to assert there are no pending transactions. serial.done();
Structs
Mock | Mock serial device |
Transaction | A serial transaction |