Best way to handle overflow?

I’ve just realized that there is only a 100 char limit after pulling my hair out over garbage text that I was receiving from my GPRS shield.

I’ve heard that Strings are bad news with the arduino… so what is the best way to handle received text that is over 100 chars? As seen below, i’ve tried changing to a new array once it hits 100, but Char variables don’t work like String variables… so I’m kinda at a loss.

Thanks in advance for any guidance.

int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout){

    //Serial.print("Sending: ");
    //Serial.println(ATcommand);
  
    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Initialize the string

    delay(100);

    while( mySerial.available() > 0) mySerial.read();    // Clean the input buffer

    mySerial.println(ATcommand);    // Send the AT command 

    int chrnum = 0;
    int buffernum = 0;

    previous = millis();

// then later...
    // this loop waits for the answer
    do{
        // if there are data in the UART input buffer, reads it and checks for the asnwer
        if(mySerial.available() != 0){

            if(chrnum<99) {
                 
             } else {
                 Serial.println("OVERFLOW PREVETION");
                 buffernum++;
                 chrnum=0;
             }

             response[chrnum] += mySerial.read();
             Serial.print("buff");
             Serial.print(buffernum);
             Serial.print(" - chr");
             Serial.print(chrnum);
             Serial.print(" - ");
             Serial.println(response[chrnum]);
             
             chrnum++;
            
            
            // check if the desired answer 1  is in the response of the module
            if (strstr(response, expected_answer1) != NULL)    
            {
                answer = 1;
            }
            // check if the desired answer 2 is in the response of the module
            else if (strstr(response, expected_answer2) != NULL)    
            {
                answer = 2;
            }
        }
    }
    // Waits for the asnwer with time out
    while((answer == 0) && ((millis() - previous) < timeout));  

    Serial.println("Received-->: ");
    Serial.println(response);
    Serial.println("<--Received: ");
    return answer;
}

Best way to handle overflow is to declare an array that is large enough.

Thanks Arrch,
I really appreciate your fast reply.

I've thought it would be best to use arrays, but the syntax is different than what I am use to using Strings vs Chars. Can you give me an example on how you would accomplish this?

Many, many thanks

Your response array is defined as having 100 elements. Increase the number of elements.

UKHeliBob:
Your response array is defined as having 100 elements. Increase the number of elements.

Or better yet, don't try and parse the entire message at once. Store characters until you get a new line. Once received parse what you have and reset the array.

Yes, but if i got above 100 i get garbage text as the buffer has overflowed. Apparently there is a 100 char limit on the Arduino. -- I know, news to me too.

Arrch:
Best way to handle overflow is to declare an array that is large enough.

You can't. None of the current Arduinos have enough SRAM to allocate the memory needed to hold Google's home page.

Like Arrch suggests, I read a line at a time. Store the serial input characters in the array, watching for a newline character '\n', then check that line. If it isn't the line you wanted, then reset the array pointer to zero, clear the array, and start reading another line. Keep the overflow protection too. Google's home page has some lines that are quite long.

There is not a 100 character limit that I know of. Unos are limited to 2K SRAM, and Megas have 8K.

OH!! :smiley:

That makes a lot more sense. Thanks.

Just so i am sure that I understand the syntax here...

char response[100];

is an array of 100 one char variables, right?

Serial.print(response);

this shows everything stored in all 100 spots, right?

Apparently there is a 100 char limit on the Arduino.

Where did you read this ?

bradburns:
Just so i am sure that I understand the syntax here...

char response[100];

is an array of 100 one char variables, right?

Serial.print(response);

this shows everything stored in all 100 spots, right?

Yes and no. The response variable is 100 characters. Remember to leave a character for the terminating zero, so 99 characters plus that zero terminator is all it will hold. Serial.print(response) will print to the zero terminator in the array.

Remember though that the terminating character does not have to be in position 99 of the array. Can you predict what the output of this code will be before you run it ?

char globalArray[100];

void setup() 
{
  Serial.begin(115200);
  globalArray[0] = 'A';
  globalArray[25] = 'Z';
  Serial.print("Global array <");
  Serial.print(globalArray);
  Serial.println(">");

  char localArray[100];
  localArray[0] = 'A';
  localArray[25] = 'Z';
  Serial.print("Local array <");
  Serial.print(localArray);
  Serial.println(">");
}

void loop() 
{
}

Remember to leave a character for the terminating zero

Technically, character arrays that you wish to use as a string are not terminated with a zero, which is an int data type. While the code below compiles without error:

  char str[10];
  str[0] = 0;

The code is more properly written as:

  char str[10];
  str[0] = NULL;

because NULL is the string termination character that is usually written as '\0' and is not the same as integer zero. Indeed, there is nothing in the X3J11 spec that says the termination character has to be binary zero. By using the symbolic constant NULL, even changing to a different compiler that used a different termination character would work. "Magic numbers" like 0 tend to make your code less portable while symbolic constants often enhance portability.

econjack:
The code is more properly written as:

  char str[10];

str[0] = NULL;




because NULL is the string termination character that is usually written as '\0' and is not the same as integer zero.

Sorry, that is plain wrong. From the ISO 2003 C++ standard:

The macro NULL is an implementation-defined C++ null pointer constant in this International Standard
(4.10).180)


  1. Possible definitions include 0 and 0L, but not (void*)0.

So NULL is a portable way to define a null pointer constant, not a string terminator.