|
本帖最后由 lllllll2b 于 2021-8-11 11:27 编辑
我用OpenMV通过串口发送json格式的数据,在Arduino端也用json接收,但是数据接收延迟有三秒左右。换成串口直接接收就没有问题。有没有大神了解的?
附下代码:
1,OpenMV端的
- import sensor, image, time, json
- from pid import PID
- from pyb import Servo , LED , UART
- uart = UART(3, 115200)
- green=LED(2)
- green.on()
- time.sleep(1)
- green.off()
- red_threshold =(36, 56, 28, 80, 16, 79)
- 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.
- def find_max(blobs):
- max_size=0
- for blob in blobs:
- if blob[2]*blob[3] > max_size:
- max_blob=blob
- max_size = blob[2]*blob[3]
- return max_blob
- while(True):
- #if (uart.any()):
- # print(uart.read())
- clock.tick() # Track elapsed milliseconds between snapshots().
- img = sensor.snapshot() # Take a picture and return the image.
- blobs = img.find_blobs([red_threshold])
- if blobs:
- max_blob = find_max(blobs)
- img.draw_rectangle(max_blob.rect()) # rect
- img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
- time.sleep_ms(500)
- data={
- "cx":max_blob.cx(),
- "cy":max_blob.cy(),
- "size":max_blob.w() * max_blob.h()
- }
- data_out=json.dumps(data)
- uart.write(data_out + '\n')
- print('you_send:',data_out)
- else:
- print('not found!')
复制代码 2,Arduino端的
- #include <ArduinoJson.h>
- #include <SoftwareSerial.h>
- SoftwareSerial softSerial(10, 11); // RX, TX
- char c=0;
- String json="";
- void setup(){
- Serial.begin(115200);
- softSerial.begin(115200);
- }
- void loop(){
- if (softSerial.available() > 0) {
- c = char(softSerial.read());
- json = String(json) + String(c);
- if (c == '}') {
- DynamicJsonDocument doc(200); //声明一个JsonDocument对象
- deserializeJson(doc, json);
- JsonObject obj = doc.as<JsonObject>();
- int cx = doc["cx"];
- int cy = doc["cy"];
- if (cx!=0 && cy!=0)
- {
- Serial.print("cx = ");
- Serial.print(cx);
- Serial.print(" cy = ");
- Serial.println(cy);
- }
- json = "";
- }
- }
- }
复制代码 这个程序接收数据很慢
3,下面的程序是Arduino直接用串口读取的
- #include <ArduinoJson.h>
- #include <SoftwareSerial.h>
- SoftwareSerial mySerial(10, 11); // RX, TX
- void setup() {
- Serial.begin(115200);
- mySerial.begin(115200);
- }
- void loop() { // run over and over
- if (mySerial.available()) {
- Serial.write(mySerial.read());
- }
- if (Serial.available()) {
- mySerial.write(Serial.read());
- }
- }
复制代码 这个串口读取就很快,但是会有乱码,而且读取速度也没有想象中的快
各位大神有了解的吗?
|
|