修改UCD连接需要重启错误

This commit is contained in:
xinzhu.yin
2026-06-16 14:31:00 +08:00
parent dc096a9805
commit 3dd383b982
5 changed files with 81 additions and 22 deletions

View File

@@ -231,6 +231,12 @@ class BaseIO:
self.__ref_count = TSI_Clean() self.__ref_count = TSI_Clean()
logging.info(f"[UniTAP] BaseIO.del: {self}; Ref count: {self.__ref_count}") logging.info(f"[UniTAP] BaseIO.del: {self}; Ref count: {self.__ref_count}")
def rescan_devices(self):
if self.__ref_count > 0:
TSIX_DEV_RescanDevices()
return
logging.error("[UniTAP] BaseIO.rescan_devices call without TSI_Init")
def get_device_count(self) -> int: def get_device_count(self) -> int:
if self.__ref_count > 0: if self.__ref_count > 0:
return TSIX_DEV_GetDeviceCount() return TSIX_DEV_GetDeviceCount()

View File

@@ -70,6 +70,11 @@ class TsiLib:
self.__io.cleanup() self.__io.cleanup()
logging.info(f"[UniTAP] TsiLib.cleanup {self}") logging.info(f"[UniTAP] TsiLib.cleanup {self}")
def rescan_devices(self):
"""Rescan USB for newly connected TSI devices without tearing down the library."""
self.__io.rescan_devices()
logging.info(f"[UniTAP] TsiLib.rescan_devices {self}")
def __del__(self): def __del__(self):
""" """
Automatically call the destructor after the script completes. Automatically call the destructor after the script completes.

View File

@@ -78,8 +78,27 @@ class ConnectionController:
# -- UCD 连接 ------------------------------------------------ # -- UCD 连接 ------------------------------------------------
def connect_ucd(self, display: str) -> bool: 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)
def _sync_ucd_combo(self, ucd_list: list[str], selected: str) -> None:
app = self._app
app.ucd_list_combo.config(values=ucd_list)
app.ucd_list_var.set(selected)
app.update_config()
def connect_ucd(self, display: str, *, rescan: bool = True) -> bool:
"""打开指定 UCD 设备。成功返回 True。""" """打开指定 UCD 设备。成功返回 True。"""
if rescan:
_, display = self._refresh_ucd_selection(display)
if not display:
self._log("未检测到 UCD 设备,请确认电源已打开后重试", level="error")
return False
if self._device.state.name != "CLOSED": if self._device.state.name != "CLOSED":
try: try:
self._device.close() self._device.close()
@@ -158,7 +177,11 @@ class ConnectionController:
def worker(): def worker():
try: try:
ucd_ok = self.connect_ucd(app.ucd_list_var.get()) ucd_list, selected = self._refresh_ucd_selection(app.ucd_list_var.get())
app._dispatch_ui(self._sync_ucd_combo, ucd_list, selected or "")
ucd_ok = False
if selected:
ucd_ok = self.connect_ucd(selected, rescan=False)
app._dispatch_ui( app._dispatch_ui(
app.update_connection_indicator, app.update_connection_indicator,
app.ucd_status_indicator, ucd_ok, app.ucd_status_indicator, ucd_ok,
@@ -185,7 +208,19 @@ class ConnectionController:
def worker(): def worker():
try: try:
ucd_ok = self.connect_ucd(app.ucd_list_var.get()) ucd_list, selected = self._refresh_ucd_selection(app.ucd_list_var.get())
app._dispatch_ui(self._sync_ucd_combo, ucd_list, selected or "")
if not selected:
app._dispatch_ui(
app.update_connection_indicator,
app.ucd_status_indicator,
False,
)
app._dispatch_ui(app.status_var.set, "未检测到 UCD 设备")
app._dispatch_ui(self._enable_widgets)
return
ucd_ok = self.connect_ucd(selected, rescan=False)
app._dispatch_ui( app._dispatch_ui(
app.update_connection_indicator, app.update_connection_indicator,
app.ucd_status_indicator, app.ucd_status_indicator,

View File

@@ -111,6 +111,12 @@ class _UcdSdkBackend:
def search_device(self): def search_device(self):
"""搜索可用设备""" """搜索可用设备"""
self._ensure_tsi_lib()
try:
self.lUniTAP.rescan_devices()
except Exception:
log.exception("UcdSdk.search_device rescan failed; reinitializing TsiLib")
self._reinit_tsi_lib()
available_devices = self.lUniTAP.get_list_of_available_devices() available_devices = self.lUniTAP.get_list_of_available_devices()
return available_devices if available_devices else [] return available_devices if available_devices else []
@@ -122,6 +128,13 @@ class _UcdSdkBackend:
if self.dev is not None or self.status: if self.dev is not None or self.status:
self._force_cleanup() self._force_cleanup()
self._ensure_tsi_lib()
try:
self.lUniTAP.rescan_devices()
except Exception:
log.exception("UcdSdk.open rescan failed; reinitializing TsiLib")
self._reinit_tsi_lib()
device_id = int(device_name.split(":")[0]) device_id = int(device_name.split(":")[0])
temp_dev = self.lUniTAP.open(device_id) temp_dev = self.lUniTAP.open(device_id)
@@ -144,6 +157,7 @@ class _UcdSdkBackend:
except Exception as e: except Exception as e:
self._force_cleanup() self._force_cleanup()
self._reinit_tsi_lib()
return False return False
def close(self): def close(self):
@@ -157,29 +171,28 @@ class _UcdSdkBackend:
self._close_device_object(self.dev) self._close_device_object(self.dev)
self._reset_state() self._reset_state()
self.lUniTAP = None self._reinit_tsi_lib()
for i in range(3):
gc.collect()
time.sleep(2.0)
self.lUniTAP = UniTAP.TsiLib()
return True return True
except Exception as e: except Exception as e:
self._reset_state() self._reset_state()
try: try:
self._reinit_tsi_lib()
except Exception:
pass
return False
def _ensure_tsi_lib(self) -> None:
if self.lUniTAP is None:
self.lUniTAP = UniTAP.TsiLib()
def _reinit_tsi_lib(self) -> None:
"""Destroy and recreate TsiLib so the native driver rescans USB devices."""
self.lUniTAP = None self.lUniTAP = None
for _ in range(3):
gc.collect() gc.collect()
time.sleep(2.0) time.sleep(2.0)
self.lUniTAP = UniTAP.TsiLib() self.lUniTAP = UniTAP.TsiLib()
except Exception as init_error:
pass
return False
def _reset_state(self): def _reset_state(self):
"""重置所有运行时状态(不关闭设备句柄)""" """重置所有运行时状态(不关闭设备句柄)"""

View File

@@ -1,5 +1,5 @@
{ {
"current_test_type": "sdr_movie", "current_test_type": "screen_module",
"test_types": { "test_types": {
"screen_module": { "screen_module": {
"name": "屏模组性能测试", "name": "屏模组性能测试",
@@ -10,10 +10,10 @@
"contrast" "contrast"
], ],
"timing": "DMT 1920x 1080 @ 60Hz", "timing": "DMT 1920x 1080 @ 60Hz",
"data_range": "Full", "data_range": "Limited",
"color_format": "RGB", "color_format": "RGB",
"bpc": 8, "bpc": 8,
"colorimetry": "sRGB", "colorimetry": "DCI-P3",
"patterns": { "patterns": {
"gamut": "rgb", "gamut": "rgb",
"gamma": "gray", "gamma": "gray",