实验结果,发送hex_0xab,显示的是171.
说明是显示的是int.
2/必须要加校验码,不然容易出错,这个程序;
# Untitled - By: Administrator - 周二 九月 22 2020
import sensor, image, time , pyb
from pyb import UART
from pyb import Timer
from pyb import LED
import json
thresholds = [(53, 100, -101, 127, -59, 127), # 红色
#(21, 75, 3, -38, 34, 68), # 绿色
(52, 100, -101, 127, -59, 127),
(39, 100, 15, 127, -112, 127)] # generic_blue_thresholds
threshold_index = 2 # 0 for red, 1 for gre9en, 2 for blue
FunNum=100
TaskFlag=0
StartChar=0xab #帧头
EndChar=0xba #帧尾
EndCharRec=0x00
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
uart=UART(1,9600) #PA9 PA10
uart.init(9600, bits=8, parity=None, stop=1, timeout_char=1000) # init with given parameters
clock = time.clock()
##########################################################
def tick(timer): # we will receive the timer object when being called
global data
global FunNum
global TaskFlag
if TaskFlag==1 : #如果是第一个功能,发送二维码数据
print('task01:',TaskFlag)
#if blobs: #如果查到色块,那么发送数据data
#print("Find")
#print('task:',output_str)
#uart.write(data)
if TaskFlag==2 : #如果是第2个功能,发送颜色数据的位置
print('task02:',TaskFlag)
#if blobs: #如果查到色块,那么发送数据data
#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 Uart_Receive(): #UART接收 改变框小球的颜色阈值
global threshold_index
global TaskFlag
global EndChar
global EndCharRec
global StartChar
if uart.any():
temp_data = uart.readchar()#读取第一个字节,发送的是0xab
print(temp_data) #打印出来,看看是什么数据, 显示171
if temp_data==0xab:#如何和帧头相等
print("LMF")#打印出来,验证
temp_data = uart.readchar() #读取第2个字节
print(temp_data)#打印出来,看看是什么数据
temp_data = uart.readchar() #读取第3个字节
print(temp_data)#打印出来,看看是什么数据
#if uart.any():
#temp_data = uart.readchar()
#if temp_data==StartChar: #帧头相等0xab
#FunNum = uart.readchar()
#EndCharRec=uart.readchar()
#if EndCharRec==EndChar: #帧尾相等
#if FunNum==0x01: #功能1:二维码扫描
#threshold_index=0
#TaskFlag=1
#print(temp_data,threshold_index)
#elif FunNum==0x02:#功能2:颜色扫描
#threshold_index=1
#TaskFlag=2
#print(temp_data,threshold_index)
#***************************************************************
while(True):
clock.tick()
img = sensor.snapshot()
Uart_Receive()
blobs = img.find_blobs([thresholds[threshold_index]])
#******************************************************查找到色块?
if blobs:
#发送 小球的(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
#data = bytearray([0xAA,0x55,0x07,int(max_blob.cx()/2),max_blob.cy(),l,threshold_index,0x00,0x00,checkout])#转成16进制
# data = bytearray([0xAA,0x55,0x07,int(max_blob.cx()/2),max_blob.cy(),l,threshold_index,0x00,0x00])#转成16进制
#uart.write(data)
time.sleep(1)
# led.on()
#else:
#print("NO FIND")
#data = bytearray([0xAA,0x55,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x06])
#uart.write(data)
#print(clock.fps())