openmv识别二维码与串口通信

目的: 给二维码指定内容:“xxx+xxx”,识别并返回及发送对应序号到串口,同时,正在执行扫码动作时,亮起绿灯,扫码结束后亮起蓝灯。

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

aa = 0;
intaa = 0;
renum = 0;
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # can be QVGA on M7...
sensor.skip_frames(30)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...

led = pyb.LED(1) # Red LED = 1, Green LED = 1, Blue LED = 2, IR LEDs = 4.
uart = UART(3, 115200, timeout_char = 1000)
led.on()

def Rec_NUM1(lista):
    if (lista[0]=='1' and lista[1]=='2' and lista[2]=='3'):
        return 1
    elif (lista[0]=='1' and lista[1]=='3' and lista[2]=='2'):
        return 2
    elif (lista[0]=='2' and lista[1]=='1' and lista[2]=='3'):
        return 3
    elif (lista[0]=='2' and lista[1]=='3' and lista[2]=='1'):
        return 4
    elif (lista[0]=='3' and lista[1]=='1' and lista[2]=='2'):
        return 5
    elif (lista[0]=='3' and lista[1]=='2' and lista[2]=='1'):
        return 6

def Rec_NUM2(lista):
    if (lista[4]=='1' and lista[5]=='2' and lista[6]=='3'):
        return 1
    elif (lista[4]=='1' and lista[5]=='3' and lista[6]=='2'):
        return 2
    elif (lista[4]=='2' and lista[5]=='1' and lista[6]=='3'):
        return 3
    elif (lista[4]=='2' and lista[5]=='3' and lista[6]=='1'):
        return 4
    elif (lista[4]=='3' and lista[5]=='1' and lista[6]=='2'):
        return 5
    elif (lista[4]=='3' and lista[5]=='2' and lista[6]=='1'):
        return 6

while(True):
    #uart.write('111')
    if uart.any():
        led.off()
        aa = uart.readline()
        time.sleep(150)     #延时150ms
        led = pyb.LED(2)
        led.on()
        intaa = int(aa)
        print(intaa)
      #  print(img.find_qrcodes())
    img = sensor.snapshot()
    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
    if(intaa==2):
        for code in img.find_qrcodes():
            output_str="%s" % code.payload() #方式1
            renum = int(Rec_NUM1(output_str)*10 + Rec_NUM2(output_str))
            print(renum)
            renum=ujson.dumps(renum)
            uart.write(renum)
            intaa = 0
            led.off()

上一篇:算法运算符


下一篇:160. 相交链表 + 双指针法