From 634d1c17e4666934e57e6882af7f16518276c55f Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Fri, 13 Oct 2023 10:31:31 +0200 Subject: [PATCH] feat: Read and Write to serial at the same time --- toos-dashboard/src-tauri/src/main.rs | 67 +++++++++++----------------- 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/toos-dashboard/src-tauri/src/main.rs b/toos-dashboard/src-tauri/src/main.rs index ac73ff0..3705e64 100644 --- a/toos-dashboard/src-tauri/src/main.rs +++ b/toos-dashboard/src-tauri/src/main.rs @@ -3,7 +3,6 @@ use std::sync:: Mutex; use std::io::ErrorKind; -use std::thread; use std::time::Duration; use std::collections::HashMap; use tauri::State; @@ -11,56 +10,20 @@ use tauri::State; #[derive(Default)] struct PortMap(Mutex>>); -#[tauri::command] -async fn read_serial(port_name: &str, port_map: State<'_, PortMap>) -> Result<(), String> { - match port_map.0.lock().unwrap().get(port_name) { - Some(port) => { - let mut clone = port.try_clone().expect("Failed to clone"); - let mut buffer: Vec = vec![0; 1024]; - println!("Reading serial"); - 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()); - } - } - }, - 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(()) - } - } -} - #[tauri::command] async fn write_serial(input: &str, port_name: &str, port_map: State<'_, PortMap>) -> Result<(), String> { - println!("Trying to write"); match port_map.0.lock().unwrap().get(port_name) { Some(port) => { let mut clone = port.try_clone().expect("Failed to clone"); println!("Writing to serial"); match clone.write(input.as_bytes()) { Ok(_) => { - println!("Writing to serial succes"); Ok(()) }, Err(err) => Err(err.to_string()), } } None => { - println!("Writing to serial not found"); Ok(()) } } @@ -79,22 +42,42 @@ fn get_serial_ports() -> Vec { } #[tauri::command] -async fn open_port(app_handle: tauri::AppHandle, port_map: State<'_, PortMap>) -> Result<(), String> { +async fn open_port(_app_handle: tauri::AppHandle, port_map: State<'_, PortMap>) -> Result<(), String> { match serialport::new("/dev/ttyUSB0", 9600).timeout(Duration::from_millis(10)).open() { Ok(port) => { - port_map.0.lock().unwrap().insert("/dev/ttyUSB0".to_string(), port); - println!("Port open"); + port_map.0.lock().unwrap().insert("/dev/ttyUSB0".to_string(), port.try_clone().expect("Error cloning")); + + println!("Port is open"); + + let mut clone = port.try_clone().expect("Failed to clone"); + let mut buffer: Vec = vec![0; 1024]; + + loop { + 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()); + } + } + }, + Err(ref e) if e.kind() == ErrorKind::TimedOut => (), + Err(e) => return Err(e.to_string()), + }; + + } }, Err(err) => return Err(err.to_string()), }; - Ok(()) } fn main() { tauri::Builder::default() .manage(PortMap::default()) - .invoke_handler(tauri::generate_handler![get_serial_ports, read_serial, write_serial, open_port]) + .invoke_handler(tauri::generate_handler![get_serial_ports, write_serial, open_port]) .run(tauri::generate_context!()) .expect("error while running tauri application"); }