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– Ignacio Vazquez-Abrams2015年06月20日 20:38:39 +00:00Commented Jun 20, 2015 at 20:38
-
Arduino is writing values to the serial, and I'm reading the serial inside my python script.whitenoisedb– whitenoisedb2015年06月20日 20:45:00 +00:00Commented Jun 20, 2015 at 20:45
-
1Yes, we all get that. What we need to know is which one needs to be in control of the timing.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2015年06月20日 20:46:39 +00:00Commented Jun 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.whitenoisedb– whitenoisedb2015年06月20日 23:00:09 +00:00Commented Jun 20, 2015 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– Ignacio Vazquez-Abrams2015年06月20日 23:28:21 +00:00Commented Jun 20, 2015 at 23:28
1 Answer 1
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 delay
s
You don't need to deal with the timing delay in python if the Arduino is delay
ing. 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 sleep
s
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 open
s 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 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.whitenoisedb– whitenoisedb2015年06月21日 18:56:04 +00:00Commented Jun 21, 2015 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– Thismatters2015年06月22日 05:04:53 +00:00Commented Jun 22, 2015 at 5:04