[SOLVED] Unable to use String functions/methods

I am currently trying to write a program that receives a message from the serial communications window, puts it together, interprets it, and responds accordingly. So far, however, I haven't gotten past the second step. The reason being that none of the functions for the String class appear to be working. Here is my (rather brief) code:

void setup(){
  Serial.begin(9600);  //Begin serial communications
}

void loop(){
  int mesLength=Serial.available();  //Determines the length of the received message
  if (mesLength>0){                  //Continues if a message was received
    String str1=String(char(Serial.read())); //Converts first value to a char, then to a String
    for (int i=0; i<=mesLength; i++){        //The for loop is to combine and convert all read values into one complete String
      String str2=String(char(Serial.read()));  //Converts next value to a char, then to a String
      str1.concat(str1, str2);  //Adds str2 to str1, both together becoming str1                             
    }
    Serial.println(str1);  //Prints str1 (the complete message)
  }
}

Unfortunately, this code brings up an error:

no matching function for call to 'String::concat(String&, String&)'

To make up for this, I changed one small piece my code:

str1+=str2;

The good news is: no error messages! The bad news: it doesn't print what I want, which is a complete string. I typed in 'Hello' (without quotation marks) using the modified code and this is what I got:

Hÿÿ
eÿÿ
lÿÿ
lÿÿ
oÿÿ

Although its a shame that this last attempt didn't work, the bigger problem is that none of the String functions are working. I typed in all of them (including concat()) and not one changed color. The only one that the IDE seems to recognize is 'String' itself.
Why is that? Can I fix the problem? Am I doing something wrong? If it helps, I am using an Arduino Uno R3 and my IDE version is 1.0.5. Thanks!

  int mesLength=Serial.available();  //Determines the length of the received message

Since serial data arrives slowly, (really slowly), most of the time, mesLength will be 0. On rare occasions, it will be one.

  if (mesLength>0){                  //Continues if a message was received
    String str1=String(char(Serial.read())); //Converts first value to a char, then to a String

If there is at least one character to read, read that character, cast it to a character, wrap the character in a String, and then make a copy of the String. Is all that crap really necessary? No, it isn't.

    for (int i=0; i<=mesLength; i++){        //The for loop is to combine and convert all read values into one complete String
      String str2=String(char(Serial.read()));  //Converts next value to a char, then to a String
      str1.concat(str1, str2);  //Adds str2 to str1, both together becoming str1                             
    }

You've already read one character (of the one available), so now you try to read the character again. Wrong! i should start at 1!

Then, you repeat the same nonsense to make a String out of a String made from a character cast to a character.

The concat method takes one argument - the string, String, or character to concatenate.

Unfortunately, this code brings up an error:

Well, now you know why.

You need to forget about Strings. Read and data available, and store it in an array, adding a NULL after each character, until some end of packet marker arrives. THEN, print the string you received.

Ugh! I'm really sorry for being so late in reply, but for some reason I have been unable to access the Arduino forums upon logging in. I just discovered that the page works on the iPad (weird), but my reply apparently didn't make it. Anyway:

Code:

int mesLength=Serial.available();  //Determines the length of the received message



Since serial data arrives slowly, (really slowly), most of the time, mesLength will be 0. On rare occasions, it will be one.

Well, that was a "duh" moment. Moving on.

What you say sounds good, but if I am to be honest, I'm rather new at this. Unfortunately, because of that, I have been unable to implement your suggestions. Please try to help me understand. While I know that the null character must be present at the end of a character array, why must it be after each character? What is an end of packet marker and how would I read it? How do I use any of this to determine the size of my array? That last question I think was really the big one. After that its pretty easy. Please remember that I am new to Arduino and that my questions are so that I may act upon what you have said.

While I know that the null character must be present at the end of a character array, why must it be after each character?

How will you be absolutely certain that there is a NULL at the end of the string unless you put one after each character that you add to the string? I do not mean that there should be a NULL between each character. Suppose that you read "12345" from the serial port, and store it into a string called stuff:

char stuff[20]
byte index = 0;

After reading the '1', you should have stuff[0] = '1' and stuff[1] = '\0'. After reading the '2', you should have stuff[0] = '1' and and stuff[1] = '2' and stuff[2] = '\0'. Each time you read a character, and add it to the array, the character overwrites the NULL at the end of the array, so you need to add another one.

What is an end of packet marker and how would I read it?

What do you want it to be? If the serial monitor is being used to send data, you can configure it to send nothing after your data, or a carriage return and line feed, or just one of them. Perfect end of packet markers.

You read them just like any other character, but you don't store it in the array. Rather, you set a flag that says that the array is now ready for processing.

How do I use any of this to determine the size of my array?

The strlen() function looks for the NULL. Or, did you mean how do you determine how big the array should be, in the first place?

Very simple code for capturing serial characters into a String, then evaluating the captured String for specific commands to do something.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

While I know that the null character must be present at the end of a character array, why must it be after each character?

How will you be absolutely certain that there is a NULL at the end of the string unless you put one after each character that you add to the string? I do not mean that there should be a NULL between each character. Suppose that you read "12345" from the serial port, and store it into a string called stuff:

char stuff[20]

byte index = 0;



After reading the '1', you should have stuff[0] = '1' and stuff[1] = '\0'. After reading the '2', you should have stuff[0] = '1' and and stuff[1] = '2' and stuff[2] = '\0'. Each time you read a character, and add it to the array, the character overwrites the NULL at the end of the array, so you need to add another one.

Alright, that makes more sense.

What is an end of packet marker and how would I read it?

What do you want it to be? If the serial monitor is being used to send data, you can configure it to send nothing after your data, or a carriage return and line feed, or just one of them. Perfect end of packet markers.

You read them just like any other character, but you don't store it in the array. Rather, you set a flag that says that the array is now ready for processing.

I can certainly do that.

How do I use any of this to determine the size of my array?

The strlen() function looks for the NULL. Or, did you mean how do you determine how big the array should be, in the first place?

I suppose I should have been more specific here. I meant "how do I know how long to make my array?", not "how do I find it's size?"
I have a feeling that I will just need to set an arbitrary length and make my program intelligent enough to know when to stop reading from the array. But if I'm wrong please let me know. Otherwise I think this problem can be considered solved. Thanks for the help! Here is my finished code:

char message[32];
int index=0;

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

void loop(){
  int availMes=Serial.available();  //Checks for available character
  if (availMes>0){              
    char thisChar=Serial.read();    //Reads character
    if (thisChar!='.'){          //Makes sure that the string continues
      if (index<32){            //Makes sure that there is enough room in the array
      message[index]=thisChar;    //Assigns thisChar to message[index]
      message[index+1]=NULL;     //Assigns NULL to message[index+1]
      index++;              //Increments index
      }
    }
    if (thisChar=='.'){        //Checks for end of sent message
      for (int i=0; i<index; i++){    //Prints the sent message
        Serial.print(message[i]);
      }
    }
  }
}

For now, I'll just use a period for an end of packet marker. I wouldn't call it the most robust program, but it is a proof of concept and uses less then 2000 bytes.

    if (thisChar!='.'){          //Makes sure that the string continues
    }
    if (thisChar=='.'){        //Checks for end of sent message

If the character is not a period, is there anything it can be but a period? The second if statement should be an else.

And the curly braces belong on a new line (in my opinion) or separatedfromtherestofthestatement.

Simple serial code that uses a data delimiter.

//zoomkat 3-5-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}