# Untitled - By: Administrator - 周五 十月 23 2020
import micropython
#import sensor, image, time
import pyb,time
from pyb import UART
from pyb import Timer
from pyb import LED
import json
from pyb import Timer
from pyb import Pin, ExtInt
#####***********************************************************************
funcode=0x31
funcolor=0x32
'''
通信定义的帧头,帧尾等,,
1pos1x,2pos2x
'''
startcode0=0x21
startcode1=0x22
temp_data0=0
temp_data1=0
temp_data2=0
temp_data3=0
temp_data4=0
temp_data5=0
temp_data6=0
temp_data7=0
temp_data8=0
temp_data9=0
checksum=0x00
endcode0=0x0d
endcode1=0x0a
########################**************************************
uart=UART(1,115200) #PA9 txd PA10--rxd GND---stm32的GND
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters timeout_char=1000
clock = time.clock()
outstr1='999+999' #123+321
############################################################用外部中断进行通话的触发信号
recflag=0 #接收一帧的标记
reccount=0 #接收数据的号码,从下标0开始,第0个数据,第1个数据,-----
#*************************************************
def Uart_Receive(): #UART接收
global funcode
global funcolor
global recflag
global reccount
global temp_data0,temp_data1,temp_data2,temp_data3,temp_data4,temp_data5,temp_data6,temp_data7,temp_data8,temp_data9
#有数据进来类似串口中断,如果第一次进来,存进tempdata0
if uart.any():
#-----------------------------------------------------------------------------帧头0
if reccount==0: #第0个数据
temp_data0=uart.readchar()#将第一个数据,存在temp_data0中
print("temp0") #如果需要,可以打印出来观察数据是不是正常
print(temp_data0)
if temp_data0==startcode0:#如果数据=设置的帧头0,那么继续下一个数据的接收,
reccount=1
else: #否则,从0开始重新接收
reccount=0
#-------------------------------------------------------------------------------帧头1
elif reccount==1: #如果第0个相等,到了这里,将第二个数据放在temp_data1中
temp_data1=uart.readchar() #,将第二个数据放在temp_data1中
print("temp1") #如果需要,可以打印出来观察数据是不是正常
print(temp_data1)
if temp_data1==startcode1:#如果数据=设置的帧头1,那么继续下一个数据的接收,
reccount=2
else:
reccount=0 #否则,接收出错,从0开始重新接收
#--------------------------------------------------------------------------------功能码
elif reccount==2: #帧头检验完成,开始数据的接收,第三个数据是功能码,功能码
temp_data2=uart.readchar() #将第二个数据放在temp_data2中
reccount=3 #接收下一个数据
print("temp2") #如果需要,可以打印出来观察数据是不是正常
print(temp_data2)
elif reccount==3: #数据p
temp_data3=uart.readchar()
reccount=4
print("temp3")
print(temp_data3)
elif reccount==4:#数据o
temp_data4=uart.readchar()
reccount=5
print("temp4")
print(temp_data4)
elif reccount==5:#数据s
temp_data5=uart.readchar()
reccount=6
print("temp5")
print(temp_data5)
elif reccount==6:#数据i,如果i='1',那是pos1,如果是‘2’,那么是pos2
temp_data6=uart.readchar()
reccount=7
print("temp6")
print(temp_data6) # h0+h1+00+01+02+03+04+checksum+t0+t1
elif reccount==7:#校验码 如果校验码相等,那么继续接收,同时当做有效的数据
temp_data7=uart.readchar()
print("temp7")
print(temp_data7)
checksum0=int(temp_data2+temp_data3+temp_data4+temp_data5+temp_data6)%256)
#如果校验码相等,那么开始接受,不相等,数据接受错误,那么等于0,重新接收
if checksum0==temp_data7:
reccount=8
else :
reccount=0
elif reccount==8:#校验码相等,那么开始帧尾0
temp_data8=uart.readchar()
print("temp8")
print(temp_data8)
reccount=9
elif reccount==9:#结束符1 全部接收完,接收标记=1
temp_data9=uart.readchar()
print("temp9")
print(temp_data9)
recflag=1 #得到一帧数据----------------------------recflag=1
reccount=0 #允许下次重新接收
uart.deinit() #关闭串口,主函数进行打开
##*****************************************************************主函数
while(True):
clock.tick()
time.sleep(10)
if recflag==1:
#得到一帧数据,和命令库匹配成功,不再接收数据
#已经经过了校验,所以肯定是完整的一帧数据,这里对数据处理得到具体的命令
# if temp_data2=='1' && temp_data3=='p'&&temp_data4=='o'&&temp_data5=='s' &&temp_data6=='1':
#如果等于第一个命令,进行二维码扫描
if temp_data2=='1' ||temp_data2=='2':#接收到命令,发送十次ok
time.sleep(10)
uart.init(115200, bits=8, parity=None, stop=1)
checksum=(int)(0x26+'O'+'K')%256
while ii<10:#接收到命令,发送十次ok
uart.writechar(startcode0)
uart.writechar(startcode1)
uart.writechar(0x26)#OK的功能码
uart.writechar('O')
uart.writechar('K')
uart.writechar(checksum)
uart.writechar(endcode0)
uart.writechar(endcode1)
time.sleep(10)
ii=ii+1
ii=0
time.sleep(100)
if temp_data2=='1':#如果是第一个功能,扫描二维码
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) #开机是灰度图片,后面做颜色处理的时候,打开颜色拍照
sensor.set_framesize(sensor.VGA)#QVGA格式
#sensor.set_windowing((240, 240)) # look at center 240x240 pixels of the VGA resolution.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # 关闭自动增益,自动曝光
clock = time.clock()
time.sleep(1000)#延时,数据初始化设置成功
##二维码识别子程序
outstr1='999+999'
print("********Scan CODE*******")
xxx=0
while xxx<10:
#抓拍一张照片,循环,直到检测出二维码信息,如果超时,就报错
img = sensor.snapshot() #抓拍一张图片
for code in img.find_qrcodes():
img.draw_rectangle(code.rect(), color = 127)
outstr1=str(code.payload())
print(code)
#可以加程序,对结果进行滤波,假如读取10次,代码相等的次数>7次,取相同次数最多的二维码,作为最终的结果
if outstr1!='999+999': #如果不等于初值,说明读取到了二维码,连续扫描10次,得到结果
xxx+=1
else :
xxx=0 #没结果
time.sleep(100)
xxx=0
#清0标记,给下一次扫描使用
#将outstr1=的前面三个字符提取出来,变为int赋值给a01,后面三个字符提取出来变为int,赋值给b01
#outstr1[0]-------一个字节---int(outstr1[0])
print("outstr1=",outstr1)
#'123+321'
checksum=int((0x23+outstr1[0]+outstr1[1]+outstr1[2]+outstr1[3]+outstr1[4]+outstr1[5]+outstr1[6])%256)
while xxx<10 : #发送十次数据给stm32
uart.writechar(startcode0)
uart.writechar(startcode1)
uart.writechar(0x23) #二维码的功能号
uart.writechar(outstr1[0])#123+132
uart.writechar(outstr1[1])
uart.writechar(outstr1[2])
uart.writechar(outstr1[3])
uart.writechar(outstr1[4])
uart.writechar(outstr1[5])
uart.writechar(outstr1[6])
uart.writechar(checksum)#校验码
uart.writechar(endcode0)
uart.writechar(endcode1)
time.sleep(10)
xxx=xxx+1
print("scan code code code")
xxx=0
temp_data0=0
temp_data1=0
temp_data2=0
temp_data3=0
temp_data4=0
temp_data5=0
temp_data6=0
temp_data7=0
temp_data8=0
temp_data9=0
outstr1='999+999'
recflag=0 #允许继续串口接收
##########################################################
if temp_data2='2': #第二个功能:扫描颜色
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) #开机是灰度图片,后面做颜色处理的时候,打开颜色拍照
sensor.set_framesize(sensor.VGA)#QVGA格式
#sensor.set_windowing((240, 240)) # look at center 240x240 pixels of the VGA resolution.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # 关闭自动增益,自动曝光
clock = time.clock()
time.sleep(1000)#延时,数据初始化设置成功
##颜色识别子程序
outstr1='999+999'
xxx=0
print("********Scan CODE*******")
outstr1='999+999'
while xxx<10:
#抓拍一张照片,循环,直到检测出二维码信息,如果超时,就报错
img = sensor.snapshot() #抓拍一张图片
#自己写代码识别颜色,将颜色代号123+321这些符号也发给stm32,整理成字符串,放到outstr1里面
#????????????????????????????????
#?????????????????????????????????
#图像处理,得到每个色块的坐标A1 A2 A3------- B1 B2 B3
#处理上层坐标:红色,绿色,蓝色的坐标,得到顺序,也是红=1,绿=2,蓝=3: 123++132++
#赋值给outstr1---outstr1[0]='1',outstr1[1]=2,,,,outstr1[2]=3
#处理下层坐标:得到顺序,也是红=1,绿=2,蓝=3: 123++132++
#赋值给outstr1---outstr1[4]='1',outstr1[5]=2,,,,outstr1[6]=3
if outstr1!='999+999': #如果不等于初值,说明读取到了二维码,连续扫描10次,得到结果
xxx+=1
else :
xxx=0 #没结果
#
xxx=0 #清0标记,给下一次扫描使用
#将outstr1=的前面三个字符提取出来,变为int赋值给a01,后面三个字符提取出来变为int,赋值给b01
#outstr1[0]-------一个字节---int(outstr1[0])
print("outstr1=",outstr1)
#'123+321'
checksum=int((0x24+outstr1[0]+outstr1[1]+outstr1[2]+outstr1[3]+outstr1[4]+outstr1[5]+outstr1[6])%256)
while xxx<10 : #发送十次数据
uart.writechar(startcode0)
uart.writechar(startcode1)
uart.writechar(0x24) #颜色的功能号
uart.writechar(outstr1[0])#123+132
uart.writechar(outstr1[1])
uart.writechar(outstr1[2])
uart.writechar(outstr1[3])
uart.writechar(outstr1[4])
uart.writechar(outstr1[5])
uart.writechar(outstr1[6])
uart.writechar(checksum)#校验码
uart.writechar(endcode0)
uart.writechar(endcode1)
time.sleep(10)
xxx=xxx+1
print("scan code code code")
xxx=0
temp_data0=0
temp_data1=0
temp_data2=0
temp_data3=0
temp_data4=0
temp_data5=0
temp_data6=0
temp_data7=0
temp_data8=0
temp_data9=0
outstr1!='999+999'
recflag=0 #允许继续串口接收
#-------------------------------------------------------------------------
else :
time.sleep(5)
Uart_Receive() #没接收到数据,那么不断的接收
mv_erweima和color
猜你喜欢
转载自blog.csdn.net/lmf666/article/details/112223568
今日推荐
周排行