Someone posted a solution for a problem I had which used Serial.read() .Why doesn't readString() seem to work though? From the docs it seems it reads the data as a String object which would make things a bit cleaner.
But here's my Python code:
import serial
arduinoSerialData = serial.Serial('com7', 9600, timeout=0)
while True:
arduinoSerialData.write('ALIVE\r\n')
And Arduino code:
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
if (Serial.readString() == "ALIVE") {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
If this was right, the green LED on the Arduino would light up when the Python program would run, but it doesn't. What is Serial.readString() expecting that it doesn't get?
I even tried if (Serial.readString().startsWith("ALIVE"))
instead, as well as arduinoSerialData.flush()
before arduinoSerialData.write()
1 Answer 1
readString()
will read characters from the serial (or other Stream) device until a timeout occurs. That timeout is, by default, 1 second. It is only appropriate to use readString()
if your data is arriving in chunks with a minimum time between each chunk.
It is more appropriate to use readStringUntil()
which will read characters from the serial device until either it times out, or it receives a certain character. It is most common to use either the line feed (\n
) or carriage return (\r
) characters as the end of string marker. You must, of course, ensure that your sending program sends the string terminated in the right way.
However, that all said, you really should avoid using String objects when possible, since the constant creation and destruction of temporary objects all over the place can make a real mess of your heap.
It is far better to learn to use character arrays and fill them using Serial.read()
. A tutorial I wrote for that is here:
-
Thank you. It's about time I also learned what the timeout is. Is there some quetion/answer about that here already? The Pyserial docs don't seem to mention that. Don't know what "heap" is either.Leo Ervin– Leo Ervin2015年09月24日 15:10:08 +00:00Commented Sep 24, 2015 at 15:10
-
The timeout is a function of the arduino's reading, not the PySerial's transmitting. The heap is an area of memory in the Arduino that is used to dynamically allocate space for variables.Majenko– Majenko2015年09月24日 15:16:15 +00:00Commented Sep 24, 2015 at 15:16
-
Thanks. So timeout is the delay between all the Serial.read() functions in the Arduino code? Sorry, just don't want to start a topic for something small like this.Leo Ervin– Leo Ervin2015年09月24日 15:18:01 +00:00Commented Sep 24, 2015 at 15:18
-
1@LeoErvin The timeout is how long readString waits to receive a character on the serial port before it decides that there are no more to receive. If more than 1 second passes without it having received a character then it finishes reading and returns whatever it has managed to get up until then. It does mean that there will be at least a 1 second delay in your program while it waits for characters to arrive.Majenko– Majenko2015年09月24日 15:22:26 +00:00Commented Sep 24, 2015 at 15:22
-
@LeoErvin If you send one character every 900ms then it will never finish, because it never reaches 1 second while waiting for the next character. If you send 30 characters together, then don't send anything for more than one second, then
readString()
will wait for 1 second after the last character has been received and then return those 30 characters as a string.Majenko– Majenko2015年09月24日 15:23:47 +00:00Commented Sep 24, 2015 at 15:23
readString
work? For example: Pyserial serial.write() doesn't work. I suggest you stick to one thread and work through your issues with the help of people who are genuinely trying to help you.