Reading a string from the serial port

I have absolutely no clue to this and the library is missing the provision.
How do I read a string from the serial port?
I know about Serial.read() but it only reads one character at a time and I do not know how to pack this into a string.

I saw some code earlier where someone did just that it kept testing & adding characters until and EOL (end of line) character was recieved. Have looked at so many posts trying to read all the new stuff coming in I don't think I could find it. Try searching for EOL, see what comes back.

There are a gazillion examples around (albeit mostly on the old forum I admit).

Try something like this

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

void loop() {
  char myString[100];
  char *ms_ptr = myString;

  // oops, this was wrong if (Serial.read() > 0) {
  if (Serial.available() > 0) {
     *ms_ptr = Serial.read();
     ms_ptr++;
     *ms_ptr = '\0';
  }
}

This will never stop reading chars though. Normally you also have a test for a end of string character at which point you break out of the loop and do some work with the string.

EDIT: oops, Serial.read -> Serial.available


Rob

Graynomad:
There are a gazillion examples around (albeit mostly on the old forum I admit).

Try something like this

void setup() {                

Serial.begin (9600);
}

void loop() {
 char myString[100];
 char *ms_ptr = myString;

if (Serial.read() > 0) {
    *ms_ptr = Serial.read();
    ms_ptr++;
    *ms_ptr = '\0';
 }
}




This will never stop reading chars though. Normally you also have a test for a end of string character at which point you break out of the loop and do some work with the string.

______
Rob

I tried this

char myString[100];
void readstring(){
  char *ms_ptr = myString;
  if (Serial.read() > 0) {
     *ms_ptr = Serial.read();
     ms_ptr++;
     *ms_ptr = '\0';
  }                  // null terminate the string
}
void setup(){
  Serial.begin(300);
  Serial.println("Serial initiated");
}
void loop(){
  if(Serial.available()){readstring();}
  Serial.println(myString);
}

And typed "Hello World!" and hit enter and got:

Serial initiated





e

l


o

l

!

!

!

!

!

Some simple code you can tinker with.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
	Serial.begin(9600);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
    	if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;}
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      
      readString="";
      } 
   }

I had a typo in my example, but also you are not accumulating a string, just getting a character at a time


Rob

@zoomcat:
That works great!
Thank you.

I had a type in my example, try

Not quite sure what this means.