This commit is contained in:
Xeovalyte 2023-10-13 10:21:08 +02:00
parent 4ae2e63add
commit e9e2650596

View File

@ -3,6 +3,7 @@
use std::sync:: Mutex; use std::sync:: Mutex;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::thread;
use std::time::Duration; use std::time::Duration;
use std::collections::HashMap; use std::collections::HashMap;
use tauri::State; use tauri::State;
@ -17,6 +18,8 @@ async fn read_serial(port_name: &str, port_map: State<'_, PortMap>) -> Result<()
let mut clone = port.try_clone().expect("Failed to clone"); let mut clone = port.try_clone().expect("Failed to clone");
let mut buffer: Vec<u8> = vec![0; 1024]; let mut buffer: Vec<u8> = vec![0; 1024];
println!("Reading serial"); println!("Reading serial");
loop {
println!(".");
match clone.read(buffer.as_mut_slice()) { match clone.read(buffer.as_mut_slice()) {
Ok(bytes_read) => { Ok(bytes_read) => {
if bytes_read > 0 { if bytes_read > 0 {
@ -26,13 +29,16 @@ async fn read_serial(port_name: &str, port_map: State<'_, PortMap>) -> Result<()
println!("{}", data.trim()); println!("{}", data.trim());
} }
} }
Ok(())
}, },
Err(ref e) if e.kind() == ErrorKind::TimedOut => Err(String::from("Timed out")), Err(ref e) if e.kind() == ErrorKind::TimedOut => (),
Err(e) => Err(e.to_string()), Err(e) => return Err(e.to_string()),
};
thread::sleep(std::time::Duration::from_millis(100));
} }
} }
None => { None => {
println!("Port not found");
Ok(()) Ok(())
} }
} }
@ -47,6 +53,7 @@ async fn write_serial(input: &str, port_name: &str, port_map: State<'_, PortMap>
println!("Writing to serial"); println!("Writing to serial");
match clone.write(input.as_bytes()) { match clone.write(input.as_bytes()) {
Ok(_) => { Ok(_) => {
println!("Writing to serial succes");
Ok(()) Ok(())
}, },
Err(err) => Err(err.to_string()), Err(err) => Err(err.to_string()),