Serial.readString() Code samples please

Could the friends please post some codes how to use Serial.readString () function ?

I would like my program wait and capture a complete variable lenght string coming from a serial port.
Not checking each character but capturing the whole string at once.

Thanks

What have you tried and where are you stuck ?
How is the string terminated ?

Are you aware of the readStringUntil() function which may be more useful

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

These examples don't tie up the Arduino while waiting for the data to arrive but they do produce an indication of when the full message has been received.

...R

I would like my program wait and capture a complete variable lenght string coming from a serial port.
Not checking each character but capturing the whole string at once.

That is what the code in Robin2's serial input basics tutorial does and without using the problematic String class (see the evils of Strings). It would be good for you to learn to use c_strings (lower case s strings or null terminated character arrays).

c-strings are very error prone and Arduino Strings cause memory fragmentation and have odd errors.

A Safe and Robust and debuggable alternative to c-strings and Arduino Strings is my SafeString library available from the Arduino Library manager (detailed tutorial here). All the SafeString methods are non-blocking.

The SafeString library contains multiple examples of safely reading Serial input, handling /r and /n and /r/n and telnet backspaces and missing end-of-lines.
Check out the following examples included with the library, SafeString_ReadFrom_WriteTo, SafeString_Menu,
SafeString_ReadCmds, SafeString_ReadCmdsTimed and SafeString_readUntil

drmpf:
c-strings are very error prone

Please provide some evidence for that ridiculous claim.

...R

Robin2:
Please provide some evidence for that ridiculous claim.

...R

Check out any of the many buffer overrun coding errors that hackers make use of. Just search on "buffer overrun vulnerability"

This link says "The buffer overflow problem is one of the oldest and most common problems in software development dating back to the introduction of interactive computing. Certain programming languages such as C and C++ are vulnerable to buffer overflow, since they contain no built-in bounds checking or protections against accessing or overwriting data in their memory."

One of the main advantages of SafeStrings is that it checks and prevents buffer overrun and prints an error message if you enable debugging. The other advantage is that SafeString provides the functionally of Arduino Strings without the memory fragmentation or the odd bugs in that class.

As an example of the problems you can get using c-string functions and Arduino Strings check out this straight forward code that caused the ESP32 to reboot every 30sec. That problem was solved using SafeStrings.

drmpf:
This link says "The buffer overflow problem is one of the oldest and most common problems in software development dating back to the introduction of interactive computing. Certain programming languages such as C and C++ are vulnerable to buffer overflow, since they contain no built-in bounds checking or protections against accessing or overwriting data in their memory."

Stupid programmer errors that can be / could have been prevented :smiley: And don't worry, I also made them, usually due to counting incorrectly.

drmpf:
Check out any of the many buffer overrun coding errors that hackers make use of.

That's poor programming. It is not the fault of cstrings.

If the point you are trying to make is that programming cstrings can be tricky, then I agree with you.

...R