![0_1722048068295_8IPUR$DJSGLU3X4S4{DLUL.png
state=0 #状态标识
rx_buff=[0]*2 #数据接收缓存区
id_tixu=0x0000 #定义ID号接收缓存区
def read_data():
global id_tixu
global state
if uart.any(): #进行串口数据的接收
#res=uart.read(1) #表示为读取一个十六进制数,这里的uart必须是例化的
#state表示处理接收到的第几位数据
for i in range(5):
if state == 0:
res=uart.read(1)
res = ustruct.unpack("B", res) #表示将res数据按照B格式进行解包
res = hex(res)
data = res[0:4]
if data == '0xFF': #帧头1
state = 1
elif state == 1:
res=uart.read(1)
res = ustruct.unpack("B", res)
res = hex(res)
for i in range(4):
data = res[0:4]
id_tixu=data
print(id_tixu)
state = 2
elif state == 2:
res=uart.read(2)
res = ustruct.unpack("B", res)
res = hex(res)
res = int(res)
rx_buff[0]=res #数据1
Find_Task=rx_buff[0]
print(Find_Task)
state = 3
elif state == 3:
#res=uart.read(1)
res=uart.read(2)
res = ustruct.unpack("B", res)
res = hex(res)
res = int(res)
rx_buff[1]=res #数据2
Target_Num=rx_buff[1]
print(Target_Num)
state = 4
elif state == 4:
res=uart.read(1)
res = ustruct.unpack("B", res)
res = hex(res)
data = res[0:4]
if data == '0xFE': #帧尾
state = 0
#数据处理处
print(rx_buff)
else:
state = 0