简体   繁体   English

Pyserial无法从Arduino获得新价值

[英]Pyserial not getting new value from Arduino

I'm trying to stream the data from an ultrasonic rangefinder on an Arduino to my laptop. 我正在尝试将数据从Arduino上的超声波测距仪流式传输到我的笔记本电脑。 I seem to be having an issue with getting the new value from the Arduino. 我似乎在从Arduino获得新价值时遇到问题。 When I launch the Python script it starts printing the data just like I hoped. 当我启动Python脚本时,它开始打印数据,就像我希望的那样。 However this data does not change when I change the distance the sensor sees, it's almost like the serial.readline() is stuck on one of the first values. 但是,当我更改传感器看到的距离时,此数据不会更改,几乎就像serial.readline()停留在第一个值之一上。 I'm new to this serial communication stuff so any help is greatly appreciated! 我是这个串行通信的新手,所以非常感谢您的帮助!

The code is below and for sanity I did check that the sensor is working with the serial monitor in the Arduino IDE. 代码在下面,为了清楚起见,我确实检查了传感器是否与Arduino IDE中的串行监视器一起使用。

import numpy
import serial 
import time
import sys
import cv2
import pickle 

#set up the camera stuff
cap = cv2.VideoCapture(0)

#container for images
images=[]

#container for distances
distances=[]

#first frame number
frame_num=1

#setup the serial connection and pause to establish it
ser = serial.Serial('/dev/cu.usbmodem1421', 9600,timeout=1)

time.sleep(5)

while True:
    try:
        #grab and image
        ret,frame=cap.read()

        #grab the distance
        distance=ser.readline()
        print(distance)

        #process the image to a gray and 1241,376
        #gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        #gray_resized=cv2.resize(gray,(1241,376))

        #cv2.imshow("FRAME",gray_resized)
        #print(distance)

        #images.append([frame_num,gray_resized])
        #distances.append([frame_num,distance])

        #ser.flush()

    except KeyboardInterrupt:
        #pickle.dump( images, open( "save.p", "wb" ) )
        #pickle.dump( distances, open( "save.p", "wb" ) )
        sys.exit()

Arduino code: Arduino代码:

// defines pins numbers
const int trigPin = 7;
const int echoPin = 8;

// defines variables
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
  delayMicroseconds(50);
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance in CM
  distance= duration*0.034/2;
  String distance_out=String(distance);

  // Prints the distance on the Serial Monitor in CM
  Serial.println(distance);
  Serial.flush();

  //Serial.print("Distance: ");
  //Serial.println(distance);
}

Actually, python's serial.readline() is blocking until it get a EOL, so if you do not have lock issue that mean the Arduino is writing in the buffer faster that your python script read it. 实际上,python的serial.readline()在获得EOL之前一直处于阻塞状态,因此,如果您没有锁定问题,则意味着Arduino在缓冲区中写入的速度比python脚本读取它的速度更快。

You should flush the buffer after reading to ensure (close to) realtime reading with serial.flushInput() or serial.reset_input_buffer() depending of your version 阅读,以确保(接近)实时与阅读之后,应该刷新缓冲区serial.flushInput()serial.reset_input_buffer()这取决于你的版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM