Go Down

Topic: Clearing a String (Read 9169 times) previous topic - next topic

iRagDoll

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!

James C4S

Wouldn't:

Code: [Select]
String stringOne = "";

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

iRagDoll

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

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

kevinsa5

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

iRagDoll

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!

Graynomad

#5
Jan 27, 2011, 01:57 pm Last Edit: Jan 27, 2011, 05:40 pm by Graynomad Reason: 1
Code: [Select]
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

Rob Gray aka the GRAYnomad www.robgray.com

marklar

#6
Jan 27, 2011, 02:23 pm Last Edit: Jan 27, 2011, 02:28 pm by marklar Reason: 1
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: [Select]

//-- 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);
}



Graynomad

Here's another loop to accumulate the string

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

Rob Gray aka the GRAYnomad www.robgray.com

zoomkat

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: [Select]
while (Serial.available() > 0) {
      delay(10);  //small delay to allow input buffer to fill     
      char c = Serial.read();
      readString += c;
  }
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

marklar

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.


zoomkat

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.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

iRagDoll

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!

Go Up