Pages: [1]   Go Down
Author Topic: Clearing a String  (Read 6395 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Guys,

This is probably an easy question for most of you guys. What is the best way to clear a String value created by the String object?
All I am doing is filling up a String with values but then I need an easy way to clear it.

Thanks!
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wouldn't:

Code:
String stringOne = "";

Do that?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Full Member
***
Karma: 1
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No I tried doing that. Here is the code that I am using.

Code:
while (Serial.available() > 0) {
      char c = Serial.read();
      readString += c;
  }
 
  if (readString == "<T1>")
  {
    Serial.println("Hello");
  }
  else
  {
    readString == "";
  }

All I need to do is in the else statement, clear the String readString so that I can read the next input properly.
Logged

North Carolina, USA
Offline Offline
Full Member
***
Karma: 1
Posts: 137
:O Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you have '=='. that's testing whether it's equal, try just a single '='
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay I tried that but now if I pass the String "<T1>" which should initialize the true case will not run. It wont print out anything.
Any ideas?

Really all I need to do is read a string from the Serial Port, and test if the String equals various String constants. Also, if the String from the Serial port does not equal any of the constant Strings, it should be completely cleared.

Thanks!
Logged

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

Code:
while (Serial.available() > 0) {
      char c = Serial.read();
      readString += c;
  }

This will exit when the first character is received (the "<"), you test it for "<T1>" and of course that fails so you clear the string. Then you get a "T", etc etc.

You should read all four characters then do the test.

Also, once 4 chars are read you should terminate the string with a '\0' or it's not a string. EDIT: wrong, the C++ += operator will do this.

______
Rob

« Last Edit: January 27, 2011, 11:40:59 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Florida
Offline Offline
Sr. Member
****
Karma: 4
Posts: 431
hookedup!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This code may be close to what you are looking for.  As the previous poster mentioned, you don't want to assume you know what is coming across the serial port in one read - ever.  Instead it is better to use a process that holds and compares the values as shown below.

This example will show the T1 Trigger message if <T1> (case sensitive) is sent, else it shows the value received.  Doing this also shows the value when it was not something expected, a must for the debug process.

Hope this helps.

Code:
//-- may need 9600 or your standard baud rate
#define SERIAL_BAUD_DEFAULT 19200
//-- global var to hold string as it is read
String readString = "";

//-- process the pending string once a delim hit
void processString(){
  //--- Check String
    if( readString == "<T1>" ){
      Serial.println("T1 Trigger");
    } else {
      Serial.println(readString);
    }
  //--- Clear String for more reading
    readString = "";
}

void loop(){
  while (Serial.available() > 0) {
      char c = Serial.read();
      readString += c;
      //-- If we see the end delim character ..
      //    then process the string, which clears it
      if( c == 62 ){ //> char
        processString();
      }
  }
}

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

« Last Edit: January 27, 2011, 08:28:08 am by marklar » Logged

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

Here's another loop to accumulate the string

Code:
if (Serial.available() > 0) {
  c = Serial.read();
  if (c == '<') { // start of packet
    readString = c;
    while (c != '>') { // until end of packet
        if (Serial.available() > 0) {
            c = Serial.read();
            readString += c;
        }
   }
}

This should synchronize on the '<' char as well so if things go wrong they won't stay wrong.

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is important to note that the arduino can loop faster than the serial buffer may fill. Also for trouble shooting, print readstring to the serial monitor so you can see what is actually being captured. Try the small delay like below to see if you capture the whole string.

Code:
while (Serial.available() > 0) {
      delay(10);  //small delay to allow input buffer to fill     
      char c = Serial.read();
      readString += c;
  }
Logged

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

Florida
Offline Offline
Sr. Member
****
Karma: 4
Posts: 431
hookedup!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It is important to note that the arduino can loop faster than the serial buffer may fill.
The original code posted accounts for this fact by not taking action until the delimiter is found.  I use serial quite a bit and never put in a delay and would not personally suggest it as I see no benefit and it would slow down the overall process.   I usually read serial in a background timer process while the primary app/loop runs.  A delay in that case would be bad.   

Not that I am arguing with you, just providing my input / perspective to the new user.

Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The original code posted accounts for this fact by not taking action until the delimiter is found.

I see no provision in the origional code posted by the OP for identifying a delimiter, although the string intended to be captured does seem to provide the < and > to delimit the string. If the string being sent has delimiters, then they should be used. If the string being sent has no delimiters and is just sent as a string of characters, then the only way to capture the string intact may be the use of the slight delay. My delay does upset some in the forum, but does seem to evade issues with the capture loop finding the buffer empty between characters being placed in it.
Logged

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all of your help! I think  I got everything to work. If I have any more problems I'll just post.

Thanks again!
Logged

Pages: [1]   Go Up
Jump to: