So I have some data going through a serial port at regular intervals. Eight bytes are sent every 2 seconds.
I would read this data. Because transmission is constant, I can't just start reading at any moment. I need to align the data. So clearly, the way to do this is to send a header or some sort of separator bytes.
With the Read
trait, I can read a certain number of bytes into my buffer. It might look like this:
let mut serial_buf: Vec<u8> = vec![0; 16];
loop {
match port.read(serial_buf.as_mut_slice()) {
Ok(t) => print_data(&serial_buf[..t]),
Err(ref e) if e.kind() == std::io::ErrorKind::TimedOut => (),
Err(e) => eprintln!("{:?}", e),
}
}
But the output I get will look something like this without alignment (with bytes 'abcd' being sent every 2 seconds):
abcd
a
bcd
abc
d
a
bcd
ab
cd
So what's the most practical way of reading and discarding until an alignment bit is found, then making sure that all subsequent reads are aligned?
Thanks,