|
|
@@ -44,8 +44,10 @@ ENDPOINTS = {"nothing": 0, "push_down": 1, "push_up": 2, "stop_clear": 255}
|
|
|
FRAME_TYPE_NAMES = {
|
|
|
0x01: "SET_ALL (下发全量控制)",
|
|
|
0x02: "GET_ALL (查询当前状态)",
|
|
|
+ 0x03: "HEARTBEAT (心跳测试 Ping)",
|
|
|
0x81: "SET_ACK (设置确认已应用)",
|
|
|
0x82: "GET_ACK (查询确认已受理)",
|
|
|
+ 0x83: "HEARTBEAT_ACK (心跳应答 Pong)",
|
|
|
0x42: "GET_DATA (全量状态数据)",
|
|
|
0x50: "MOTOR_DONE (电机动作完成事件)",
|
|
|
0xC0: "NACK (请求被拒绝)",
|
|
|
@@ -194,6 +196,10 @@ class Model:
|
|
|
slots_payload[i * 5 + 3 : i * 5 + 5] = half.to_bytes(2, "little")
|
|
|
return encode_frame(FrameType.SET_ALL, duty_bytes + bytes(slots_payload))
|
|
|
|
|
|
+ def heartbeat(self) -> bytes:
|
|
|
+ """Generate golden HEARTBEAT request frame (A5 5A 03 00 5C 48)."""
|
|
|
+ return encode_frame(FrameType.HEARTBEAT)
|
|
|
+
|
|
|
def get_all(self) -> bytes:
|
|
|
"""Generate golden GET_ALL request frame (A5 5A 02 00 6D 7B)."""
|
|
|
return encode_frame(FrameType.GET_ALL)
|
|
|
@@ -202,7 +208,7 @@ class Model:
|
|
|
parsed = decode_frame(frame)
|
|
|
if not parsed.crc_ok:
|
|
|
raise ValueError("CRC 校验失败")
|
|
|
- allowed = {FrameType.SET_ALL, FrameType.GET_ALL}
|
|
|
+ allowed = {FrameType.SET_ALL, FrameType.GET_ALL, FrameType.HEARTBEAT}
|
|
|
if parsed.frame_type not in allowed:
|
|
|
raise ValueError("此产品不支持该请求类型;响应请在状态页解析")
|
|
|
return parsed
|
|
|
@@ -240,6 +246,8 @@ class Model:
|
|
|
result.append({"kind": "ack", "type": "SET_ACK"})
|
|
|
elif kind == FrameType.GET_ACK:
|
|
|
result.append({"kind": "ack", "type": "GET_ACK"})
|
|
|
+ elif kind == FrameType.HEARTBEAT_ACK:
|
|
|
+ result.append({"kind": "ack", "type": "HEARTBEAT_ACK"})
|
|
|
elif kind == FrameType.MOTOR_DONE:
|
|
|
if len(p) != 4:
|
|
|
raise ValueError("MOTOR_DONE 长度必须为 4 字节")
|
|
|
@@ -325,6 +333,10 @@ class Model:
|
|
|
explanation["summary"] = "上位机向单板查询当前全量状态与温度;单板先回复 GET_ACK (0x82),紧接着回复 GET_DATA (0x42)"
|
|
|
fields.append("无载荷 (0 字节)")
|
|
|
|
|
|
+ elif ftype == 0x03: # HEARTBEAT
|
|
|
+ explanation["summary"] = "上位机向单板发送独立心跳检测请求 (PING),单板立即应答 0x83 并重载看门狗"
|
|
|
+ fields.append("零载荷心跳探测帧,用于物理链路连通性无损快速检测 (0 字节)")
|
|
|
+
|
|
|
elif ftype == 0x81: # SET_ACK
|
|
|
explanation["summary"] = "单板确认已应用 SET_ALL 配置;30 秒硬件看门狗已成功重载"
|
|
|
fields.append("无载荷 (0 字节)")
|
|
|
@@ -333,6 +345,10 @@ class Model:
|
|
|
explanation["summary"] = "单板确认已受理 GET_ALL 查询请求,随后立即发送 GET_DATA"
|
|
|
fields.append("无载荷 (0 字节)")
|
|
|
|
|
|
+ elif ftype == 0x83: # HEARTBEAT_ACK
|
|
|
+ explanation["summary"] = "单板向上位机回复心跳应答 (PONG),确认物理链路双向收发正常、逻辑正常运行"
|
|
|
+ fields.append("零载荷心跳确认帧 (0 字节)")
|
|
|
+
|
|
|
elif ftype == 0x42: # GET_DATA
|
|
|
explanation["summary"] = "单板返回全量状态、各通道温度与限位信息"
|
|
|
state = self.state(payload)
|
|
|
@@ -566,6 +582,9 @@ def run_gui(product: str) -> int:
|
|
|
btn_send_set = ttk.Button(ctrl_bar, text="⚡ 发送当前 SET 配置", state="disabled")
|
|
|
btn_send_set.pack(side="left", padx=5)
|
|
|
|
|
|
+ btn_heartbeat = ttk.Button(ctrl_bar, text="💓 心跳测试 (Ping)", state="disabled")
|
|
|
+ btn_heartbeat.pack(side="left", padx=5)
|
|
|
+
|
|
|
poll_var = tk.BooleanVar(value=False)
|
|
|
chk_poll = ttk.Checkbutton(ctrl_bar, text="自动每秒轮询 GET_ALL (维持看门狗)", variable=poll_var, state="disabled")
|
|
|
chk_poll.pack(side="left", padx=15)
|
|
|
@@ -878,6 +897,8 @@ def run_gui(product: str) -> int:
|
|
|
ttk.Button(quick_bar, text="GET_ALL 请求", command=lambda: load_template("A5 5A 02 00 6D 7B")).pack(side="left", padx=3)
|
|
|
ttk.Button(quick_bar, text="SET_ACK 应答", command=lambda: load_template("A5 5A 81 00 A6 35")).pack(side="left", padx=3)
|
|
|
ttk.Button(quick_bar, text="GET_ACK 应答", command=lambda: load_template("A5 5A 82 00 F5 60")).pack(side="left", padx=3)
|
|
|
+ ttk.Button(quick_bar, text="心跳请求(Ping)", command=lambda: load_template("A5 5A 03 00 5C 48")).pack(side="left", padx=3)
|
|
|
+ ttk.Button(quick_bar, text="心跳应答(Pong)", command=lambda: load_template("A5 5A 83 00 C4 53")).pack(side="left", padx=3)
|
|
|
ttk.Button(quick_bar, text="NACK 队列满", command=lambda: load_template("A5 5A C0 02 01 04 B1 2A")).pack(side="left", padx=3)
|
|
|
if model.motor:
|
|
|
ttk.Button(quick_bar, text="MOTOR_DONE(上限位4387步)", command=lambda: load_template("A5 5A 50 04 04 02 23 11 B6 02")).pack(side="left", padx=3)
|
|
|
@@ -1065,6 +1086,7 @@ def run_gui(product: str) -> int:
|
|
|
lbl_conn_status.config(text="已断开", foreground="gray")
|
|
|
btn_send_get.config(state="disabled")
|
|
|
btn_send_set.config(state="disabled")
|
|
|
+ btn_heartbeat.config(state="disabled")
|
|
|
chk_poll.config(state="disabled")
|
|
|
return
|
|
|
|
|
|
@@ -1084,6 +1106,7 @@ def run_gui(product: str) -> int:
|
|
|
lbl_conn_status.config(text=f"已连接 {port} @ {baud}", foreground="green")
|
|
|
btn_send_get.config(state="normal")
|
|
|
btn_send_set.config(state="normal")
|
|
|
+ btn_heartbeat.config(state="normal")
|
|
|
chk_poll.config(state="normal")
|
|
|
|
|
|
# Start reading thread with this epoch
|
|
|
@@ -1129,6 +1152,16 @@ def run_gui(product: str) -> int:
|
|
|
except Exception as exc:
|
|
|
messagebox.showerror("发送失败", str(exc))
|
|
|
|
|
|
+ def send_heartbeat_clicked():
|
|
|
+ with serial_lock:
|
|
|
+ sp = serial_port_obj
|
|
|
+ if not sp or not sp.is_open:
|
|
|
+ messagebox.showwarning("提示", "串口未连接")
|
|
|
+ return
|
|
|
+ hb_frame = model.heartbeat()
|
|
|
+ request_queue.put(("PING", hb_frame, "HEARTBEAT 心跳测试 (0x03 -> 0x83)"))
|
|
|
+ dispatch_next_request()
|
|
|
+
|
|
|
def on_poll_toggle():
|
|
|
nonlocal poll_generation
|
|
|
poll_generation += 1
|
|
|
@@ -1143,6 +1176,7 @@ def run_gui(product: str) -> int:
|
|
|
btn_connect.config(command=toggle_connect)
|
|
|
btn_send_get.config(command=send_get_clicked)
|
|
|
btn_send_set.config(command=send_set_clicked)
|
|
|
+ btn_heartbeat.config(command=send_heartbeat_clicked)
|
|
|
chk_poll.config(command=on_poll_toggle)
|
|
|
|
|
|
# Initial generation
|