Compare commits
4 commits
dea7db543a
...
a8bfb8cd8a
Author | SHA1 | Date | |
---|---|---|---|
a8bfb8cd8a | |||
38ca539af8 | |||
09c1fe5982 | |||
60c0c862d9 |
2 changed files with 19 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
|||
use std::{f32::NAN, io::{BufRead, BufReader, BufWriter, Read, Write}, net::TcpListener, sync::{Arc, Mutex}, thread::sleep, time::Duration};
|
||||
use std::{io::{BufRead, BufReader, BufWriter, Read, Write}, net::TcpListener, sync::{Arc, Mutex}, thread::sleep, time::Duration};
|
||||
|
||||
use common::{Command, Telemetry};
|
||||
use postcard::to_vec_cobs;
|
||||
|
@ -32,7 +32,10 @@ fn main() {
|
|||
loop {
|
||||
let command = command.lock().unwrap().clone();
|
||||
let encoded: Vec<u8, 20> = to_vec_cobs(&command).unwrap();
|
||||
port.write(&encoded).expect("port write fail");
|
||||
if port.write(&encoded).is_err() {
|
||||
println!("disconnected from robot");
|
||||
return;
|
||||
};
|
||||
|
||||
sleep(Duration::from_millis(200));
|
||||
}
|
||||
|
@ -73,6 +76,14 @@ fn handle_connection(command: Arc<Mutex<Command>>, telemetry: Arc<Mutex<Telemetr
|
|||
let mut data_buf = std::vec::Vec::new();
|
||||
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;};
|
||||
|
||||
*command.lock().unwrap() = new_command.clone();
|
||||
|
@ -86,7 +97,7 @@ fn handle_connection(command: Arc<Mutex<Command>>, telemetry: Arc<Mutex<Telemetr
|
|||
println!("sent d{}", telem.distance);
|
||||
|
||||
serde_json::to_writer(&mut writer, &telem).unwrap();
|
||||
writer.write(&[b'\r',b'\n'])?;
|
||||
writer.write_all(b"\r\n")?;
|
||||
writer.flush()?;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,13 +4,18 @@ use anyhow::Result;
|
|||
fn main() -> Result<()> {
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Data {
|
||||
boolean: bool,
|
||||
string: String,
|
||||
number: u8,
|
||||
#[serde(rename = "numbers")]
|
||||
array: [u8; 4],
|
||||
}
|
||||
|
||||
let data = Data {
|
||||
boolean: false,
|
||||
string: String::from("data"),
|
||||
number: 42,
|
||||
array: [0,1,2,3],
|
||||
};
|
||||
|
||||
println!("debug: {:?}", data);
|
||||
|
|
Loading…
Reference in a new issue