openmv4arduino_uart arduino串口


# OpenMV 和 Arduino Merge2560 基本uart通信 # 1) OpenMV Cam 與 Arduino Uno 按如下連線: #定義了軟串口 # OpenMV Cam Ground Pin ----> Arduino Ground # OpenMV Cam UART3_TX(P4) ----> Arduino Uno UART_RX(10) # OpenMV Cam UART3_RX(P5) ----> Arduino Uno UART_TX(11)


openmv代碼:
import  ustruct, time
from pyb import UART

text = "Hello World!\n"
#data = ustruct.pack("<bi%ds" % len(text), 85, len(text), text) # 85 is a sync char.

# 使用 "ustruct" 來生成需要發送的數據包
# "<" 把數據以小端序放進struct中
# "b" 把一個 signed char 放進數據流
# "i" 把一個 signed integer 放進數據流
# "%ds" 把字符串放進數據流,比如:"13s" 對應的 "Hello World!\n" (13 chars).
# 詳見 https://docs.python.org/3/library/struct.html


# 零填充數據為4字節加4字節的倍數。
#data += "\x00" * (4 + (len(data) % 4))



# UART 3, and baudrate.
uart = UART(3, 19200)

while(True):
    uart.write(text)
    if (uart.any()):
        print(uart.read())
    time.sleep(500)       #延時的准確設定很重要

 

arduino代碼:

#include <SoftwareSerial.h>

SoftwareSerial softSerial(10, 11); // RX, TX

void setup() {
  // 初始化串口
  softSerial.begin(19200);
  Serial.begin(19200);
  
}

/*//每次只獲取單個字符
void loop() {
  // 讀取輸入的信息
  char byteRead;
  if(softSerial.available()){
  byteRead=softSerial.read();
  // 輸出信息
  Serial.println(byteRead);
  softSerial.write(byteRead);
  delay(1000); 
  }
}
*/

//獲取字符串函數
void loop(){
    int32_t temp = 0;
    char buff[100] = {0};
    //String byteRead;
    if(softSerial.available()){
      while (softSerial.available()) buff[temp++] = softSerial.read();
      // 輸出信息
      //byteRead= softSerial.read();
      Serial.println(buff);
      softSerial.write(buff);
      delay(1000); 
  }
}

 

openmv運行效果:


arduino運行效果:




本文章實現了openmv4與arduino的相互通信,各自都能給對方發送並接受Hello World!字符串。







正是步行者,一步步登峰!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM