Go Down

Topic: Using Serial.readStringUntil (Read 28932 times) previous topic - next topic

KVNMRSMN

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.

PaulS

Quote
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.

sterretje

Serial Input Basics - updated

Example #2 achieves the same as readStringUntil, example #3 might be the more reliable approach.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Bronson_alex

Here is an example:

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


Hope it helps.

Bronson
www.pt-altraman.com

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

deey

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.

MorganS

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.
"The problem is in the code you didn't post."

sterretje

See https://forum.arduino.cc/index.php?topic=539995.msg3680822#msg3680822 for an example that will show what goes on under the hood; it requires changes in the String 'library'.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Robin2

#8
Jun 19, 2019, 08:21 am Last Edit: Jun 19, 2019, 08:23 am by 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.
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
Two or three hours spent thinking and reading documentation solves most programming problems.

MorganS

Code: (Don't do this) [Select]
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.
"The problem is in the code you didn't post."

Robin2

#10
Jun 20, 2019, 09:28 am Last Edit: Jun 20, 2019, 09:29 am by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

cuteted

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

sterretje

@cuteted

I hope that OP was not waiting over two years for your reply.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

seagullpat

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

aarg

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?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Go Up