>>> 星瞳科技|OpenMV中国官方网站 <<<

>>> 星瞳科技店铺地址|OpenMV中国官方代理,全球九大代理商之一 <<<

>>> OpenMV3 Cam M7 官方高配版上市啦~戳我戳我戳我~ <<<

>>> 星瞳科技-OpenMV中文教程网 <<<

2016-7-25 更新:更改代码,使之输出坐标组
这节介绍一下OpenMV的串口通信。

为什么要用串口呢?因为要时候需要把信息传给其他MCU,串口简单,通用,基本每一个MCU都会有串口。

TTL串口至少需要3根线:TXD,RXD,GND。TXD是发送端,RXD是接收端,GND是地线。
连线的时候,需要把OpenMV的RXD连到另一个MCU的TXD,TXD连到RXD。图示:

好,我们来看一下示例程序:

# UART Control
#
# This example shows how to use the serial port on your OpenMV Cam. Attach pin
# P4 to the serial input of a serial LCD screen to see "Hello World!" printed
# on the serial LCD display.

import time
from pyb import UART

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 19200)

while(True):
    uart.write("Hello World!\r")
    time.sleep(1000)

看上去很简单是不是,先实例化一个19200波特率的串口3(这里说一下,必须是3,因为板上只引出了这个串口,pyb的串口有好多个的),然后调用write方法就可以了。

重点强调一下,write里只能是字符串,不能传入整数或者浮点数,否则会按照ASCII码来处理,可以直接str(12)来把整数12转为字符串'12'。

好的,下面我分享一个程序,可以把视野中的黄色区域通过串口输出。

# Blob Detection and uart transport
import sensor, image, time
from pyb import UART
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold   = ( 46,  100,  -68,   72,   58,   92)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.

uart = UART(3, 115200)

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([yellow_threshold])
    if blobs:
        print('sum : %d'% len(blobs))
        out_str = '['
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b[0:4]) # rect
            img.draw_cross(b[5], b[6]) # cx, cy
            x = b[5]
            y = b[6]
            out_str += '(%d,%d)' % (x,y)

        out_str += ']'

        #[(1,22)(-3,33)(22222,0)(9999,12)(0,0)]
        uart.write(out_str+'\r\n')
        print(out_str)

    #print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.

我直接用USB-TTL把结果显示在电脑上。
结果如下:

串口输出如下:

下节我更新一下Arduino接收的代码。