最近一段时间都在捣鼓OpenMV和Stm32的通信问题,刚开始不知道哪里出了问题,一直通信失败,明明使用TTL串口接收OpenMv发送的数据是可以在串口调试助手上显示的,但就是无法发给Stm32的USART串口。经过了差不多一周的时间,终于解决了。于是在这里记录学习记录。
一、OpenMv配置1.第一种发送方法OpenMv代码如下 - # Untitled - By: 86188 - 周二 5月 25 2021
- import sensor, image, time,pyb
- from pyb import UART,LED
- import json
- import ustruct
- #white_threshold_01 = ((95, 100, -18, 3, -8, 4)); #白色阈值
- red_threshold_01 = ((2, 51, 11, 127, -128, 127)) #红色阈值
- LED_R = pyb.LED(1) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
- LED_G = pyb.LED(2)
- LED_B = pyb.LED(3)
- LED_R.on()
- LED_G.on()
- LED_B.on()
- 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()
- LED_R.off()
- LED_G.off()
- LED_B.off()
- uart = UART(3,115200) #定义串口3变量
- uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
- def find_max(blobs): #定义寻找色块面积最大的函数
- max_size=0
- for blob in blobs:
- if blob.pixels() > max_size:
- max_blob=blob
- max_size = blob.pixels()
- return max_blob
- def send_data_packet(x,y,z,w):
- temp = ustruct.pack("<bbhhhh", #格式为俩个字符俩个整型
- 0x2C, #帧头1
- 0x12, #帧头2
- int(x), # up sample by 2 #数据1
- int(y), # up sample by 2 #数据2
- int(z),
- int(w))
- #0x5B)
- uart.write(temp); #串口发送
- while(True):
- img = sensor.snapshot()
- #time.sleep_ms(1000)
- #send_data_packet(x,y)
- blobs = img.find_blobs([red_threshold_01]);
- cx=0;cy=0;
- if blobs:
- #如果找到了目标颜色
- max_b = find_max(blobs);
- # Draw a rect around the blob.
- img.draw_rectangle(max_b[0:4]) # rect
- #用矩形标记出目标颜色区域
- img.draw_cross(max_b[5], max_b[6]) # cx, cy
- #img.draw_cross(160, 120) # 在中心点画标记
- #在目标颜色区域的中心画十字形标记
- cx=max_b[5];
- cy=max_b[6];
- cw=max_b[2];
- ch=max_b[3];
- #data = bytearray([x,y])
- send_data_packet(cx,cy,cw,ch)
- #time.sleep_ms(1000)
|