修改UCD逻辑、添加关闭界面的x

This commit is contained in:
xinzhu.yin
2026-06-22 10:31:19 +08:00
parent 9c15751dd0
commit 22f8b12690
5 changed files with 294 additions and 52 deletions

View File

@@ -78,12 +78,21 @@ class ConnectionController:
# -- UCD 连接 ------------------------------------------------
def _pick_ucd_device(self, ucd_list: list[str], display: str) -> str | None:
"""Prefer devices not marked in-use; fall back to full list."""
usable = [d for d in ucd_list if "(in use)" not in d.lower()]
if display in usable:
return display
if usable:
return usable[0]
if display in ucd_list:
return display
return ucd_list[0] if ucd_list else None
def _refresh_ucd_selection(self, display: str) -> tuple[list[str], str | None]:
"""Rescan devices and return (device list, valid selection or None)."""
ucd_list = self.list_ucd_devices()
if display in ucd_list:
return ucd_list, display
return ucd_list, (ucd_list[0] if ucd_list else None)
return ucd_list, self._pick_ucd_device(ucd_list, display)
def _sync_ucd_combo(self, ucd_list: list[str], selected: str) -> None:
app = self._app
@@ -91,6 +100,19 @@ class ConnectionController:
app.ucd_list_var.set(selected)
app.update_config()
def _prepare_ucd_reconnect(self) -> None:
"""Release stale SDK/native handles before a new open attempt."""
if self._device.state.name != "CLOSED":
try:
self._device.close()
return
except Exception as exc: # noqa: BLE001
self._log(f"关闭旧 UCD 会话失败,将强制重置: {exc}", level="info")
try:
self._device.force_reset()
except Exception as exc: # noqa: BLE001
self._log(f"强制重置 UCD SDK 失败: {exc}", level="error")
def connect_ucd(self, display: str, *, rescan: bool = True) -> bool:
"""打开指定 UCD 设备。成功返回 True。"""
if rescan:
@@ -99,26 +121,48 @@ class ConnectionController:
self._log("未检测到 UCD 设备,请确认电源已打开后重试", level="error")
return False
if self._device.state.name != "CLOSED":
self._prepare_ucd_reconnect()
last_error: Exception | None = None
for attempt in range(2):
try:
self._device.close()
except Exception: # noqa: BLE001
pass
try:
self._device.open(DeviceInfo.parse(display))
return True
except UcdError as exc:
self._log(f"设备 {display} 异常UCD323 连接失败: {exc}", level="error")
return False
except Exception as exc: # noqa: BLE001
self._log(f"UCD323 连接异常: {exc}", level="error")
return False
self._device.open(DeviceInfo.parse(display))
return True
except UcdError as exc:
last_error = exc
self._log(
f"设备 {display} 连接失败 ({attempt + 1}/2): {exc}",
level="error",
)
except Exception as exc: # noqa: BLE001
last_error = exc
self._log(
f"UCD323 连接异常 ({attempt + 1}/2): {exc}",
level="error",
)
if attempt == 0:
try:
self._device.force_reset()
except Exception as exc: # noqa: BLE001
self._log(f"重连前强制重置 UCD 失败: {exc}", level="error")
if rescan:
_, display = self._refresh_ucd_selection(display)
if not display:
return False
if last_error is not None:
self._log(f"UCD323 重连失败: {last_error}", level="error")
return False
def disconnect_ucd(self) -> None:
try:
self._device.close()
except Exception: # noqa: BLE001
pass
try:
self._device.force_reset()
except Exception: # noqa: BLE001
pass
self._log("UCD连接已断开", level="info")
# -- CA 连接 -------------------------------------------------
@@ -297,9 +341,15 @@ class ConnectionController:
app = self._app
com_ports = self.list_com_ports()
ucd_list = self.list_ucd_devices()
if not ucd_list or all("(in use)" in d.lower() for d in ucd_list):
try:
self._device.force_reset()
except Exception as exc: # noqa: BLE001
self._log(f"刷新时重置 UCD SDK: {exc}", level="info")
ucd_list = self.list_ucd_devices()
if app.ucd_list_var.get() not in ucd_list:
app.ucd_list_var.set(ucd_list[0] if ucd_list else "")
selected = self._pick_ucd_device(ucd_list, app.ucd_list_var.get())
app.ucd_list_var.set(selected or "")
app.ucd_list_combo.config(values=ucd_list)
if app.ca_com_var.get() not in com_ports: