Can't clean String?

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

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

UKHeliBob:
Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

I have tried that, but mine browser might have some issue with that, so I don't see any buttons with that tag. So it is possible to manually add? Like < code/> or </> code </>?

Type [­code] before the code and [­/code] after it or use Copy for Forum from a right click in the IDE and paste into the forum editor complete with code tags

Turn off line ending in the Serial Monitor :wink:

septillion:
Turn off line ending in the Serial Monitor :wink:

oh no....

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.