feat: Read and Write to serial at the same time
This commit is contained in:
parent
e9e2650596
commit
634d1c17e4
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
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;
|
||||||
@ -11,56 +10,20 @@ use tauri::State;
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct PortMap(Mutex<HashMap<String, Box<dyn serialport::SerialPort>>>);
|
struct PortMap(Mutex<HashMap<String, Box<dyn serialport::SerialPort>>>);
|
||||||
|
|
||||||
#[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<u8> = 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]
|
#[tauri::command]
|
||||||
async fn write_serial(input: &str, port_name: &str, port_map: State<'_, PortMap>) -> Result<(), String> {
|
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) {
|
match port_map.0.lock().unwrap().get(port_name) {
|
||||||
Some(port) => {
|
Some(port) => {
|
||||||
let mut clone = port.try_clone().expect("Failed to clone");
|
let mut clone = port.try_clone().expect("Failed to clone");
|
||||||
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()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
println!("Writing to serial not found");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,22 +42,42 @@ fn get_serial_ports() -> Vec<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[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() {
|
match serialport::new("/dev/ttyUSB0", 9600).timeout(Duration::from_millis(10)).open() {
|
||||||
Ok(port) => {
|
Ok(port) => {
|
||||||
port_map.0.lock().unwrap().insert("/dev/ttyUSB0".to_string(), port);
|
port_map.0.lock().unwrap().insert("/dev/ttyUSB0".to_string(), port.try_clone().expect("Error cloning"));
|
||||||
println!("Port open");
|
|
||||||
|
println!("Port is open");
|
||||||
|
|
||||||
|
let mut clone = port.try_clone().expect("Failed to clone");
|
||||||
|
let mut buffer: Vec<u8> = 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()),
|
Err(err) => return Err(err.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.manage(PortMap::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!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user