Skip to content

Commit

Permalink
fix: add a workaround to fix auto reconnect on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuswhite committed Aug 31, 2024
1 parent 007483f commit 7d7d506
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/serial/serial_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -315,14 +334,24 @@ impl SerialInterface {
serial: &mut Option<SerialPort>,
logger: &Logger,
plugin_engine_cmd_sender: &Sender<PluginEngineCommand>,
#[cfg(target_os = "windows")]
port_name: &mut String,
) -> Option<SerialMode> {
let mut has_changes = false;
let mut sw = shared
.write()
.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;
}

Expand Down

0 comments on commit 7d7d506

Please sign in to comment.