[Python] 纯文本查看 复制代码
#!/usr/bin/env python3
"""Combined auto_debug + TCP test - run TCP test while GDB Server is still up"""
import subprocess
import time
import os
import sys
import re
import socket
from datetime import datetime
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
sys.stderr.reconfigure(encoding='utf-8', errors='replace')
# ============ 配置 ============
PROJECT_DIR = r"D:\SuperMicroTong\AD16X6-ETH\CODE"
BUILD_DIR = os.path.join(PROJECT_DIR, "cmake-build-relwithdebinfo-mingw")
ELF_FILE = os.path.join(BUILD_DIR, "AD16X6-ETH.elf")
CMAKE = r"C:\Program Files\JetBrains\CLion 2026.1\bin\cmake\win\x64\bin\cmake.exe"
JLINK_GDB_SERVER = r"C:\Program Files\SEGGER\JLink_V796p\JLinkGDBServerCL.exe"
GDB = r"C:\Program Files (x86)\Arm\GNU Toolchain mingw-w64-i686-arm-none-eabi\bin\arm-none-eabi-gdb.exe"
PROCKILL = os.path.join(PROJECT_DIR, "tools", "prockill", "bin", "Release", "net10.0", "prockill.dll")
TCP_TEST = os.path.join(PROJECT_DIR, "test_tcp_server.py")
GDB_SERVER_PORT = 2331
RTT_TELNET_PORT = 19021
def run_cmd(cmd, timeout=60):
try:
result = subprocess.run(
cmd, capture_output=True, text=True, timeout=timeout,
encoding='utf-8', errors='replace', shell=True
)
return result.stdout + result.stderr, result.returncode
except subprocess.TimeoutExpired:
return "TIMEOUT", -1
def kill_processes(*names):
if not os.path.exists(PROCKILL):
return
args = " ".join(names)
run_cmd(f'dotnet "{PROCKILL}" {args}', timeout=10)
def main():
print("=" * 50)
print("Auto Debug + TCP Test")
print(f"时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 50)
# Step 1: 编译
print("\n[1/5] 编译项目")
output, rc = run_cmd(f'cmd /C ""{CMAKE}" --build "{BUILD_DIR}" --target AD16X6-ETH -j 14"', timeout=120)
if rc != 0 and "no work to do" not in output.lower():
print(" X 编译失败!")
return
print(" OK 编译成功")
# Step 2: 清理旧进程
print("\n[2/5] 清理旧进程")
kill_processes("JLinkGDBServerCL")
time.sleep(1)
# Step 3: 启动GDB Server
print("\n[3/5] 启动 GDB Server + 下载程序")
cmd = [
JLINK_GDB_SERVER,
"-select", "USB",
"-device", "STM32H723VG",
"-if", "SWD",
"-speed", "4000",
"-noir",
"-localhostonly", "1",
"-rtos", "auto",
"-silent",
]
gdb_proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
creationflags=subprocess.CREATE_NO_WINDOW
)
time.sleep(3)
# GDB下载程序
gdb_cmds = os.path.join(BUILD_DIR, "gdb_auto.txt")
elf_gdb_path = ELF_FILE.replace("\\", "/")
with open(gdb_cmds, 'w') as f:
f.write("set confirm off\n")
f.write("set pagination off\n")
f.write(f"target remote localhost:{GDB_SERVER_PORT}\n")
f.write("monitor reset halt\n")
f.write(f"load {elf_gdb_path}\n")
f.write("monitor reset\n")
f.write("monitor go\n")
f.write("detach\n")
f.write("quit\n")
output, rc = run_cmd(f'"{GDB}" -batch -x "{gdb_cmds}" "{ELF_FILE}"', timeout=30)
if "Loading section" in output or rc == 0:
print(" OK 程序已下载并运行")
else:
print(" ! 下载可能失败")
# Wait for board to fully initialize (W5500, TCP server, etc.)
print("\n 等待板卡初始化 (5秒)...")
time.sleep(5)
# Step 4: 读取RTT看初始化日志
print("\n[4/5] 读取 RTT 初始化日志")
try:
rtt_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
rtt_sock.settimeout(3)
rtt_sock.connect(("127.0.0.1", RTT_TELNET_PORT))
time.sleep(2)
init_data = b""
while True:
try:
d = rtt_sock.recv(4096)
if d:
init_data += d
else:
break
except socket.timeout:
break
rtt_sock.close()
text = init_data.decode('utf-8', errors='replace')
for line in text.splitlines():
if line.strip():
print(f" {line.strip()}")
except Exception as e:
print(f" RTT连接失败: {e}")
# Step 5: 运行TCP测试
print("\n[5/5] 运行 TCP 测试")
tcp_result = subprocess.run(
[sys.executable, TCP_TEST],
capture_output=True, text=True, timeout=30,
encoding='utf-8', errors='replace'
)
print(tcp_result.stdout)
if tcp_result.stderr:
for line in tcp_result.stderr.splitlines():
if line.strip():
print(f" ERR: {line.strip()}")
# 读取测试后的RTT日志
print("\n--- RTT 日志 (测试后) ---")
try:
rtt_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
rtt_sock.settimeout(3)
rtt_sock.connect(("127.0.0.1", RTT_TELNET_PORT))
time.sleep(1)
post_data = b""
while True:
try:
d = rtt_sock.recv(4096)
if d:
post_data += d
else:
break
except socket.timeout:
break
rtt_sock.close()
text = post_data.decode('utf-8', errors='replace')
for line in text.splitlines():
if line.strip():
print(f" {line.strip()}")
except Exception as e:
print(f" RTT连接失败: {e}")
# 清理
print("\n 清理GDB Server...")
try:
gdb_proc.kill()
gdb_proc.wait(timeout=5)
except:
pass
kill_processes("JLinkGDBServerCL")
print("\n" + "=" * 50)
print("完成!")
print("=" * 50)
if __name__ == "__main__":
main()