Pages: [1]   Go Down
Author Topic: Reading a string from the serial port  (Read 905 times)
0 Members and 1 Guest are viewing this topic.
Washington
Offline Offline
God Member
*****
Karma: 38
Posts: 790
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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!

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27089
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 127
Posts: 8518
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Try something like this

Code:
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

« Last Edit: January 30, 2011, 12:34:54 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Washington
Offline Offline
God Member
*****
Karma: 38
Posts: 790
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Try something like this

Code:
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:
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:
Serial initiated





e

l


o

l

!

!

!

!

!
Logged

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!

0
Offline Offline
Tesla Member
***
Karma: 144
Posts: 9609
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some simple code you can tinker with.

Code:
// 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="";
      }
   }
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 127
Posts: 8518
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
« Last Edit: January 30, 2011, 12:43:55 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Washington
Offline Offline
God Member
*****
Karma: 38
Posts: 790
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@zoomcat:
That works great!
Thank you.
Quote
I had a type in my example, try
Not quite sure what this means.
Logged

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!

Pages: [1]   Go Up
Jump to: