logo

OPENMV与arduino串口通信问题?

2019-04-17 21:09
C
rlms

我是想让openmv发送1给arduino的串口RX2TX2

这是openmv代码

# Blob Detection and uart transport
import sensor, image, time
from pyb import UART
import json
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
blue_threshold   = (92, 16, -31, 9, -40, -17)
# 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_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
sensor.set_hmirror(True)
sensor.set_vflip(True)

uart = UART(3, 9600)
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

while(True):
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([blue_threshold])
    if blobs:
        max_blob=find_max(blobs)
        print('sum :', len(blobs))
        img.draw_rectangle(max_blob.rect())
        img.draw_cross(max_blob.cx(), max_blob.cy())

        output_str="[%d]" % (1) #方式1
        #output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
        print('you send:',output_str)
        uart.write(output_str+'\r\n')
    else:
        print('not found!')

这是arduino代码

int a;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial2.begin(9600);
}

void loop() {
  Serial2.read();
   a = Serial2.read();//读取串口接收到的值,并且存放在a中保存起来
   Serial.print(a);
   Serial.print('\n');
   
}

在openmv没有打开时是低电平-1

86a7111f-5839-4df7-b0f9-900a342c2c7e.png

打开openmv时接收到的不是1而是乱码

e66fd759-a848-47a6-acd1-801ea11463ae.png

求大佬解释一下我应该怎么做

2019-04-18 09:40
I
网红小智智 星瞳科技

OpenMV发送的是字符串

[1]\r\n

所以你的Arduino中,应该用readStringUntil('\r\n')