Using Serial.readStringUntil

Hi,

I am trying to send some data over a serial connection (2 arduino's with xbee's) on the receiver side it should be placed in an array/string (still not quite sure what the difference is).

On the internet I found the command "Serial.readStringUntil" and I think is is exactly what I need.
But it looks like I can't find any example codes that could give me some sort of direction on how to use the command?

Does someone have some code I can use as example, or know where I can find a code.

But it looks like I can't find any example codes that could give me some sort of direction on how to use the command?

It's really a simple function to use. It takes one argument - the character who's arrival should terminate the reading of serial data.

It returns a String.

Don't come back here whining when using Strings causes your program to fail. There are other methods of reading serial data that do not use Strings.

Serial Input Basics - updated

Example #2 achieves the same as readStringUntil, example #3 might be the more reliable approach.

Here is an example:

if (mySoftSerial.available()>0)
    {
       String s=mySoftSerial.readStringUntil(/r);   // Until CR (Carriage Return)
       Serial.println(s);
    }

Hope it helps.

Bronson
www.pt-altraman.com

Bronson_alex:
Here is an example:

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Hi Robin2,

Could you elaborate, or point us to a minimal example that reproduces such a memory-corruption issue caused by the String class?

That would be really interesting.

Thanks.

KVNMRSMN:
could give me some sort of direction on how to use the command?

Don't use it.

It should not be used in anything more complex than a primary school assignment. You need the high school code.

See 'Serial.HardwareSerial::read' does not have class type - #8 by sterretje - Programming Questions - Arduino Forum for an example that will show what goes on under the hood; it requires changes in the String 'library'.

deey:
Could you elaborate, or point us to a minimal example that reproduces such a memory-corruption issue caused by the String class?

That would be really interesting.

Sorry, no.

It may be interesting but I can't see that it would be useful.

If you don't believe in my concern then go ahead and use the String class and provide me with a future opportunity to say "I told you so". Who knows, you might get away with it.

...R

String bigAssString;
int i;
void setup() {
  Serial.begin(9600);
}
void loop(){
  bigAssString += i++;
  Serial.println(bigAssString);
}

Serial slows this down so you can see the crash in slow motion. Without that, it will lock up in under a second;

Of course you can do something equally stupid without Strings but it takes a bit more effort.

MorganS:
Of course you can do something equally stupid without Strings but it takes a bit more effort.

The real problem with the String class is that it can bite your Arduino in the ass even when you write sensible code.

...R

If you got a delay on readStringUntil, you can consider using Serial.setTimeout(t)
t is the time in milliseconds, not sure if it helps you but it works for me

@cuteted

I hope that OP was not waiting over two years for your reply.

To anyone trying to make sense of this, the gist is 'use Serial.read because it isn't that much harder and you don't have to deal with String'.

Incidentally, this thread makes me wonder why you'd bother replying to a post just to tell someone you won't help them and assume they're trying to insult you

2 Likes

seagullpat:
Incidentally, this thread makes me wonder why you'd bother replying to a post just to tell someone you won't help them and assume they're trying to insult you

What help do you need?