Work around RTS/DTR serial reset issues on Windows

Some devices (e.g. Heltec V3.1) are reset when the RTS or DTR pins are
messed with, which means that without workarounds, Meshtastic CLI will
cause a reset with any operation that opens the serial port.

This behavior is documented in this PySerial issue:
https://github.com/pyserial/pyserial/issues/124

On Linux, we already handle this by disabling the HUPCL termios flag,
which has the effect of preventing the offending lines being toggled. On
Windows, setting the initial state of RTS and DTR before opening the
port has a similar effect.

Implement this workaround so that Meshtastic CLI can be used on Windows
with these devices.
This commit is contained in:
Dale Whinham
2025-03-20 13:14:58 +00:00
parent 32a61b0209
commit bc1664dade

View File

@@ -46,7 +46,12 @@ class SerialInterface(StreamInterface):
logging.debug(f"Connecting to {self.devPath}")
# first we need to set the HUPCL so the device will not reboot based on RTS and/or DTR
# set port to None to prevent automatically opening
self.stream = serial.Serial(
port=None, baudrate=115200, exclusive=True, timeout=0.5, write_timeout=0
)
# first we need to clear HUPCL (UNIX) or clear RTS/DTR (Windows) so the device will not reboot based on RTS and/or DTR
# see https://github.com/pyserial/pyserial/issues/124
if platform.system() != "Windows":
with open(self.devPath, encoding="utf8") as f:
@@ -55,10 +60,14 @@ class SerialInterface(StreamInterface):
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
f.close()
time.sleep(0.1)
else:
self.stream.rts = 0
self.stream.dtr = 0
# set proper port and open now that we've worked-around RTS/DTR issues
self.stream.port = self.devPath
self.stream.open()
self.stream = serial.Serial(
self.devPath, 115200, exclusive=True, timeout=0.5, write_timeout=0
)
self.stream.flush() # type: ignore[attr-defined]
time.sleep(0.1)