Hello, I am new to rust, I am trying to write a Tauri app that reads data from the serial port, writes it to a file. This much I have accomplished, but the data is only written to the file immediately if I have a buffer size of 1. I am receiving data from a proprietary radio and I was told that a buffer size of one will be too slow to keep up.
The second I make this buffer size any bigger, it waits until a certain amount of new bytes have come in before writing to the file (not even the same size as the buffer), so I always get data cutoff, until the next burst of data comes in from the serialport.
Any help is greatly appreciated!
TLDR; How do I make this code write to a file immediately even with a buffer size of [0,1024] (without waiting for the buffer to be full).
#[tauri::command]
fn read_from_port(port_name: String, baud_rate: u32) -> Result<(), String> {
let running = RUNNING.clone();
running.store(true, Ordering::Relaxed);
thread::spawn(move || {
let mut port = match serialport::new(&port_name, baud_rate).open() {
Ok(port) => port,
Err(e) => {
println!("Error opening port: {}", e);
return;
}
};
let output_path = Path::new("A:/tauri/tauri-dev/output.txt");
let mut file = match OpenOptions::new().write(true).append(true).open(&output_path) {
Ok(file) => file,
Err(e) => {
println!("Error opening file: {}", e);
return;
}
};
let mut small_buffer: [u8; 1] = [0; 1];
loop {
if !running.load(Ordering::Relaxed) {
break;
}
match port.read(&mut small_buffer) {
Ok(n) => {
if let Err(e) = file.write_all(&small_buffer[..n]) {
println!("Error writing to file: {}", e);
return;
}
},
Err(e) => {
println!("Error reading from port: {}", e);
return;
}
}
}
});
Ok(())
}