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