2

I'm not sure about how to properly handle timing readings from serial inside a Python script. I need to read a value every5 seconds.

From Arduino I could do,

Serial.println(value);delay(5000); // should I wait here?

Inside Python script I could do,

import serialimport timearduino = serial.Serial('/dev/ttyACM1', 9600, timeout=5) # should I set timeout?while True and arduino.isOpen():    try:            value = arduino.readline().strip()        print value        time.sleep(5) # should I wait here?    except KeyboardInterrupt:        print "\nClosing..."        arduino.close()

EDIT: Apart from that, the first reading is always wrong, like if there was something in the reading buffer.I've found out that to solve that I should addarduino.flushInput() just after opening the port.

askedJun 20, 2015 at 20:29
whitenoisedb's user avatar
6
  • Do you need to "read a value every 5 seconds" or "transmit a value from the Arduino to the host every 5 seconds"?CommentedJun 20, 2015 at 20:38
  • Arduino is writing values to the serial, and I'm reading the serial inside my python script.CommentedJun 20, 2015 at 20:45
  • 1
    Yes, we all get that. What we need to know is which one needs to be in control of the timing.CommentedJun 20, 2015 at 20:46
  • Sorry, thanks for replying. I'm not sure which, what is the proper way to do this simple stuff? I mean, my sketch just sends temperature values, it's the only thing it does. My python script would read those values and print them every 5 seconds.CommentedJun 20, 2015 at 23:00
  • Does it justkeep sending them as quickly as possible, and the Python program is supposed to only display the ones that show up every 5 seconds?CommentedJun 20, 2015 at 23:28

1 Answer1

3

The answer is that it depends on what you are actually trying to accomplish. If there is nothing else to be done on the Arduino then it is fine to have it sit and wait; on the other hand if there is nothing else for the python script to do then it is fine for it to sit and wait, but there is no reason for both of them wait.

Arduinodelays

You don't need to deal with the timing delay in python if the Arduino isdelaying. According tothe documentationreadline will wait until it has received a value or until the timeout is reached. That said, I would make the timeout slightly longer than the delay on the Arduino side of things.

Note thatreadline will block your program from doing anything else while it waits. As such, It is pretty useful to explore pythonthreading when you are writing a program which collects data over a serial stream unless the program isonly collecting data.

Pythonsleeps

If there is other stuff to be done on the Arduino (perhaps if you are doing some filtering of the temperature data using the Arduino so as to reduce noise) then it might make more sense for it to push data to the serial stream at the end of eachloop, and for the python script to only read the data every 5 seconds. In this case you could have pythonsleep and use no delay (or a shorter delay) on the Arduino. You can achieve this behavior without resorting to usingsleep by using atimer object to periodically call a function thatopens the serial stream (possibly flush it), reads the data, processes it, and closes the serial stream.

answeredJun 21, 2015 at 18:27
Thismatters's user avatar
2
  • Thanks! There was another factor in my Python script, and that was thewhile True statement. In this simple example, it doesn't matter what timeout value is becausereadline would be called again on the next loop. However, I guess I should notice thatreadline is blocking everything else in my script, if there was another thing to do.CommentedJun 21, 2015 at 18:56
  • I was considering the (remote) possibility of your Arduino and computer being perfectly synchronized, such that thereadline times out after reading half of the line, and then immediately reads the second half of the line on the next iteration of the loop. This is an admittedly far-fetched possibility, but by making your timeout longer than 5 seconds you limit the likelihood of it.CommentedJun 22, 2015 at 5:04

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.