From 590d34d08cd2eb15464a5f2c93d582cd90e50cf7 Mon Sep 17 00:00:00 2001 From: SlenderData Date: Fri, 2 Oct 2026 00:28:44 +0800 Subject: [PATCH] fix(device): de-assert RTS before DTR after opening serial port On macOS (and other non-Windows platforms) the OS raises DTR and RTS when the port is opened. SerialPort then drops them DTR-first, which briefly leaves RTS high with DTR low; through the auto-download circuit on many ESP32 boards this pulls EN low and resets the chip on every connect, so the hello sent right after opening was lost and the handshake always timed out (#56). On non-Windows platforms, open the port with DTR/RTS asserted and then drop RTS before DTR. The final line state is unchanged (both low), but the reset-triggering intermediate state is avoided. Windows behaviour is left untouched. --- src/EasyCon.Device/Connection/TTLSerialClient.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/EasyCon.Device/Connection/TTLSerialClient.cs b/src/EasyCon.Device/Connection/TTLSerialClient.cs index 82dd84a5..97b80d82 100644 --- a/src/EasyCon.Device/Connection/TTLSerialClient.cs +++ b/src/EasyCon.Device/Connection/TTLSerialClient.cs @@ -57,6 +57,14 @@ public override void Connect() return; _sport = new SerialPort(_connStr, _port); + // 非 Windows 平台打开串口时系统会先拉高 DTR/RTS,若随后先撤销 DTR,会短暂出现 + // “RTS 高、DTR 低”,经 ESP32 等板子的自动下载电路拉低 EN 导致单片机复位。 + // 因此以拉高状态打开,再在 Open 后按“先 RTS、后 DTR”的顺序撤销(见 Loop)。 + if (!OperatingSystem.IsWindows()) + { + _sport.DtrEnable = true; + _sport.RtsEnable = true; + } source = new CancellationTokenSource(); var token = source.Token; @@ -88,6 +96,11 @@ void Loop() { byte[] inBuffer = new byte[2550]; _sport.Open(); + if (!OperatingSystem.IsWindows()) + { + _sport.RtsEnable = false; + _sport.DtrEnable = false; + } _sport.DiscardInBuffer(); _sport.DiscardOutBuffer(); var stream = _sport.BaseStream;