fix(DLPilot): 二进制载荷日志行以 hex 展示 —— 修复复制被 NUL 截断

This commit is contained in:
2026-09-18 15:32:34 +08:00
parent 6c56fac5dc
commit e3544239af
3 changed files with 25 additions and 5 deletions
+19 -5
View File
@@ -185,15 +185,29 @@ class FrameParser:
del self._buf[:1]
def _clean(s):
"""控制字符 -> '·': 二进制载荷按 ASCII 解出的 NUL 等会污染日志行,
且 Windows 剪贴板遇 NUL 直接截断复制内容"""
return ''.join(c if ord(c) >= 0x20 and ord(c) != 0x7F else '·' for c in s)
def describe(fr):
"""响应帧 -> 人读文本: TYPE 译码 + payload, err:N 追加 DLPC 错误释义"""
"""响应帧 -> 人读文本: TYPE 译码 + payload, err:N 追加 DLPC 错误释义。
二进制载荷(M303/M304/M305 电源实测)不以 ASCII 展示 —— 改显 hex,
避免乱码与 NUL 截断剪贴板(值由 GUI 读数框/CLI format_power 译码)。"""
pl = fr.payload
if any(ord(c) < 0x20 or ord(c) == 0x7F for c in pl):
pl = f'[二进制载荷 {fr.raw[3:-1].hex(" ").upper()}]'
else:
pl = _clean(pl)
if fr.type_code == 0xF0:
text = fr.payload or 'ok'
text = pl or 'ok'
else:
text = TYPE_NAMES.get(fr.type_code, f'TYPE=0x{fr.type_code:02X}')
if fr.payload:
text += f' {fr.payload}'
m = re.search(r'err:(\d+)', fr.payload)
if pl:
text += f' {pl}'
m = re.search(r'err:(\d+)', pl)
if m:
text += f'({DLPC_ERR_NAMES.get(int(m.group(1)), "未知错误码")})'
return text