# Read line from Serial

**URL:** <https://forum.arduino.cc/t/read-line-from-serial/98251>\
**Category:** Programming\
**Created:** [April 8, 2012, 1:21pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251 "2012-04-08T13:21:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulE](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@PaulE](https://forum.arduino.cc/u/PaulE)\
**Post date:** [April 8, 2012, 1:21pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/1 "2012-04-08T13:21:17Z")

</div>

Hello,

I want to be able to control a RGB led over serial by writing different Analog values.  
The problem is that Serial.read() only returns one value a time.

What would be the best method to process data which is sent like:  
r=0  
g=255  
b=255

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 8, 2012, 1:23pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/2 "2012-04-08T13:23:16Z")

</div>

> What would be the best method to process data which is sent like:

One character at a time.

---

<div class="post-metadata">

**Author:** ![PaulE](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@PaulE](https://forum.arduino.cc/u/PaulE)\
**Post date:** [April 8, 2012, 1:28pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/3 "2012-04-08T13:28:23Z")

</div>

> PaulS:
> 
> > What would be the best method to process data which is sent like:
> 
> One character at a time.

Yes, I could save these characters in a buffer, but how will I be able to convert the buffer to a int which can be used for PWM.

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [April 8, 2012, 1:49pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/4 "2012-04-08T13:49:48Z")

</div>

null terminate it and use atoi

---

<div class="post-metadata">

**Author:** ![zoomkat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/zoomkat/32/1190177_2.png) [@zoomkat](https://forum.arduino.cc/u/zoomkat)\
**Post date:** [April 8, 2012, 3:56pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/5 "2012-04-08T15:56:05Z")

</div>

You can capture the values as a string, and then convert the string into a number. The below shows how to capture a string and convert it into a number for use for servo control. If you can attach a delimiter to each value sent to keep the values seperate (like using a comma, 254,128,200,90,), then possibly simpler code can be used.

```cpp
// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

```

---

<div class="post-metadata">

**Author:** ![thenamefirst](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@thenamefirst](https://forum.arduino.cc/u/thenamefirst)\
**Post date:** [January 9, 2015, 4:31pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/6 "2015-01-09T16:31:59Z")

</div>

'readString' was not declared in this scope  
How to solve it?

---

<div class="post-metadata">

**Author:** ![Arank](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@Arank](https://forum.arduino.cc/u/Arank)\
**Post date:** [January 9, 2015, 4:50pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/7 "2015-01-09T16:50:05Z")

</div>

> thenamefirst:  
> 'readString' was not declared in this scope  
> How to solve it?

You probably messed something when selecting / coping, here it works out of the box.

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [January 9, 2015, 4:50pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/8 "2015-01-09T16:50:37Z")

</div>

Personally, I would use strtok. Read and buffer the entire line, then parse it, something like this (untested...):

```cpp
char buffer [32];
int cnt = 0;
boolean ready = false;
int rgbval, red, green, blue;

void ParseLine
{
char key[8];
char value[8];

    key = strtok(buffer, "=");    // Everything up to the '=' is the color name
    value = strtok(NULL, "\n");  // Everything else is the color value
    if ((key != NULL) && (value != NULL))
    {
        rgbval = atoi(value);
        switch (toupper(key))
        {
            case 'R'
                red = rgbval;
                break;
            case 'G'
                green = rgbval;
                break;
            case 'B'
                blue = rgbval;
                break;
        }
    }
}

void loop()
{
    if (ready)
    {
        ParseLine();
        ready = false;
    } else while (Serial.available())
    {
        char c = Serial.read();
        buffer[cnt++] = c;
        if ((c == '\n') || (cnt == sizeof(buffer)-1))
        {
            buffer[cnt] = '\0';
            cnt = 0;
            ready = true;
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![thenamefirst](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@thenamefirst](https://forum.arduino.cc/u/thenamefirst)\
**Post date:** [January 9, 2015, 5:46pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/9 "2015-01-09T17:46:25Z")

</div>

thx

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 9, 2015, 5:59pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/10 "2015-01-09T17:59:58Z")

</div>

The examples in [serial input basics](http://forum.arduino.cc/index.php?topic=288234.0) should do what you need.

...R

---

<div class="post-metadata">

**Author:** ![thenamefirst](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@thenamefirst](https://forum.arduino.cc/u/thenamefirst)\
**Post date:** [January 9, 2015, 6:05pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/11 "2015-01-09T18:05:31Z")

</div>

why i adapt this code to use like this:  
#include \<Servo.h\>  
#include \<SPI.h\>  
#include \<SD.h\>  
const int chipSelect = 4;  
File myFile;  
String readString, servo1, servo2;

void setup() {  
Serial.begin(9600);

while (!Serial)  
{  
; //  
}  
Serial.print("Initializing SD card...");  
if (!SD.begin(chipSelect)) {  
Serial.println("initialization failed!");  
return;  
}  
Serial.println("initialization done.");  
myFile = SD.open("FIRST1.txt");  
if (myFile)  
{  
Serial.println("FIRST1.txt:");  
}  
else  
{  
Serial.println("error opening test.txt");  
}  
}

void loop() {  
myFile = SD.open("LL.txt");  
if (myFile){  
delay(3); //delay to allow buffer to fill  
if (myFile.available() \>0) {  
char c = myFile.read(); //gets one byte from serial buffer  
readString += c; //makes the string readString  
}  
if (readString.length() \>0) {  
Serial.println(readString); //see what was received

}  
}

}  
and my SD card have 12345678 data  
but the result was  
1  
11  
111  
1111  
11111  
111111  
1111111

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 9, 2015, 8:36pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/12 "2015-01-09T20:36:16Z")

</div>

> thenamefirst:  
> why i adapt **this** code to use like this:

What do you mean by "this" ? What was the original code before you changed it?

...R

---

<div class="post-metadata">

**Author:** ![max\_saeta](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/max_saeta/32/218102_2.png) [@max\_saeta](https://forum.arduino.cc/u/max_saeta)\
**Post date:** [January 10, 2015, 12:26am UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/13 "2015-01-10T00:26:33Z")

</div>

Greetings.

Try this code

```cpp
#define red  9
#define green 10
#define blue 11

int tblue = 0;
int tgreen = 0;
int tred = 0;

void setup() {
  pinMode(blue, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  Serial.begin(115200);
  color(255,0,0);

}

void loop() {
  if(Serial.available()>0){
    tred=Serial.parseInt();
    tgreen=Serial.parseInt();
    tblue=Serial.parseInt();
    color(tred,tgreen,tblue);
    Serial.println("red " + (String)tred + " green  " + (String)tgreen + " blue " + (String)tblue);
  }
 
}

void color(int cred, int cgreen, int cblue) {
  analogWrite(red, cred);
  analogWrite(green, cgreen);
  analogWrite(blue, cblue);
}

```

Sending 0,255,255 for r,g,b

---

<div class="post-metadata">

**Author:** ![thenamefirst](https://avatars.discourse-cdn.com/v4/letter/t/b2d939/32.png) [@thenamefirst](https://forum.arduino.cc/u/thenamefirst)\
**Post date:** [January 10, 2015, 7:44am UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/14 "2015-01-10T07:44:30Z")

</div>

the original code is #4 i change it to read data from SD card.  
my SD card data is 12345678  
but when i use #10 code the result is  
1  
11  
111  
1111  
11111  
111111

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 10, 2015, 3:04pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/15 "2015-01-10T15:04:39Z")

</div>

```cpp
myFile = SD.open("LL.txt");
  if (myFile){
    delay(3);  //delay to allow buffer to fill
    if (myFile.available() >0) {

```

That delay is just plain stupid. The SD.open() function doesn't return until the associated buffer has been filled.

Then, you read one character from the file, append it to the String instance, print the string, end loop(), and open the file again.

The if in the snippet above needs to be a while, to read the whole file.

---

<div class="post-metadata">

**Author:** ![foadsf](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@foadsf](https://forum.arduino.cc/u/foadsf)\
**Post date:** [September 15, 2019, 8:13pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/16 "2019-09-15T20:13:39Z")

</div>

I would suggest using `Serial.readStringUntil("\n")` which waits until the carriage return character is read or the end of the timeout.

---

<div class="post-metadata">

**Author:** ![rtek1000](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rtek1000](https://forum.arduino.cc/u/rtek1000)\
**Post date:** [September 25, 2019, 5:18am UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/17 "2019-09-25T05:18:50Z")

</div>

> foadsf:  
> I would suggest using
> 
> ```arduino
> Serial.readStringUntil("\n")
> 
> ```
> 
> which waits until the carriage return character is read or the end of the timeout.

Error:

> invalid conversion from 'const char\*' to 'char' [-fpermissive]

Need to use single instead of double quotes for only one character, this works:

```arduino
String inputString = Serial.readStringUntil('\n');

```

See too:

> <https://stackoverflow.com/questions/11197097/how-to-read-a-string-value-with-a-delimiter-on-arduino>

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [September 25, 2019, 7:58am UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/18 "2019-09-25T07:58:59Z")

</div>

> foadsf:  
> I would suggest using `Serial.readStringUntil("\n")` which waits until the carriage return character is read or the end of the timeout.

I would advise not to promote the use of String (capital S) objects; there is the risk of memory fragmentation which might result in unpredictable run time behaviour. readBytesUntil is a far better option and Robin's Serial Input Basics tutorial is far preferred.

Note  
This thread is 7 years old

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 7:46pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/19 "2021-05-05T19:46:42Z")

</div>

Closed on May 5, 2021, 7:46pm UTC



---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [December 31, 2024, 8:17pm UTC](https://forum.arduino.cc/t/read-line-from-serial/98251/20 "2024-12-31T20:17:47Z")

</div>

Opened on December 31, 2024, 8:17pm UTC



[Next page](https://forum.arduino.cc/t/read-line-from-serial/98251.md?page=2)
