Skip to content
Snippets Groups Projects
Commit 090b7d8d authored by Federico Lolli's avatar Federico Lolli
Browse files

fixed workflow and implemented RTC and CTS control over communication availability

parent b08226fe
No related branches found
No related tags found
No related merge requests found
...@@ -2,17 +2,17 @@ mod ffi; ...@@ -2,17 +2,17 @@ mod ffi;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::{Read, Write}; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use clap::Parser; use clap::Parser;
use log::{debug, error, info, trace}; use log::{debug, error, info, warn};
use serialport::SerialPort; use serialport::SerialPort;
use ffi::MavlinkPayloadFlightTM; use ffi::MavlinkPayloadFlightTM;
const EOT: u8 = 0x04; const ACK: u8 = 0x06;
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Cli { struct Cli {
...@@ -72,15 +72,16 @@ fn main() { ...@@ -72,15 +72,16 @@ fn main() {
let mut buffer = [0u8; 1000]; let mut buffer = [0u8; 1000];
let mut start: Option<Instant> = None; let mut start: Option<Instant> = None;
let mut times = Vec::new(); let mut times = Vec::new();
loop { loop {
match port.read(&mut buffer) { // wait clear to send
Ok(t) => { port.write_request_to_send(true).expect("Unable to set RTS");
// check if we got the signal to start sending data while !port.read_clear_to_send().expect("Unable to read CTS") {
let eot_f = buffer[..t].contains(&EOT); std::thread::sleep(Duration::from_millis(100));
debug!("waiting for CTS");
}
// if we got the signal to start sending data, send it // send packet if there is one
if eot_f {
trace!("got EOT, sending data");
if let Some(packet) = packets.wait_next() { if let Some(packet) = packets.wait_next() {
if let Some(t) = start { if let Some(t) = start {
times.push(t.elapsed().as_millis()); times.push(t.elapsed().as_millis());
...@@ -88,13 +89,31 @@ fn main() { ...@@ -88,13 +89,31 @@ fn main() {
start = Some(Instant::now()); start = Some(Instant::now());
write_port.write_packet(packet.to_owned()); write_port.write_packet(packet.to_owned());
debug!("sent packet ({}/{})", packets.index, packets.packets.len()); debug!("sent packet ({}/{})", packets.index, packets.packets.len());
port.write_request_to_send(false)
.expect("Unable to set RTS");
} else { } else {
break; break;
} }
// wait ACK
loop {
match port.read(&mut buffer) {
Ok(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(100));
// eprintln!("{}", String::from_utf8_lossy(&buffer[..t]));
} }
} }
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (),
Err(e) => error!("{:?}", e), Err(e) => {
error!("FATAL: {:?}", e);
std::process::exit(1);
}
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment