Serial communication II

Serial communication II

OpenMV串口调试扩展板将OpenMV的串口UART3,通过cp2104芯片连接到电脑,通过USB连接到串口调试扩展板,可以看到OpenMV的串口UART3上发送的数据。

https://singtown.com/product/49906

OpenMV和串口调试扩展板,通过2条USB线,都连接到电脑。

Module TTL-USB

如果没有OpenMV串口调试扩展板,也可以使用TTL-USB模块,他的作用和OpenMV串口调试扩展板一样。

First of all, we need to know:all kinds of single chip(including Arduino, OpenMV, esp8266, stm32, 51) use serial TTL instead of rs485 or rs232.

Serial TTL’s voltage is 3.3V or 5V,RS 232’s voltage is +-15V,and RS485’s voltage is 5V,but two data line is differential line with different pact so that it can not be universal.

We use module TTL to USB(recommend using module FTDI,a little more expensive but with better quality)

Cording diagram(pay attention to common-base and RXTX shouold be linked crisscross

OpenMV FTDI
P4(TX) RX
P5(RX) TX
GND GND

Insert USB end of module TTL into computer,there would be a serial,open serial helper(or serial helper of OpenMV)

星瞳串口助手

首先下载星瞳串口助手:
https://singtown.com/singtownserialport/

星瞳串口助手是一款,简洁易用,支持Windows,MacOS,Linux的开源的串口助手。

运行程序

Attention:some software can choose HEX(hexadecimal) or ASC(ascii).If only choose ASC,string can be shown.

Run procedure uart.write("hello world!") in OpenMV(see it on the last section)

String Hello World will show on serial helper.

If run the following procedure:

import sensor, image, time
import json
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_auto_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))
        data=[]
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b.rect()) # rect
            img.draw_cross(b.cx(), b.cy()) # cx, cy
            data.append((b.cx(),b.cy()))

        #{(1,22),(-3,33),(22222,0),(9999,12),(0,0)}
        data_out = json.dumps(set(data))
        uart.write(data_out +'\n')
        print('you send:',data_out)
    else:
        print("not found!")

Will send the central coordinate of all the color blobs.

星瞳串口助手会显示接收到的数据:

Arduino parse code

Since Arduino Uno has only one serial to receive,there is no other ways to send to computer to display.Thus we use software to simulate serial to process procedure of transmitting serial.

OpenMV Arduino
P4(TX) 10(RX)
P5(RX) 11(TX)
GND GND

Transmitting logic is like this:data of OpenMV is sent to sofe serial of Arduino Uno,then serial of Arduino link to computer showing results.

In this way,ArduinoMega’s logic is:reading data of softSerial(json),then analyze it to array sending to Serial(computer).

Code

#include <SoftwareSerial.h>

SoftwareSerial softSerial(10, 11); // RX, TX
typedef struct
{
  int data[50][2] = {{0,0}};
  int len = 0;
}List;
List list;

void setup() {
  // put your setup code here, to run once:
  softSerial.begin(115200);
  Serial.begin(115200);
}

void loop() {
  if(softSerial.available())
  {
    getList();
    for (int i=0; i<list.len; i++)
    {
      Serial.print(list.data[i][0]);
      Serial.print('\t');
      Serial.println(list.data[i][1]);
    }
    Serial.println("============");
    clearList();
  }

}


String detectString()
{
  while(softSerial.read() != '{');
  return(softSerial.readStringUntil('}'));
}
void clearList()
{
  memset(list.data, sizeof(list.data),0);
  list.len = 0;
}
void getList()
{
  String s = detectString();
  String numStr = "";
  for(int i = 0; i<s.length(); i++)
  {
    if(s[i]=='('){
      numStr = "";
    }
    else if(s[i] == ','){
      list.data[list.len][0] = numStr.toInt();
      numStr = "";
    }
    else if(s[i]==')'){
      list.data[list.len][1] = numStr.toInt();
      numStr = "";
      list.len++;
    }
    else{
      numStr += s[i];
    }
  }
}

results matching ""

    No results matching ""