use std::time::Duration; use serialport::SerialPortInfo; fn get_port(ports: Vec) -> Option { for p in ports { if p.port_name.contains("ttyACM") { return Some(p); } } None } pub fn read_messages(mut request: curl::easy::Easy, data: &str) { let ports = serialport::available_ports().expect("No ports found!"); let r = get_port(ports).unwrap().port_name; let mut connection = serialport::new(r, 9600) .timeout(Duration::from_millis(1000)) .open().expect("Failed to open the port"); let mut serial_buf: Vec = vec![0; 1024]; loop { match connection.read(serial_buf.as_mut_slice()) { Ok(t) => { if t < 1 { continue; } let output = String::from_utf8_lossy(&serial_buf[..t]); if output.contains("1") { // send curl request let res = crate::curl::request(&mut request, &data); match res { Ok(t) => println!("Response code: {t}"), Err(e) => eprintln!("[ERROR] Something went wrong with the CURL request:\n{e}"), } } }, Err(_) => {} } } }