diff --git a/on-host/src/cli.rs b/on-host/src/cli.rs index a1a74831b2fdc2010261bc24ed4d9bcc881abeb5..267efbfb52c998e9c2d5cce81c07001bb243853f 100644 --- a/on-host/src/cli.rs +++ b/on-host/src/cli.rs @@ -21,6 +21,11 @@ pub struct Cli { /// baud rate #[clap(short, long, default_value = "115200")] pub baud_rate: u32, + + /// SYNC-MODE: for each packet sent wait for a signal from the device before + /// sending the next one + #[clap(short, long)] + pub sync: bool, } pub fn get_styles() -> Styles { diff --git a/on-host/src/main.rs b/on-host/src/main.rs index 96d885f255631481d92b4feaba8ffabb004e679d..7eb4875950cd26e92e56fed0f1f0fc7c6a707916 100644 --- a/on-host/src/main.rs +++ b/on-host/src/main.rs @@ -81,33 +81,41 @@ fn main() { packets.total_packets() ); - // wait ACK - loop { - match port.read(&mut buffer) { - Ok(t) => { - // eprint!("{}", String::from_utf8_lossy(&buffer[..t])); - // if didn't get the signal then print the buffer and exit with error - if buffer[..t].contains(&ACK) { - break; - } else { - warn!("didn't get the signal"); - std::thread::sleep(Duration::from_millis(10)); - } - } - Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), - Err(e) => { - error!("FATAL: {:?}", e); - std::process::exit(1); - } - } + // if sync mode is enabled then wait for ACK + if args.sync { + wait_for_ack(&mut port, &mut buffer); } - // eprintln!(); } info!("sent {} packets", packets.sent_packets()); print_stats(times); } +/// Wait for ACK signal from the device +fn wait_for_ack(port: &mut Box<dyn SerialPort>, buffer: &mut [u8]) { + // wait ACK + loop { + match port.read(buffer) { + Ok(t) => { + // eprint!("{}", String::from_utf8_lossy(&buffer[..t])); + // if didn't get the signal then print the buffer and exit with error + if buffer[..t].contains(&ACK) { + break; + } else { + warn!("didn't get the signal"); + std::thread::sleep(Duration::from_millis(10)); + } + } + Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), + Err(e) => { + error!("FATAL: {:?}", e); + std::process::exit(1); + } + } + } + // eprintln!(); +} + trait MavLinkPort { fn write_packet(&mut self, packet: MavMessage); }