1
Fork 0

Compare commits

..

No commits in common. "a8bfb8cd8a333d3d06cd93e913aac3479e51a1da" and "dea7db543a1e3f5ec8abc9b826e279614b3ebb9a" have entirely different histories.

2 changed files with 3 additions and 19 deletions

View file

@ -1,4 +1,4 @@
use std::{io::{BufRead, BufReader, BufWriter, Read, Write}, net::TcpListener, sync::{Arc, Mutex}, thread::sleep, time::Duration}; use std::{f32::NAN, io::{BufRead, BufReader, BufWriter, Read, Write}, net::TcpListener, sync::{Arc, Mutex}, thread::sleep, time::Duration};
use common::{Command, Telemetry}; use common::{Command, Telemetry};
use postcard::to_vec_cobs; use postcard::to_vec_cobs;
@ -32,10 +32,7 @@ fn main() {
loop { loop {
let command = command.lock().unwrap().clone(); let command = command.lock().unwrap().clone();
let encoded: Vec<u8, 20> = to_vec_cobs(&command).unwrap(); let encoded: Vec<u8, 20> = to_vec_cobs(&command).unwrap();
if port.write(&encoded).is_err() { port.write(&encoded).expect("port write fail");
println!("disconnected from robot");
return;
};
sleep(Duration::from_millis(200)); sleep(Duration::from_millis(200));
} }
@ -76,14 +73,6 @@ fn handle_connection(command: Arc<Mutex<Command>>, telemetry: Arc<Mutex<Telemetr
let mut data_buf = std::vec::Vec::new(); let mut data_buf = std::vec::Vec::new();
let len = reader.read_until(b'\n', &mut data_buf)?; let len = reader.read_until(b'\n', &mut data_buf)?;
// eof
if len == 0 {
*command.lock().unwrap() = Command::default(); // stop motors
println!("disconnected from LabVIEW");
return anyhow::Ok(());
}
let Ok(new_command) = serde_json::from_slice::<Command>(&data_buf[0..len]) else {continue;}; let Ok(new_command) = serde_json::from_slice::<Command>(&data_buf[0..len]) else {continue;};
*command.lock().unwrap() = new_command.clone(); *command.lock().unwrap() = new_command.clone();
@ -97,7 +86,7 @@ fn handle_connection(command: Arc<Mutex<Command>>, telemetry: Arc<Mutex<Telemetr
println!("sent d{}", telem.distance); println!("sent d{}", telem.distance);
serde_json::to_writer(&mut writer, &telem).unwrap(); serde_json::to_writer(&mut writer, &telem).unwrap();
writer.write_all(b"\r\n")?; writer.write(&[b'\r',b'\n'])?;
writer.flush()?; writer.flush()?;
} }
} }

View file

@ -4,18 +4,13 @@ use anyhow::Result;
fn main() -> Result<()> { fn main() -> Result<()> {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct Data { struct Data {
boolean: bool,
string: String, string: String,
number: u8, number: u8,
#[serde(rename = "numbers")]
array: [u8; 4],
} }
let data = Data { let data = Data {
boolean: false,
string: String::from("data"), string: String::from("data"),
number: 42, number: 42,
array: [0,1,2,3],
}; };
println!("debug: {:?}", data); println!("debug: {:?}", data);