Last active
February 24, 2024 21:14
-
-
Save marcpabst/da547209087dd58b285f0b61a5f37a8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// setup serial port | |
let port_name = "/dev/cu.usbmodem21301"; | |
let mut port = serialport::new(port_name, 115_200) | |
.timeout(std::time::Duration::from_millis(10)) | |
.open() | |
.expect("Failed to open port"); | |
loop { | |
// start timer | |
let start = std::time::Instant::now(); | |
// send a byte and read the response | |
port.write(&[0x01]).unwrap(); | |
// flush the buffer | |
port.flush().unwrap(); | |
let mut response: Vec<u8> = vec![0; 1]; | |
port.read(response.as_mut_slice()).expect("Found no data!"); | |
let elapsed = start.elapsed(); | |
println!( | |
"Round trip time: {:?} (sent 0x01, received 0x{:02X})", | |
elapsed, response[0] | |
); | |
// busy wait in tight loop | |
let wait_ms = 50 - elapsed.as_millis() as u64; | |
while (start.elapsed().as_millis() as u64) < wait_ms { | |
// do nothing | |
} | |
// print actual time waited | |
println!("Waited for: {:?}", start.elapsed()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment