openmv无法处理飞控发送过来的数据

问题遇到的现象和发生背景

在做openmv与匿名飞控通信时,发现openmv一直没有接受到数据,但是发送数据是正常的,可以在串口调试助手看到,但是,用串口调试助手发送数据给openmv却没有任何的反应。

问题相关代码,请勿粘贴截图

```python
import sensor, image, time, os, tf, math, uos, gc
#import ei_object_detection
from pyb import UART, Timer, LED
#from ei_object_detection import lockhanzi()

sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)      # Set frame size to QVGA (320x240)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=2000)          # Let the camera adjust.

net = None
labels = None
min_confidence = 0.5

try:
    # load the model, alloc the model file on the heap if we have at least 64K free after loading
    net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
except Exception as e:
    raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')

try:
    labels = [line.rstrip('\n') for line in open("labels.txt")]
except Exception as e:
    raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')

colors = [ # Add more colors if you are detecting more than 7 types of classes at once.
    (255,   0,   0),
    (  0, 255,   0),
    (255, 255,   0),
    (  0,   0, 255),
    (255,   0, 255),
    (  0, 255, 255),
    (255, 255, 255),
]

clock = time.clock()

# 保存识别照片类
class Saveimage(object):
    def __init__(self):
        #self.saveimage = img
        pass

    def Recognize(self,img):
        self.img = img
        for i, detection_list in enumerate(net.detect(self.img, thresholds=[(math.ceil(min_confidence * 255), 255)])):
            if (i == 0): continue # background class
            if (len(detection_list) == 0): continue # no detections for this class?
            print("recognize to: "+labels[i])
            if labels[i]=='gui': return 'gui'
            elif labels[i]=='lin': return 'lin'
            elif labels[i]=='dian': return 'dian'
            elif labels[i]=='zi': return 'zi'
            elif labels[i]=='ke': return 'ke'
            elif labels[i]=='ji': return 'ji'
            elif labels[i]=='da': return 'da'
            else:
                continue

    # 保存识别到的照片
    def Saveimg(self,img):
        self.img = img
        self.imglabel = self.Recognize(self.img)
        if self.imglabel=='gui':
            image.save("imglabel"+".jpg")
        elif self.imglabel=='lin':
            image.save("imglabel"+".jpg")
        elif self.imglabel=='dian':
            image.save("imglabel"+".jpg")
        elif self.imglabel=='zi':
            image.save("imglabel"+".jpg")
        elif self.imglabel=='ke':
            image.save("imglabel"+".jpg")
        elif self.imglabel=='ji':
            image.save("imglabel"+".jpg")
        elif self.imglabel=='dad':
            image.save("imglabel"+".jpg")
        else:
            pass
Saveimg=Saveimage()

# 串口初始化
uart = UART(3,115200)
uart.init(115200,8, parity=None, stop=1)

# 串口接收控制类
class Receive(object):
    def __init__(self,uart):
        self.uart_buf  = []
        self.state = 0
        self.uart = uart
        self.WorkMode = 0
        self.SelectMode = 0
    #读取串口缓存
    def UartReadBuffer(self):
        i = 0
        Buffer_size = self.uart.any()
        while i1
    #串口通信协议接收
    def ReceivePrepare(self,data):
        if self.state==0:
            if data == 0xAA:#帧头
                self.uart_buf.append(data)
                self.state = 1
            else:
                self.state = 0
        elif self.state==1:
            if data==0xAF:
                self.uart_buf.append(data)
                self.state = 2
            else:
                self.state = 0
        elif self.state==2:
            if data==0x05:
                self.uart_buf.append(data)
                self.state=3
            else:
                self.state=0
        elif self.state==3:
            if data!=0x00:
                self.uart_buf.append(data)
                self.state=4
            else:
                self.state=0
        elif self.state==4:
            if data!=0x00:
                self.uart_buf.append(data)
                self.state=5
            else:
                self.state=0
        elif self.state==5:
            if data!=0x00:
                self.uart_buf.append(data)
                self.state=6
            else:
                self.state=0
        elif self.state==6:
            self.state = 0
            self.uart_buf.append(data)
            self.ReceiveAnl()
            self.uart_buf=[]#清空缓冲区,准备下次接收数据
        else:
            self.state = 0
    #串口数据解析
    def ReceiveAnl(self):
        #校验
        i = 0
        sum = 0
        while i<(num-1):
            sum = sum+data_buf[i]
            i=i+1
        sum=sum%256
        if sum!=data_buf[num-1]:
            return
        #校验通过 设置模块工作模式
        self.WorkMode = self.uart_buf[4]
        self.SelectMode = self.uart_buf[5]

# 发送数据
uart_bufl = bytearray([0xAA,0xFF, 0xAA, 0x00, 0xFF, 0x00,0x00,0x00,0x00, 0x00, 0x00, 0x00, 0x00])
# 串口接收控制类实例化
Receive_ctrl=Receive(uart)

# 数据发送函数
def uartsend(timer):
    uart.write(uart_bufl)

# 发送数据更新
def updatesend(custom, cx, cy, distance):
    i=0
    sum = 0
    add = 0
    uart_bufl[2] = custom;            #用户自定义功能(待定,格式:0xCC等)
    uart_bufl[4] = cx;
    uart_bufl[5] = cy;
    uart_bufl[6] = distance;           # 距离
    uart_bufl[3] = len(uart_bufl)-6;
    while i<(len(uart_bufl)-2):
        sum = sum+uart_bufl[i]
        add = add+sum
        i+=1
    uart_bufl[-2] = sum;         # 和校验
    uart_bufl[-1] = add;        # 附加校验

tim = Timer(2, freq=20)
tim.callback(uartsend)

while(True):
    clock.tick()
    img = sensor.snapshot()
    if Receive_ctrl.SelectMode==0xDD:  # 进入汉字保存模式
        img0 = sensor.snapshot()
        Saveimg.Saveimg(img0)
    elif Receive_ctrl.SelectMode==0xEE:  # 进入寻找汉字模式
        if Receive_ctrl.WorkMode!=0x06:  # 进入汉字锁定模式
            img1 = sensor.snapshot()
            lockimg = ''
            for i, detection_list in enumerate(net.detect(img1, thresholds=[(math.ceil(min_confidence * 255), 255)])):
                if (i == 0): continue # background class
                if (len(detection_list) == 0): continue # no detections for this class?

                lockimg = labels[i]
                uart_bufl[4] = 0xCC
                print('reading:'+lockimg)
        else:                           # 跳出锁定模式,开始寻找汉字
            img2 = sensor.snapshot()
            for k, detection_list in enumerate(net.detect(img2, thresholds=[(math.ceil(min_confidence * 255), 255)])):
                if (k == 0): continue # background class
                if (len(detection_list) == 0): continue # no detections for this class?
                    #lockimg = labels[k]
                if labels[k]==lockimg:
                    print(lockimg+labels[k]+'\r\n')
                    #print(labels[k]+'\r\n')
                    for d in detection_list:
                        [x, y, w, h] = d.rect()
                        center_x = math.floor(x + (w / 2))
                        center_y = math.floor(y + (h / 2))
                        distance = int((80-center_x)*100*0.0024/0.42)
                        uart_bufl[4] = center_x
                        uart_bufl[5] = center_y
                        uart_bufl[6] = distance
                        updatesend(0xAA,center_x,center_y,distance)
                        print('%d',distance)
                else:
                    uart_bufl[6] = 0x3C
                    print("Don't get picture!")
            #time.sleep_ms(2)
    else:    # 等待进入一个识别模式
        print("Wait enter a recognization mode!")
    print(clock.fps(), "fps", end="\n\n")

我的解答思路和尝试过的方法

如果只是用uart.readchar()函数的话是可以读取一个字节的,但是就是无法处理一帧数据,所以我以为是我发送的数据没有通过校验,然后就试着把校验位删掉,但是结果是openmv依然是处于没有接收到数据的状态。

我想要达到的结果

能够正确处理飞控发送过来的数据。