From 7d7d506304b73d09d593522dba2fd632987017e2 Mon Sep 17 00:00:00 2001 From: "Matheus T. dos Santos" Date: Sat, 31 Aug 2024 13:03:10 -0300 Subject: [PATCH] fix: add a workaround to fix auto reconnect on windows --- src/serial/serial_if.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/serial/serial_if.rs b/src/serial/serial_if.rs index 8d7c65e..f18c545 100644 --- a/src/serial/serial_if.rs +++ b/src/serial/serial_if.rs @@ -125,6 +125,8 @@ impl SerialInterface { let mut buffer = [0u8]; let mut serial = None; let mut now = Instant::now(); + #[cfg(target_os = "windows")] + let mut port_name = String::new(); 'task_loop: loop { if let Ok(cmd) = cmd_receiver.try_recv() { @@ -148,6 +150,8 @@ impl SerialInterface { &mut serial, &logger, &plugin_engine_cmd_sender, + #[cfg(target_os = "windows")] + &mut port_name, ), }; Self::set_mode(shared.clone(), new_mode); @@ -188,6 +192,21 @@ impl SerialInterface { } } + #[cfg(target_os = "windows")] + if let Ok(ports) = serialport::available_ports() { + if !ports.into_iter().any(|info| info.port_name == port_name) { + let _ = Self::disconnect( + shared.clone(), + &mut Some(ser), + &logger, + &plugin_engine_cmd_sender, + ); + Self::set_mode(shared.clone(), Some(SerialMode::Reconnecting)); + std::thread::yield_now(); + continue 'task_loop; + } + } + match ser.read(&mut buffer) { Ok(_) => { now = Instant::now(); @@ -315,6 +334,8 @@ impl SerialInterface { serial: &mut Option, logger: &Logger, plugin_engine_cmd_sender: &Sender, + #[cfg(target_os = "windows")] + port_name: &mut String, ) -> Option { let mut has_changes = false; let mut sw = shared @@ -322,7 +343,15 @@ impl SerialInterface { .expect("Cannot get serial shared lock for write"); if let Some(port) = setup.port { - sw.port = port; + #[cfg(not(target_os = "windows"))] + { + sw.port = port; + } + #[cfg(target_os = "windows")] + { + sw.port = port.clone(); + *port_name = port; + } has_changes = true; }