logo

请问用uart进行串口通信,要调用哪个函数才能读入

2018-11-16 16:20
O
😩
import sensor, image, time,pyb
from pyb import UART
import json

thresholds = [(-5,35, 20, 60, 0, 40), # generic_red红_thresholds
              (10, 70, -40, -20, 0, 20), # generic_green绿_thresholds
              (15,45, 0, 35, -60, -30)] # generic_blue蓝_thresholds
uart = UART(3, 9600)

ledR=  pyb.LED(1)
ledG=  pyb.LED(2)
ledB=  pyb.LED(3)
led=  pyb.LED(1) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.

def R():
    ledR.on()            #亮灯
    time.sleep(1000)     #延时150ms
    ledR.off()           #暗灯
    time.sleep(500)
def B():
    ledB.on()            #亮灯
    time.sleep(1000)     #延时150ms
    ledB.off()           #暗灯
    time.sleep(500)
def G():
    ledG.on()            #亮灯
    time.sleep(1000)     #延时150ms
    ledG.off()           #暗灯
    time.sleep(500)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()



def twoley():
    while(True):
        clock.tick()
        img = sensor.snapshot()
        img.lens_corr(1.8)#
        for code  in  img.find_qrcodes():
            img.draw_rectangle(code.rect(), color   =(255,0,0))
            message =   code.payload()
            a=int(message)

            if a==123:
                while(True):
                    R()
                    G()
                    B()
                    print(a)
                    uart.write('123')

            
                

     
                    

                   
    

def yanshe():
    while(True):
        clock.tick()
        img = sensor.snapshot()
        for blob in img.find_blobs([thresholds[0]], pixels_threshold=200, area_threshold=200):
                img.draw_rectangle(blob.rect())
                img.draw_cross(blob.cx(), blob.cy())
                a=blob.cx()
        for blob in img.find_blobs([thresholds[1]], pixels_threshold=200, area_threshold=200):
                img.draw_rectangle(blob.rect())
                img.draw_cross(blob.cx(), blob.cy())
                b=blob.cx()
        for blob in img.find_blobs([thresholds[2]], pixels_threshold=200, area_threshold=200):
                img.draw_rectangle(blob.rect())
                img.draw_cross(blob.cx(), blob.cy())
                c=blob.cx()
                time.sleep(500)
                if a<b :
                    if a<c :
                        if b<c :
                            print(3,2,1)
                            uart.write('out\n')
                        else :
                            print(2,3,1)
                    else :
                        print(2,1,3)
                else :
                     if b<c :
                         if a<c :
                              print(3,1,2)
                         else :
                              print(1,3,2)
                     else :
                          print(1,2,3)

while(True):
    flag=uart.read(0)

    if  flag==1:
        twoley()

    elif flag==0:
        yanshe()
I
网红小智智 星瞳科技
if uart.any():
    uart.read()
2018-11-16 21:13
O
😩

那我要问呢吧读到的数据进行赋值呢

2018-11-16 23:14
I
网红小智智 星瞳科技

数据=uart.read()

2018-11-19 20:30
O
😩

咱们这个串口输出的数据类型是哪种?要怎么转换才能让51单片机识别?谢谢了

2018-11-19 21:50
I
网红小智智 星瞳科技

新的问题单独发帖子