diff --git a/src/main.rs b/src/main.rs index 077f1b5b7c2dc385db4595feb6510cd01024c780..9f2359c9c6627b2b0a6d292daf9c4c4cb685c8e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ use std::time::Instant; use ffi::MavlinkPayloadFlightTM; use serialport::SerialPort; +const EOT: u8 = 0x04; + trait MavLinkPort { fn write_packet(&mut self, packet: MavlinkPayloadFlightTM); } @@ -52,19 +54,24 @@ fn main() { // read from buffer and print to stdout let mut buffer = [0u8; 1000]; - let mut buffered_string = String::new(); let mut start: Option<Instant> = None; let mut times = Vec::new(); let mut i = 0; loop { match port.read(&mut buffer) { Ok(t) => { - // encode the buffer into a string - let input = String::from_utf8_lossy(&buffer[..t]); + // check if we got the signal to start sending data + let eot_f = buffer[..t].contains(&EOT); + + // encode the buffer into a string (remove the EOT character) + let input = if eot_f { + String::from_utf8_lossy(&buffer[..t - 1]) + } else { + String::from_utf8_lossy(&buffer[..t]) + }; + // if we got the signal to start sending data, send it - buffered_string.push_str(&input); - if buffered_string.contains("[WAITING]") { - buffered_string.clear(); + if eot_f { if let Some(packet) = iter.next() { if let Some(t) = start { times.push(t.elapsed().as_millis());