user13501: 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()

user4143: 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:

http://hacking.majenko.co.uk/reading-serial-on-the-arduino


13501: <p>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.</p>

<p>But here's my Python code:</p>

<pre><code>import serial

arduinoSerialData = serial.Serial('com7', 9600, timeout=0)

while True:
    arduinoSerialData.write('ALIVE\r\n')
</code></pre>

<p>And Arduino code:</p>

<pre><code>void setup() {
  Serial.begin(9600);

  pinMode(13, OUTPUT);

}

void loop() {
    if (Serial.readString() == "ALIVE") {
        digitalWrite(13, HIGH);
    } else {
        digitalWrite(13, LOW);
    }
}
</code></pre>

<p>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 <code>if (Serial.readString().startsWith("ALIVE"))</code> instead, as well as <code>arduinoSerialData.flush()</code> before <code>arduinoSerialData.write()</code></p>
 4143: <p><code>readString()</code> 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 <code>readString()</code> if your data is arriving in chunks with a minimum time between each chunk.</p>

<p>It is more appropriate to use <code>readStringUntil()</code> which will read characters from the serial device until <em>either</em> it times out, <em>or</em> it receives a certain character.  It is most common to use either the line feed (<code>\n</code>) or carriage return (<code>\r</code>) characters as the end of string marker.  You must, of course, ensure that your sending program sends the string terminated in the right way.</p>

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

<p>It is far better to learn to use character arrays and fill them using <code>Serial.read()</code>.  A tutorial I wrote for that is here:</p>

<ul>
<li><a href="http://hacking.majenko.co.uk/reading-serial-on-the-arduino" rel="nofollow">http://hacking.majenko.co.uk/reading-serial-on-the-arduino</a></li>
</ul>
