OPENMV和STM32通信

OPENMV端

import sensor, image, time, math
from pyb import UART
uart = UART(3,38400)
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((640, 50))
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
def barcode_name(code):
    if(code.type() == image.EAN2):
        return "EAN2"
    if(code.type() == image.EAN5):
        return "EAN5"
    if(code.type() == image.EAN8):
        return "EAN8"
    if(code.type() == image.UPCE):
        return "UPCE"
    if(code.type() == image.ISBN10):
        return "ISBN10"
    if(code.type() == image.UPCA):
        return "UPCA"
    if(code.type() == image.EAN13):
        return "EAN13"
    if(code.type() == image.ISBN13):
        return "ISBN13"
    if(code.type() == image.I25):
        return "I25"
    if(code.type() == image.DATABAR):
        return "DATABAR"
    if(code.type() == image.DATABAR_EXP):
        return "DATABAR_EXP"
    if(code.type() == image.CODABAR):
        return "CODABAR"
    if(code.type() == image.CODE39):
        return "CODE39"
    if(code.type() == image.PDF417):
        return "PDF417"
    if(code.type() == image.CODE93):
        return "CODE93"
    if(code.type() == image.CODE128):
        return "CODE128"
while(True):
    clock.tick()
    img = sensor.snapshot()
    codes = img.find_barcodes()
    for code in codes:
        img.draw_rectangle(code.rect())
        print_args = (barcode_name(code), code.payload(), (180 * code.rotation()) / math.pi, code.quality(), clock.fps())
        print("Barcode %s, Payload \"%s\", rotation %f (degrees), quality %d, FPS %f" % print_args)
        long1=code.payload()
        shuai=code.quality()
        print("%d" % shuai)
        FH = bytearray([0xb2,0xb3,int(long1),0xa2,int(shuai)])
        uart.write(FH)
    if not codes:
        print("FPS %f" % clock.fps())

STM32端

u8 data,a,b;
static u8 step=0;
void USART2_IRQHandler(void)                	//串口1中斷服務程序
{
	
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  //接收中斷(接收到的數據必須是0x0d 0x0a結尾)
   	{USART_ClearITPendingBit(USART2,USART_IT_RXNE);//清除中斷標誌
			data= USART_ReceiveData(USART2);

			switch (step)
										{
											case 0:
															if(data==0xb2)
																	step=1;
															else {step=0;}
																											break;
											case 1:
															if(data==0xb3) 
																	 step=2;
															else {step=0;}
																											break;
											case 2:
															a=data;	
															step=3;
																											break;
											case 3:		if(data==0xa2)	
																		 step=4;
																else step=0;
																											break;
											case 4:		
															b=data;	
															step=0;
																											break;
										}	 			
	   }
			
} 


這些代碼挺簡單的認真看能看懂

  • 注意波特率一致

  • 可以分別調試,先用串口助手調試STM32端看指令是否正確-----再使用openmv打印到串口使用16進制顯示看顯示是否正常

  • 在串口中不要放顯示函數之類的(否則會出現顯示第一個正確·,顯示第二個錯誤的問題)

  • OPWNMV的那個代碼是識別條形碼

  • bytearray是phthon內置函數,大於255的數字拆分然後在STM32端組裝起來

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章