2

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

From Arduino I could do,

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

Inside Python script I could do,

import serial
import time

arduino = 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 add arduino.flushInput() just after opening the port.

  • Do you need to "read a value every 5 seconds" or "transmit a value from the Arduino to the host every 5 seconds"? – Ignacio Vazquez-Abrams Jun 20 '15 at 20:38
  • Arduino is writing values to the serial, and I'm reading the serial inside my python script. – whitenoisedb Jun 20 '15 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. – Ignacio Vazquez-Abrams Jun 20 '15 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. – whitenoisedb Jun 20 '15 at 23:00
  • Does it just keep sending them as quickly as possible, and the Python program is supposed to only display the ones that show up every 5 seconds? – Ignacio Vazquez-Abrams Jun 20 '15 at 23:28
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.

Arduino delays

You don't need to deal with the timing delay in python if the Arduino is delaying. According to the documentation readline 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 that readline will block your program from doing anything else while it waits. As such, It is pretty useful to explore python threading when you are writing a program which collects data over a serial stream unless the program is only collecting data.

Python sleeps

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 each loop, and for the python script to only read the data every 5 seconds. In this case you could have python sleep and use no delay (or a shorter delay) on the Arduino. You can achieve this behavior without resorting to using sleep by using a timer object to periodically call a function that opens the serial stream (possibly flush it), reads the data, processes it, and closes the serial stream.

  • Thanks! There was another factor in my Python script, and that was the while True statement. In this simple example, it doesn't matter what timeout value is because readline would be called again on the next loop. However, I guess I should notice that readline is blocking everything else in my script, if there was another thing to do. – whitenoisedb Jun 21 '15 at 18:56
  • I was considering the (remote) possibility of your Arduino and computer being perfectly synchronized, such that the readline 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. – Thismatters Jun 22 '15 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.