capture.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import serial, time
  2. s = serial.Serial('COM9', 115200, timeout=0.3)
  3. print(f"[{time.strftime('%H:%M:%S')}] COM9 opened")
  4. # wait and collect
  5. print("\n=== Collecting 30 seconds of data ===\n")
  6. end = time.time() + 30
  7. buf = b''
  8. all_lines = []
  9. while time.time() < end:
  10. data = s.read(1024)
  11. if data:
  12. buf += data
  13. while b'\n' in buf:
  14. line, buf = buf.split(b'\n', 1)
  15. all_lines.append(line.strip())
  16. s.close()
  17. # 统计
  18. counts = {}
  19. for line in all_lines:
  20. try:
  21. t = line.decode('ascii', errors='replace').strip()
  22. except:
  23. continue
  24. if t.startswith('SM7OK'):
  25. # 提取 command bytes
  26. parts = t.split(':')[1].strip().split() if ':' in t else []
  27. if len(parts) >= 13:
  28. cmd_hex = f"{parts[11].upper()}{parts[12].upper()}"
  29. key = f"0x{cmd_hex}"
  30. else:
  31. key = "short"
  32. counts[key] = counts.get(key, 0) + 1
  33. print(f" [{key}] {t[:120]}")
  34. print(f"\n=== Frame counts ===")
  35. for k, v in sorted(counts.items()):
  36. print(f" {k}: {v}")
  37. # 详细分析 0x0207 和 0x0209 和 0x0202 和 0x020B
  38. def analyze_frame(t):
  39. parts = t.split(':')[1].strip().split() if ':' in t else []
  40. if len(parts) < 15:
  41. return
  42. cmd = f"{parts[11].upper()}{parts[12].upper()}"
  43. print(f"\n--- 0x{cmd} len={len(parts)} ---")
  44. print(f" RAW: {' '.join(parts)}")
  45. # 关键字节偏移(从 AA 开始算,parts[0]=EF, [1]=AA, [2]=55, ...)
  46. # Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  47. # Field: EF AA 55 LENh LENl VER TYP SEQh SEQL CH RES CMDh CMDl D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 ...
  48. for i, p in enumerate(parts):
  49. print(f" [{i:2d}] 0x{p.upper()} = {int(p,16)}")
  50. print("\n\n=== Detailed 0x0207 frames ===")
  51. for line in all_lines:
  52. t = line.decode('ascii', errors='replace').strip()
  53. if t.startswith('SM7OK') and '02 07' in t:
  54. analyze_frame(t)
  55. print("\n\n=== Detailed 0x0209 frames ===")
  56. for line in all_lines:
  57. t = line.decode('ascii', errors='replace').strip()
  58. if t.startswith('SM7OK') and '02 09' in t:
  59. analyze_frame(t)
  60. print("\n\n=== Detailed 0x0202 frames ===")
  61. for line in all_lines[:3]:
  62. t = line.decode('ascii', errors='replace').strip()
  63. if t.startswith('SM7OK') and '02 02' in t:
  64. analyze_frame(t)
  65. print("\n\n=== Any 0x020B frames? ===")
  66. found_0b = False
  67. for line in all_lines:
  68. t = line.decode('ascii', errors='replace').strip()
  69. if t.startswith('SM7OK') and ('02 0B' in t or '0B ' in t.split(':')[1][:40] if ':' in t else False):
  70. found_0b = True
  71. analyze_frame(t)
  72. if not found_0b:
  73. print(" NO 0x020B frames found!")
  74. # 也打印 JSON 里的 pl2/qx2 值
  75. print("\n\n=== JSON pl2/qx2 values ===")
  76. json_seen = 0
  77. for line in all_lines:
  78. t = line.decode('ascii', errors='replace').strip()
  79. if t.startswith('{') and json_seen < 5:
  80. if 'pl2' in t or 'qx2' in t or 'ql2' in t or 'shuimian' in t or 'xinlv' in t:
  81. print(f" {t}")
  82. json_seen += 1