Skip to content

Commit

Permalink
Add readString example and notes/warnings re: line endingssections (#808
Browse files Browse the repository at this point in the history
)

A very common mistake is not expecting readString value to have invisible \r \n characters.
  • Loading branch information
grayconstruct authored Aug 16, 2022
1 parent ac98490 commit f7296fb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Language/Functions/Communication/Serial/readString.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,49 @@ title: Serial.readString()
=== Returns
A `String` read from the serial buffer


--
// OVERVIEW SECTION ENDS


// HOW TO USE SECTION STARTS
[#howtouse]
--

[float]
=== Example Code
Demonstrate Serial.readString()

[source,arduino]
----
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Enter data:");
while (Serial.available() == 0) {} //wait for data available
String teststr = Serial.readString(); //read until timeout
teststr.trim(); // remove any \r \n whitespace at the end of the String
if (teststr == "red") {
Serial.println("A primary color");
} else {
Serial.println("Something else");
}
}
----
[%hardbreaks]


[float]
=== Notes and Warnings
The function does not terminate early if the data contains end of line characters. The returned `String` may contain carriage return and/or line feed characters if they were received.
[%hardbreaks]

--
// HOW TO USE SECTION ENDS


// SEE ALSO SECTION
[#see_also]
--
Expand Down

0 comments on commit f7296fb

Please sign in to comment.