Read a string via Serial. Please help

I am coming from C programming and I am used to scanf to grab a single character as well as a complete string. I noticed that i can easily grab a single character using Serial.Read();. How do i grab a string like “This is arduino”. I have tried serial.readstring();read.stringuntil();

Simple answer - lots of Serial.read()s

Look the the code in the demos here and here. The first one is more robust, but a little more complex.

...R

#define HEADER ‘#’
#define TRAILER ‘|’

char inData[16];

char c = Serial.read();

if (c==HEADER){
memset(inData, 0, sizeof(inData)); //clear the old data
char c = Serial.readBytesUntil(TRAILER, inData, sizeof(inData));

}

should work if you only send a text and don´t want to do much with it.

else you can use

val = Serial.parseInt()

so if you send #21/22/23/Arduino is cool|

char c = Serial.read();
if (c==HEADER){
val1=Serial.parseInt();
val2=Serial.parseInt();
val3=Serial.parseInt();
char c = Serial.readBytesUntil(TRAILER, inData, sizeof(inData));

Serial.println(val1);
Serial.println(val2);
Serial.println(val3);
for ( int i = 0; i < sizeof(inData); i++){
Serial.print(inData*);*

  • }*
    That should also work :wink:
    i hope i was able to help you.

I believe that with very a little extra coding, you can also use Serial.ReadBytesUntil (Oh, I see SerialQ beat me to it :slight_smile: )

Below is what I worked on today. The code below does exactly what I want. It can also take in long sentences as well as short sentence and transfers them to a buffer array. My question is what kind of condition should i put in the for loop or any other way to help come out of the loop once I have finished entering my required string. Thank you. By the way the code is in the void setup function.

char string[100];
        char x;
   
    for( int i = 0;;++i)
     {
         while(!Serial.available())
            {
                  //hang until data is entered
               ;
            
            }
    x = Serial.read();
     string[i] = x;
    Serial.print(string);
  }

goodbyeplanet:
Below is what I worked on today. The code below does exactly what I want. It can also take in long sentences as well as short sentence and transfers them to a buffer array. My question is what kind of condition should i put in the for loop or any other way to help come out of the loop once I have finished entering my required string. Thank you. By the way the code is in the void setup function.

As it is, it's a risky code. At the minimum you should add a condition, to end the FOR loop when the index reaches 100.

Now, there are two ways you can handle an "end of string": either 1) measure time and BREAK if no additional character arrives for a (relatively) long time, or 2) set an "end of string" delimiter that the user will have to type, and when you detect that, break out of the loop.

In the Arduino Serial Monitor window, at the bottom there's a combobox where you can select a "line ending" behavior, such as a newline character to be sent automatically. However, you have to make sure you or your users are aware of this option and use it.

i think I have totally failed to capture a string. I have tried to put a string delimiter but nothing works. the only thing that works is if i set my condition to be an exact integer of the length of my string,eg. i < 7 (if my text is 7).

goodbyeplanet:
i think I have totally failed to capture a string. I have tried to put a string delimiter but nothing works. the only thing that works is if i set my condition to be an exact integer of the length of my string,eg. i < 7 (if my text is 7).

Didn’t you say the code did exactly what you want?
Please tell us exactly what input you use, what you expect to get as output and what you actually get.

Hallo Igendel. Thank you so much for trying your best to help. I implemented part 2 to change the combo box and it worked fine after I inserted if(x == '\r' || x =='\n')break;. even though its ok i still think its somehow a bit of a pain to be telling users to select option in the combo box. how would i go about implementing part 1 u suggested to measure time and break loop. is it about setting a timer delay? would be great if u can help.

Thanks again.

goodbyeplanet:
i think I have totally failed to capture a string. I have tried to put a string delimiter but nothing works. the only thing that works is if i set my condition to be an exact integer of the length of my string,eg. i < 7 (if my text is 7).

Have you studied the code in the first demo I linked to in Reply #1 ?

It works.

…R

Habe you tried the Serial.readBytesUntil() linke i mentioned in my post? It works for me and in fact it works really well. I don't See a reason why it shouldn't work in your code... Pls Check my post again.

goodbyeplanet:
Hallo Igendel. Thank you so much for trying your best to help. I implemented part 2 to change the combo box and it worked fine after I inserted if(x == '\r' || x =='\n')break;. even though its ok i still think its somehow a bit of a pain to be telling users to select option in the combo box. how would i go about implementing part 1 u suggested to measure time and break loop. is it about setting a timer delay? would be great if u can help.

Thanks again.

First, you should really try Serial.readBytesUntil(). It handles timeouts itself so you don't have to worry about it (see also Serial.setTimeout()). If you want the final result to be a proper c-style string, just add a '\n' after the last received character.

To handle timing manually, whenever you read a new character you should take note of the time, for example using the millis() function. Then, while waiting for the next character, check again and again if the difference between the current millis() and the time you recorded earlier is bigger than, say, 10ms (depending on your communication rate). If so, break.

Moved to programming questions.