39 lines
1.0 KiB
Rust
39 lines
1.0 KiB
Rust
use std::io;
|
|
use std::{io::Write, thread};
|
|
use std::time::Duration;
|
|
|
|
use cheapsdo_protocol::*;
|
|
use postcard::{to_stdvec_cobs, from_bytes_cobs};
|
|
|
|
fn main() {
|
|
let mut port = serialport::new("/dev/ttyACM1", 115_200)
|
|
.timeout(Duration::from_millis(10))
|
|
.open().expect("Failed to open port");
|
|
|
|
|
|
|
|
loop {
|
|
let host_msg = HostMessage::RequestStatus;
|
|
let msg_bytes = to_stdvec_cobs(&host_msg).unwrap();
|
|
|
|
port.write_all(&msg_bytes).unwrap();
|
|
|
|
let mut serial_buf: Vec<u8> = vec![0; 128];
|
|
match port.read(serial_buf.as_mut_slice()) {
|
|
Ok(t) => {
|
|
serial_buf.truncate(t);
|
|
println!("Data: {:?}", serial_buf);
|
|
|
|
let dev_msg = from_bytes_cobs::<DeviceMessage>(&mut serial_buf).unwrap();
|
|
println!("Message: {:?}", dev_msg);
|
|
}
|
|
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (),
|
|
Err(e) => eprintln!("{:?}", e),
|
|
}
|
|
|
|
|
|
thread::sleep(Duration::from_millis(1000));
|
|
}
|
|
|
|
}
|