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,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 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");
match clone.read(buffer.as_mut_slice()) { loop {
Ok(bytes_read) => { println!(".");
if bytes_read > 0 { match clone.read(buffer.as_mut_slice()) {
let data = &buffer[..bytes_read]; Ok(bytes_read) => {
let data = String::from_utf8_lossy(data).to_string(); if bytes_read > 0 {
if !data.trim().is_empty() { let data = &buffer[..bytes_read];
println!("{}", data.trim()); 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(e) => return Err(e.to_string()),
Err(ref e) if e.kind() == ErrorKind::TimedOut => Err(String::from("Timed out")), };
Err(e) => 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()),