0
Offline
Full Member
Karma: 1
Posts: 146
Arduino rocks
|
 |
« on: January 26, 2011, 10:34:26 pm » |
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
Faraday Member
Karma: 41
Posts: 5166
CMiYC
|
 |
« Reply #1 on: January 26, 2011, 10:44:06 pm » |
Wouldn't: String stringOne = ""; Do that?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 146
Arduino rocks
|
 |
« Reply #2 on: January 26, 2011, 11:38:13 pm » |
No I tried doing that. Here is the code that I am using. 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
Full Member
Karma: 1
Posts: 137
:O Arduino!
|
 |
« Reply #3 on: January 27, 2011, 12:34:48 am » |
you have '=='. that's testing whether it's equal, try just a single '='
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 146
Arduino rocks
|
 |
« Reply #4 on: January 27, 2011, 07:28:51 am » |
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
Tesla Member
Karma: 71
Posts: 6817
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #5 on: January 27, 2011, 07:57:19 am » |
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
|
|
|
|
|
Florida
Offline
Sr. Member
Karma: 3
Posts: 431
hookedup!
|
 |
« Reply #6 on: January 27, 2011, 08:23:29 am » |
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. //-- 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
Tesla Member
Karma: 71
Posts: 6817
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #7 on: January 27, 2011, 11:53:18 am » |
Here's another loop to accumulate the string 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
|
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6540
Arduino rocks
|
 |
« Reply #8 on: January 27, 2011, 01:14:10 pm » |
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. while (Serial.available() > 0) { delay(10); //small delay to allow input buffer to fill char c = Serial.read(); readString += c; }
|
|
|
|
|
Logged
|
|
|
|
|
Florida
Offline
Sr. Member
Karma: 3
Posts: 431
hookedup!
|
 |
« Reply #9 on: January 27, 2011, 02:00:54 pm » |
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
Tesla Member
Karma: 50
Posts: 6540
Arduino rocks
|
 |
« Reply #10 on: January 27, 2011, 05:44:22 pm » |
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
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 146
Arduino rocks
|
 |
« Reply #11 on: January 28, 2011, 07:42:54 am » |
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
|
|
|
|
|
|