String Object Invalid Length display

Hi Everyone,
In the following code, I have created an array of max index 200 and also a String object of similar maximum length. The array gets populated from GSM module data. This populated array is then copied to the String object. Although both holds same data, but they display different length. Serial1 is interfaced with a GSM module and Serial2 in connected with my PC Serial port. I am using Arduino Mega 2560.
Kindly provide answer asap.

Best regards,

Nick.

#define maxLength 200
String STR_BUFFER = String(maxLength);       // allocate a new String


char RX_BUFFER[200];
unsigned int RX_BYTE_COUNT=0;
unsigned int TX_BYTE_COUNT=0;
unsigned int TOTAL_RX_BYTES = 0;
unsigned char RX_SER_BYTE=0;
unsigned char RX_STATE=0;

boolean RX_COMPLETE = false;
boolean START_BUFFERING = false;

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
unsigned int ONBOARD_LED = 13;
unsigned int GSM_START = 7;

void setup()
{
    // initialize the digital pin as an output.
    pinMode(ONBOARD_LED, OUTPUT);
    pinMode(GSM_START, OUTPUT);
    
    digitalWrite(GSM_START, HIGH);
    delay(1000);
    
    Serial1.begin(9600);
    Serial2.begin(9600);    
    
    Serial2.println(" ");     
    Serial2.println("Starting Communication...");
    RX_STATE=0;
    RX_COMPLETE = false;
    
    digitalWrite(GSM_START, LOW);
    delay(1000);
    digitalWrite(GSM_START, HIGH);
}

void loop()
{
    // read from port 1, send to port 2:
    if (Serial1.available()) 
    {
       RX_SER_BYTE = Serial1.read();                    
       RX_BUFFER[RX_BYTE_COUNT]=RX_SER_BYTE;
       RX_BYTE_COUNT++;
       //RX_COMPLETE=false;    
       
       if(RX_BUFFER[RX_BYTE_COUNT-1]==0x0A)
       {
           if(RX_BUFFER[RX_BYTE_COUNT-2]==0x0D)
           {               
               RX_COMPLETE=true;
               TOTAL_RX_BYTES=RX_BYTE_COUNT;
               RX_BYTE_COUNT=0;                        
           }
       }               
    }
       
    if(RX_COMPLETE==true && TOTAL_RX_BYTES>2)
    {        
        for(TX_BYTE_COUNT=0;TX_BYTE_COUNT<TOTAL_RX_BYTES-1;TX_BYTE_COUNT++)
        {  
            Serial2.print(RX_BUFFER[TX_BYTE_COUNT]);              
        }
               
        STR_BUFFER = RX_BUFFER; 
        Serial2.println(TOTAL_RX_BYTES);
        Serial2.println(STR_BUFFER.length()); 
        TOTAL_RX_BYTES=0;
        RX_COMPLETE=false;
    }    
}

output

RDY
5
5
+CFUN: 1
10
10
+CPIN: READY
14
14
Call Ready
12
14

There is a convention that all capital letter names are reserved for constants. Constants NEVER appear on the left of an equal sign.

In addition to printing values, you should print identifiers for those values.

You should also print, delimited in some fashion, the two character collections - the string and the String. Perhaps that will provide a clue.

#define maxLength 200
String STR_BUFFER = String(maxLength);       // allocate a new String

All that does is create a string containing "200".

Proof:

#define maxLength 200
String STR_BUFFER = String(maxLength);       // allocate a new String

void setup ()
  {
  Serial.begin (115200);
  Serial.println (STR_BUFFER);
  Serial.println (STR_BUFFER.length ());
  }  // end of setup

void loop () { }

Output:

200
3

Note that the length is 3.

And what PaulS said. Don't capitalize variable names.

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

Hi Mr. Gammon,
This code is compiled using Arduino IDE 1.0.4.

Regards,

Nick.