Go Down

Topic: Reading a string from the serial port (Read 988 times) previous topic - next topic

smeezekitty

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.
Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Graynomad

#2
Jan 30, 2011, 05:41 am Last Edit: Jan 30, 2011, 06:34 am by Graynomad Reason: 1
There are a gazillion examples around (albeit mostly on the old forum I admit).

Try something like this

Code: [Select]
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

Rob Gray aka the GRAYnomad www.robgray.com

smeezekitty


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

Try something like this

Code: [Select]
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
Code: [Select]
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:
Code: [Select]

Serial initiated





e

l


o

l

!

!

!

!

!
Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

zoomkat

Some simple code you can tinker with.

Code: [Select]
// 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="";
      }
   }
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Graynomad

#5
Jan 30, 2011, 06:37 am Last Edit: Jan 30, 2011, 06:43 am by Graynomad Reason: 1
I had a typo in my example, but also you are not accumulating a string, just getting a character at a time

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

smeezekitty

@zoomcat:
That works great!
Thank you.
Quote

I had a type in my example, try

Not quite sure what this means.
Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

Go Up