请问串口每次读一个数据是自动情况缓冲区的内容吗
-
#2018.8.2 import sensor, image, time , pyb,json #引入感光元件的模块 from pyb import UART from pyb import Timer from pyb import LED from machine import I2C from vl53l1x import VL53L1X import time led = pyb.LED(3) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4. thresholds = [(27, 67, 19, 91, 45, 76), # 红色 #(21, 75, 3, -38, 34, 68), # 绿色 #(36, 100, 45, -54, 26, 124), #(55, 70, -28, -10, 33, 57), (27, 90, -3, -28, 31, 125), (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds threshold_index = 1 # 0 for red, 1 for gre9en, 2 for blue uart_index=0 start=0 current_exposure_time_in_microseconds = sensor.get_exposure_us() sensor.reset()#初始化感光元件 sensor.set_pixformat(sensor.RGB565)#设置为彩色 sensor.set_framesize(sensor.QVGA) #320*240 设置图像大小 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() uart = UART(1, 115200) uart.init(115200, bits=8, parity=None, stop=1, timeout_char=1000) # 使用给定参数初始化 i2c = I2C(2) distance = VL53L1X(i2c) #def tick(timer): # we will receive the timer object when being called # global data # if blobs: # print("Find") # print('you send:',output_str) # uart.write(data) tim = Timer(4, freq=10) # create a timer object using timer 4 - trigger at 1Hz #tim.callback(tick) # set the callback to our tick function def find_max(blobs): max_size=0 for blob in blobs: if blob[2]*blob[3] > max_size: max_blob=blob max_size = blob[2]*blob[3] return max_blob def find_dir(xzb): dire=0 if (xzb <= 120): dire=1 elif (xzb > 120) and (xzb <185): dire=2 elif (xzb >= 185): dire=3 return dire def Uart_Receive(): #UART接收 改变框小球的颜色阈值 global uart_index; if uart.any(): temp_data = uart.readchar() if temp_data==1: #红色 uart_index=1 print(temp_data,uart_index) else: uart_index=0 print(temp_data,uart_index) while(True): if start<5: start=start+1 img = sensor.snapshot() blobs = img.find_blobs([thresholds[threshold_index]]) if blobs: max_blob = find_max(blobs) b = max_blob[0] #方框元组 L = (max_blob[2]+max_blob[3])/2 l=int(1000/L) #x_error = max_blob[5]-img.width()/2 #求横向偏差 x_error = max_blob[5]-img.width()/2 h_error = max_blob[2]*max_blob[3]-size_threshold img.draw_rectangle(max_blob[0:4]) # 画矩形 img.draw_cross(max_blob[5], max_blob[6]) # 画十字 #发送 小球的(x,y,l,n) #x为横坐标,y为纵坐标,l为粗略的距离,n为小球颜色(0:红 1:绿) # output_str="%d,%d,%d,%d" % (max_blob.cx(),max_blob.cy(),l,threshold_index) #10进制字符包 #checkout=0xAA+0x55+0x07+int(max_blob.cx()/2)+max_blob.cy()+l+threshold_index fx=find_dir(max_blob.cx()) xzb=max_blob.cx() xzba=int(xzb/100) xzbb=int(xzb%100) dis=distance.read() disa=int(dis/100) disb=int(dis%100) time.sleep_ms(100) output_str="[%d,%d,%d,%d,%d]"%(xzba,xzbb,disa,disb,l) data_out=bytearray([xzba,xzbb,disa,disb,l]) print('you send:',output_str) uart.write(data_out+'\r\n'); time.sleep_ms(50) led.on() #data = bytearray([0xAA,0x55,0x07,int(max_blob.cx()/2),max_blob.cy(),l,threshold_index,0x00,0x00,checkout])#转成16进制 # uart.write(data) # uart.write(output_str) #time.sleep_ms(10) #led.on() else: print("NO FIND") #data = bytearray([0xAA,0x55,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x06]) data_out=bytearray([0,0]) uart.write(data_out+'\r\n') time.sleep_ms(50) led.off() elif start==5: img = sensor.snapshot() blobs = img.find_blobs([thresholds[threshold_index]]) if blobs: max_blob = find_max(blobs) b = max_blob[0] #方框元组 L = (max_blob[2]+max_blob[3])/2 l=int(1000/L) #x_error = max_blob[5]-img.width()/2 #求横向偏差 x_error = max_blob[5]-img.width()/2 img.draw_rectangle(max_blob[0:4]) # 画矩形 img.draw_cross(max_blob[5], max_blob[6]) # 画十字 #发送 小球的(x,y,l,n) #x为横坐标,y为纵坐标,l为粗略的距离,n为小球颜色(0:红 1:绿) # output_str="%d,%d,%d,%d" % (max_blob.cx(),max_blob.cy(),l,threshold_index) #10进制字符包 #checkout=0xAA+0x55+0x07+int(max_blob.cx()/2)+max_blob.cy()+l+threshold_index #Uart_Receive() #time.sleep_ms(50) #if uart_index==1: #time.sleep_ms(1000) #uart_index=0; fx=find_dir(max_blob.cx()) dis=distance.read() time.sleep_ms(100) disa=int(dis/100) disb=int(dis%100) xzb=max_blob.cx() xzba=int(xzb/100) xzbb=int(xzb%100) output_str="[%d,%d,%d,%d,%d]"%(xzba,xzbb,disa,disb,l) data_out=bytearray([xzba,xzbb,disa,disb,l]) print('you send:',output_str) Uart_Receive() if uart_index==1: uart.write(data_out+'\r\n'); uart_index=0; time.sleep_ms(50) led.on() #data = bytearray([0xAA,0x55,0x07,int(max_blob.cx()/2),max_blob.cy(),l,threshold_index,0x00,0x00,checkout])#转成16进制 # uart.write(data) # uart.write(output_str) #time.sleep_ms(10) #led.on() else: print("NO FIND") #data = bytearray([0xAA,0x55,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x06]) data_out=bytearray([0,0]) if uart_index==1: uart.write(data_out+'\r\n'); uart_index=0; time.sleep_ms(50) led.off()
我发了字符1 然后readchar,读完后会自动清空吗,下次我不发1 会不会还读到1呢
-