1.首先要导入Pyserial第三方库
pip install pyserial
2.测试串口是否打开
#测试串口是否打开
import serial
ser = serial.Serial('COM8', 15200, timeout=1) #第一个参数为串口名,可通过设备管理器查询
ser.open()
if ser.is_open:
print('串口已打开')
else:
print('串口打开失败')
3.串口读取字节
while True:
if ser.in_waiting:
data = ser.read(1)
hex_data = hex(data[0])
print(hex_data)
假如给定数据包
hex_data = "53 59 81 05 00 05 63 4B 42 8F E2 98 54 43"
该数据包来自60G毫米波呼吸睡眠雷达数据
其中53 59为该数据帧帧头,54 43为数据帧帧尾
def find_packets(data, start_marker, end_marker):
packets = [] # 存储找到的数据包
start_index = 0 # 开始搜索的位置
while True:
# 查找起始标记
start_index = data.find(start_marker, start_index)
if sta