logo

串口发送数据为什么会发送两次

2019-03-22 16:34
B
上校无人来信

我用这个代码发给stm32为什么会发过去两次?

import sensor, image, time
from pyb import UART
from pyb import LED

red_led   = LED(1)  
green_led = LED(2)
blue_led  = LED(3)

red_threshold    = (42, 66, 44, 83, 11, 62)    #  红色阈值 调整 (minL, maxL, minA, maxA, minB, maxB)
green_threshold  = (23, 74, -53, -17, 5, 31)
blue_threshold = (31, 48, -2, 39, -65, -29)
flag = '0'
put3= ['0','0','0']
check = ['1','2','3']


sensor.reset()
sensor.set_pixformat(sensor.RGB565)  #设置色彩模式
sensor.set_framesize(sensor.QVGA)   #分辨率
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must turn this off to prevent image washout... 取消增益
clock = time.clock()

uart = UART(3, 115200,)  #串口初始化设置 串口名 波特率
uart.init(115200, bits=8, parity=None, stop=1)

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob




def judge_position(x_value):
    if x_value > 100 and x_value < 220:
        return '2'
    if x_value > 0 and x_value <120:
        return '1'
    if x_value > 200 and x_value <320:
        return '3'


while(True):
    clock.tick()
    img = sensor.snapshot() #拍取图像
#    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.图像消畸变,可以用非鱼眼摄像头硬件消畸变

    if uart.any():
        flag = uart.readline().decode().strip()
    if flag == '1':
        img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.图像消畸变,可以用非鱼眼摄像头硬件消畸变
        green_led.on()
        blue_led.off()
        for code in img.find_qrcodes():
            img.draw_rectangle(code.rect())  #rect()返回的是x,y,w,h数据格式,而draw可以单独接收描边
            messages = code.payload()   #接收二维码数据
            if messages in check :
#                print(messages)
                uart.write(messages+'\0') #串口发送二维码数据
                flag = '0'


    if flag == '2':
        red_blobs   = img.find_blobs([red_threshold],pixels_threshold=200) #50
        green_blobs = img.find_blobs([green_threshold], pixels_threshold=200)
        blue_blobs  = img.find_blobs([blue_threshold],pixels_threshold=200)
        blue_led.on()
        green_led.off()
        img.draw_line(100, 0, 100, 240)
        img.draw_line(220, 0, 220, 240)
        if red_blobs:
            red_blob = find_max(red_blobs)
            img.draw_rectangle(red_blob.rect())
            img.draw_cross(red_blob.cx(), red_blob.cy())
            put3[0] = judge_position(red_blob.cx())
        if green_blobs:
            green_blob = find_max(green_blobs)
            img.draw_rectangle(green_blob.rect())
            img.draw_cross(green_blob.cx(), green_blob.cy())
            put3[1] = judge_position(green_blob.cx())
        if blue_blobs:
            blue_blob = find_max(blue_blobs)
            img.draw_rectangle(blue_blob.rect())
            img.draw_cross(blue_blob.cx(), blue_blob.cy())
            put3[2] = judge_position(blue_blob.cx())
#        print(put3[0]+put3[1]+put3[2])
        if (put3[0] in check) or (put3[1] in check) or (put3[2] in check):
            print(put3[0]+put3[1]+put3[2])
            uart.write(put3[0]+put3[1]+put3[2])
            flag = '0'
I
网红小智智 星瞳科技

逻辑看不懂,好几个flag不知道什么意思。

B
上校无人来信

flag就是stm32发送过来的数据,发送2就是开启颜色识别,识别然后串口发送过去flag就置零,就不会再进 if flag == ‘2’ 继续发送,但是还是会发送两次。