From 485be11e92e5a600a576a9789f7f269b8b09703c Mon Sep 17 00:00:00 2001 From: grayconstruct <77263741+grayconstruct@users.noreply.github.com> Date: Mon, 11 Jan 2021 02:34:04 -0600 Subject: [PATCH 1/4] Add example and notes and warnings sections A very common mistake is not expecting readString value to have invisible \r \n characters. --- .../Communication/Serial/readString.adoc | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Language/Functions/Communication/Serial/readString.adoc b/Language/Functions/Communication/Serial/readString.adoc index 3e6822e2a..31af1ed96 100644 --- a/Language/Functions/Communication/Serial/readString.adoc +++ b/Language/Functions/Communication/Serial/readString.adoc @@ -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 because of end of line characters and 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] -- From 1f5519bb43228799f874fc7b52a001f2fbfecea8 Mon Sep 17 00:00:00 2001 From: grayconstruct <77263741+grayconstruct@users.noreply.github.com> Date: Tue, 16 Aug 2022 14:07:25 -0500 Subject: [PATCH 2/4] Update Language/Functions/Communication/Serial/readString.adoc Co-authored-by: per1234 --- Language/Functions/Communication/Serial/readString.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Language/Functions/Communication/Serial/readString.adoc b/Language/Functions/Communication/Serial/readString.adoc index 31af1ed96..503f4294b 100644 --- a/Language/Functions/Communication/Serial/readString.adoc +++ b/Language/Functions/Communication/Serial/readString.adoc @@ -55,7 +55,7 @@ void setup() { void loop() { Serial.println("Enter data:"); - while(Serial.available()==0); //wait for data available + 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") { From 71a507b145e941c3be18530259fcc70d320986a1 Mon Sep 17 00:00:00 2001 From: grayconstruct <77263741+grayconstruct@users.noreply.github.com> Date: Tue, 16 Aug 2022 14:08:03 -0500 Subject: [PATCH 3/4] Update Language/Functions/Communication/Serial/readString.adoc Co-authored-by: per1234 --- Language/Functions/Communication/Serial/readString.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Language/Functions/Communication/Serial/readString.adoc b/Language/Functions/Communication/Serial/readString.adoc index 503f4294b..5e730467b 100644 --- a/Language/Functions/Communication/Serial/readString.adoc +++ b/Language/Functions/Communication/Serial/readString.adoc @@ -56,9 +56,9 @@ void setup() { 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") { + 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"); From 2cd5ac08a17550a3d827557c97fcc26c4926506a Mon Sep 17 00:00:00 2001 From: grayconstruct <77263741+grayconstruct@users.noreply.github.com> Date: Tue, 16 Aug 2022 14:09:06 -0500 Subject: [PATCH 4/4] Update Language/Functions/Communication/Serial/readString.adoc Co-authored-by: per1234 --- Language/Functions/Communication/Serial/readString.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Language/Functions/Communication/Serial/readString.adoc b/Language/Functions/Communication/Serial/readString.adoc index 5e730467b..c0fa8856c 100644 --- a/Language/Functions/Communication/Serial/readString.adoc +++ b/Language/Functions/Communication/Serial/readString.adoc @@ -70,7 +70,7 @@ void loop() { [float] === Notes and Warnings -The function does not terminate early because of end of line characters and the returned String may contain carriage return and/or line feed characters if they were received. +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] --