Revert "fix: try both COM11 and \\.\COM11 formats when opening serial port"

This reverts commit 36fbb6a662.
This commit is contained in:
2026-06-12 14:18:49 +08:00
parent ef685d6657
commit 03bc5d5268
3 changed files with 7 additions and 37 deletions
+1 -2
View File
@@ -1097,8 +1097,7 @@ class UartMonitorWindow(ctk.CTkToplevel):
def _bg_open() -> None: def _bg_open() -> None:
try: try:
import serial import serial
from serial_monitor import _open_serial ser = serial.Serial(self._port, self._baudrate, timeout=1)
ser = _open_serial(self._port, self._baudrate, 1)
ser.close() ser.close()
self.after(0, self._on_open_success) self.after(0, self._on_open_success)
except Exception as e: except Exception as e:
+2 -3
View File
@@ -125,12 +125,11 @@ def reboot_via_serial(
try: try:
import serial import serial
from serial_monitor import _open_serial
with _open_serial( with serial.Serial(
config.serial_port, config.serial_port,
config.serial_baudrate, config.serial_baudrate,
timeout=5.0, timeout=5,
) as ser: ) as ser:
ser.reset_input_buffer() ser.reset_input_buffer()
# Send Ctrl+C to break out of any running process # Send Ctrl+C to break out of any running process
+4 -32
View File
@@ -8,7 +8,6 @@ Provides UartMonitor for continuous background monitoring.
from __future__ import annotations from __future__ import annotations
import re import re
import sys
import threading import threading
import time import time
from dataclasses import dataclass from dataclasses import dataclass
@@ -18,33 +17,6 @@ import serial
import serial.tools.list_ports import serial.tools.list_ports
def _open_serial(port: str, baudrate: int, timeout: float = 1.0) -> serial.Serial:
"""Open a serial port, trying both name formats on Windows.
pyserial normally adds \\\\.\\COM prefix for COM>=10, but some
versions don't. Try raw name first, then explicit NT namespace.
"""
port = port.strip()
last_err = None
# Try 1: port name as-is
try:
return serial.Serial(port, baudrate, timeout=timeout)
except serial.SerialException as e:
last_err = e
# Try 2: Windows NT namespace prefix
if sys.platform == "win32":
m = re.match(r"^(COM\d+)$", port, re.IGNORECASE)
if m:
try:
return serial.Serial(r"\\.\%s" % port, baudrate, timeout=timeout)
except serial.SerialException as e:
last_err = e
raise last_err or OSError(f"Cannot open serial port: {port}")
@dataclass @dataclass
class BootInfo: class BootInfo:
"""Parsed information extracted from Zynq boot output.""" """Parsed information extracted from Zynq boot output."""
@@ -197,7 +169,7 @@ def check_uart_available(
# 3. Try to open and read # 3. Try to open and read
try: try:
with _open_serial(port, baudrate, timeout) as ser: with serial.Serial(port, baudrate, timeout=timeout) as ser:
ser.reset_input_buffer() ser.reset_input_buffer()
lines: list[str] = [] lines: list[str] = []
while ser.in_waiting: while ser.in_waiting:
@@ -271,7 +243,7 @@ def test_serial_version(
import serial import serial
try: try:
with _open_serial(port, baudrate, timeout) as ser: with serial.Serial(port, baudrate, timeout=timeout) as ser:
ser.reset_input_buffer() ser.reset_input_buffer()
ser.reset_output_buffer() ser.reset_output_buffer()
@@ -412,7 +384,7 @@ class UartMonitor:
# Quick port check before starting thread # Quick port check before starting thread
try: try:
test_ser = _open_serial(self._port, self._baudrate, 1) test_ser = serial.Serial(self._port, self._baudrate, timeout=1)
test_ser.close() test_ser.close()
except serial.SerialException as e: except serial.SerialException as e:
if self.on_error: if self.on_error:
@@ -438,7 +410,7 @@ class UartMonitor:
def _read_loop(self) -> None: def _read_loop(self) -> None:
"""Main read loop running in background thread.""" """Main read loop running in background thread."""
try: try:
ser = _open_serial( ser = serial.Serial(
self._port, self._port,
self._baudrate, self._baudrate,
timeout=self._timeout, timeout=self._timeout,