| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import serial, time, json
- s = serial.Serial('COM9', 115200, timeout=0.1)
- print('COM9 opened')
- # 先清积压,等 5 秒抓当前状态
- time.sleep(2)
- print('\n=== 当前状态(先看 5 秒)===')
- cnt = 0
- last_json = None
- line = b''
- end = time.time() + 5
- while time.time() < end:
- data = s.read(256)
- for b in data:
- if b in (13, 10):
- if not line: continue
- t = line.decode('ascii', errors='replace')
- line = b''
- if t.startswith('{'):
- last_json = json.loads(t)
- cnt += 1
- else:
- line += bytes([b])
- if last_json:
- print(f" 收到 {cnt} 帧,最新:")
- for k in ['xl1','xy1','sm2','pl2','zy2','xl2','hx2']:
- v = last_json.get(k, '')
- print(f" {k}={v or '(空)'}")
- else:
- print(" ❌ 5 秒内没收到任何 JSON!")
- s.close()
- exit(1)
- # 发 xy1,01
- print('\n=== TX: {"zt":"xy1","neirong":"01"} ===')
- s.write(b'{"zt":"xy1","neirong":"01"}\n')
- s.flush()
- # 等 25 秒观察
- print('\n=== 等 25 秒,看血氧能不能上来 ===')
- end = time.time() + 25
- found = False
- frame_idx = 0
- while time.time() < end:
- data = s.read(256)
- for b in data:
- if b in (13, 10):
- if not line: continue
- t = line.decode('ascii', errors='replace')
- line = b''
- if t.startswith('{'):
- d = json.loads(t)
- frame_idx += 1
- xl1 = d.get('xl1', '')
- xy1 = d.get('xy1', '')
- sm2 = d.get('sm2', '')
- xl2 = d.get('xl2', '')
- hx2 = d.get('hx2', '')
- flag = ''
- if xl1 and xy1:
- flag = ' ✅ 血氧正常!'
- found = True
- elif xl1 == '' and xy1 == '':
- flag = ' ⚠️ 血氧空'
- print(f" JSON#{frame_idx:>2} xl1={xl1 or '(空)':<6} xy1={xy1 or '(空)':<6} sm2={sm2 or '(空)':<5} xl2={xl2 or '(空)':<6} hx2={hx2 or '(空)':<6}{flag}")
- else:
- line += bytes([b])
- s.close()
- print(f"\n=== 结论 ===")
- print(f" 血氧有没有上来: {'✅ YES' if found else '❌ NO'}")
- print(f" 总共收到 {frame_idx} 帧 JSON")
|