How can I filter a garbage completely blank line over my serial data? It comes from the wifi module ESP8266
How I read data:
while (Serial1.available()){
String inData = Serial1.readStringUntil('\n');
Serial.println("Got: " + inData);
}
Here is my common output:
Got: AT+CIPSEND=6
Got:
Got: OK
Got: >
Got: Recv 6 bytes
Got:
Got: SEND OK
Got:
Got: +IPD,21:Volume From: 99 To 89AT+CIPCLOSE
Got: CLOSED
Got:
Got: OK
1 Answer 1
inData.trim();
if( inData.length() > 0 ){
Serial.println("Got: " + inData + "\n");
::trim() removes all leading and trailing whitespace characters including newlines. A line of [zero or more whitespace] + [newline] will get trimmed to the null string. If the result isn't null, then append the stripped off newline and print it. Note that this will flatten any indentation or vertical spacing that was supplied in the incoming text.
-
Worked like charm! Thanks! Since I don't want to process any blank line/newline I just removed the "+ \n" since "println()" does it automaticallyDarkXDroid– DarkXDroid2016年05月25日 17:11:13 +00:00Commented May 25, 2016 at 17:11
-
You're right - missed that. (oops!). Glad you got it working.JRobert– JRobert2016年05月25日 17:19:41 +00:00Commented May 25, 2016 at 17:19
\r
\n
, carriage return and new-line. So effectively, the cr+lf is printed out as two blank lines, one cr and one lf. You may tryString inData = Serial1.readStringUntil('\r');
but other than that, why even bother?