Problem using SoftwareSerial with GPRS module...

Actually I don't really know if thats my problem...

When I communicate with the gprs module and read the response in the Serial Monitor it works fine at first but then it just stop doing what it is supposed to.

The code has two parts:

1- I send the AT command through SoftwareSerial ( mySerial.println(...) )

2- Then I read the serial response from the gprs shield, convert it into a matrix of chars (to work later with that response)

This part convert the serial response into a matrix of chars....

int Serial_Received(char palabras[][50]){
 
  Data = "";
  int i=0; 
  int largo=0;

  
   while (mySerial.available()){
    
        char character = mySerial.read(); // Receive a single character from the software serial port

        if(character != '\n'){
        Data.concat(character); // Add the received character to the receive trash
        }
        
        if(character == '\n' && Data.length() >= 4){

          Data.toCharArray(palabras[i],50); //Copia lo que había en DATA (hasta el momento) en un array...
          i++; // Aumentamos el contador

          Data = ""; // Limpiamos el array.
       }

    }

    i--; // Esto lo realizamos para eliminar la última fila del string,el cual devuelve solo un '/0'
    return i;

}

And this part of the code sends the AT command, then i read the responde with the previous function (Serial_Received())

All the for and sprintf I added was to track the error...

void GET_senddata(int temperatura,int humedad){

char palabras[20][50] = {};
int indice=9999;
int i=0;
int cantidad = 0;
char trash[50];
 
  mySerial.println("AT+CGATT?");
  delay(1000);
 

 indice = Serial_Received(palabras);


 for(i=0;i<=indice;i++){

  sprintf(trash,"1La palabra en la posicion %d es: %s",i,palabras[i]);

  Serial.println(trash);
 }
 
 
 
  mySerial.println(" AT+SAPBR=3,1, \"CONTYPE\", \"GPRS\" ");//setting the SAPBR, the connection type is using gprs
  delay(1000);
 
 indice = Serial_Received(palabras);

 for(i=0;i<=indice;i++){

  sprintf(trash,"2La palabra en la posicion %d es: %s",i,palabras[i]);

  Serial.println(trash);
 }





  mySerial.println("AT+SAPBR=3,1,\"APN\",\"gprs.personal.com\"");
 
  delay(1000);

 indice = Serial_Received(palabras);


 for(i=0;i<=indice;i++){

  sprintf(trash,"3La palabra en la posicion %d es: %s",i,palabras[i]);

  Serial.println(trash);
 }






  mySerial.println("AT+SAPBR=3,1,\"USER\",\"gprs\"");
  delay(1000);

 indice = Serial_Received(palabras);


 for(i=0;i<=indice;i++){

  sprintf(trash,"4La palabra en la posicion %d es: %s",i,palabras[i]);

  Serial.println(trash);
 }

  mySerial.println("AT+SAPBR=3,1,\"PWD\",\"gprs\"");
  delay(1000); 
 
 indice = Serial_Received(palabras);


 for(i=0;i<=indice;i++){

  sprintf(trash,"5La palabra en la posicion %d es: %s",i,palabras[i]);

  Serial.println(trash);
 }

What I see in the Serial Monitor is:

1La palabra en la posicion 0 es: AT+CGATT?
1La palabra en la posicion 1 es:
+CGATT: 1
1La palabra en la posicion 2 es:
OK
2La palabra en la posicion 0 es: AT+SAPBR=3,1, "CONTYPE", "GPRS"
2La palabra en la posicion 1 es:
OK
3La palabra en la posicion 0 es: AT+SAPBR=3,1,"APN","gprs.personal.com"
3La palabra en la posicion 1 es:
OK
4La palabra en la posicion 0 es: AT+SAPBR=3,1,"USER","gprs"
4La palabra en la posicion 1 es:
OK
5La palabra en la posicion 0 es: AT

As you can see, in the 5th FOR, the program stops and the loop starts again (from the first for) until the 5th FOR...

I don't understand why is this happening...

Thanks for advance!

Regulus8:
This part convert the serial response into a matrix of chars....

   while (mySerial.available()){

This is not a reliable way to receive data because the Arduino is so much faster than the speed at which data arrives. The WHILE can complete long before all the data arrives.

Have a look at the second example in Serial Input Basics

...R