简体   繁体   中英

Pyserial not getting new value from Arduino

I'm trying to stream the data from an ultrasonic rangefinder on an Arduino to my laptop. I seem to be having an issue with getting the new value from the Arduino. When I launch the Python script it starts printing the data just like I hoped. 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. 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.

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:

// 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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