I have two Strings with reserved memory to handle incoming data from serial. However, after I clean the content of the first one, it shows an extra blank line. Seems it has some end marker or something left and I was not able to clean it. Or the serial buffer has something in it which hold a place but doesn't trigger serial.availalbe().
My code:
String comdata = "";
String bufferS = "";
void setup() {
Serial.begin(9600);
comdata.reserve(60);
bufferS.reserve(60);
}
void loop() {
if (Serial.available())
{
bufferS += char(Serial.read());
Serial.println("bufferS");
Serial.println(bufferS);
if (bufferS.indexOf(",") != -1) {
comdata = bufferS.substring(0, bufferS.length() - 1);
bufferS = "";
}
Serial.println("comdata");
Serial.println(comdata);
}
}
after running it handles the very first input well: after I type "abc,", it shows :
bufferS
a
comdata
bufferS
ab
comdata
bufferS
abc
comdata
bufferS
abc,
comdata
abc
bufferS
comdata
abc
BUT NOTE THE BLANK LINES AFTER THE LAST bufferS.
Then I typed "123,", and it shows:
bufferS
1
comdata
abc
bufferS
12
comdata
abc
bufferS
123
comdata
abc
bufferS
123,
comdata
123
bufferS
comdata
123