Arduino get reply after each AT commnd send to phone

Hello,
I’m connecting a Sony Ericsson k700i phone to Arduino Uno. I’m able to send AT commands but cannot get all the bytes when requesting a SMS read. I wrote a small function to execute AT commands and get reply but it does not show the reply from phone.
What am i doing wrong?

With the function below I want to read the value of first SMS and if the pdu format contains a specific string I want to turn on a led.

The function code:

/*
EX: executare_comanda_at_v2(“AT”);

executare_comanda_at_v2(“AT”);
executare_comanda_at_v2(“ATE=0”);//Echo off
executare_comanda_at_v2(“AT+CPMS=“ME”,“SM””);
executare_comanda_at_v2(“AT+CMGR=1”);

*/
void executare_comanda_at_v2(String comanda_at) {

String readString="";//format readString…
char c=NULL;

Serial.begin(9600);
Serial.println("–START COMANDA–");
//delay(3000);

readString="";//format readString…

Serial.println(comanda_at);

// read phone reply if available
while(Serial.available() > 0)
{
Serial.print( c=Serial.read());
readString+=c;
}
//delay(1000);
Serial.println(readString);//serial monitor display sms pdu format
//delay(3000);
if (readString.indexOf(“026F37”)>0)// if admin number=true and sms pdu on=true
{
digitalWrite(indicatorPin, HIGH);//only for test led on
//pulsesIR_Toyotomi.SendACCodeOn();//AC Toyotomi on
delay(5000);
//sendsms_temp();//send sms to admin temperature room
digitalWrite(indicatorPin, LOW);//only for test
}

Serial.println("–STOP COMANDA–\r\n");
Serial.flush();
delay(3000);//delay start loop again for check new sms…

}

Serial.println(comanda_at);


     
    // read phone reply if available
   while(Serial.available() > 0)
           {
              Serial.print( c=Serial.read());
               readString+=c;
           }

Send a command. Expect a complete reply, and nothing more or less than a complete reply, nanoseconds later. I wonder why that doesn't work.

I think that you don't understand the asynchronous nature of serial communications.

So I have to add delay(3000)?
How long should I wait for the reply?
I’m sorry if it’s a dumb question but I’m new to arduino.

So I have to add delay(3000)?

No. Think about how this forum works. You post a question. At some indefinite time later, someone reads it, and posts a reply.

That is exactly what happens when you send an AT command, and the modem generates a response.

You have two choices. You can send a command, and then spin in a while loop, waiting for a response, accomplishing nothing in the meanwhile. That would be like posting here, and then staring at your computer until you get a response. Not very productive, and you'd get tired, hungry, thirsty, and miserable waiting.

Or, you can send a command, and keep track, in some kind of queue, the commands that you sent. Then, as data comes in, you'd read and store the data, ignoring it until the end of the response arrived. When the end of the response arrives, you parse the response, and decide what it meant, based on the first request in the queue. That would be like checking periodically for replies here, and reading the previous response (when the poster doesn't quote anything) to see what the reply is in response to.

There are four problems with this approach. First, you need to understand what constitutes the end of a response. That may not be easy to determine.

Second, you need some way to keep track of the commands sent. With the limited memory of the Arduino, that could be a problem.

Third, there may be cases where you need to decide what to do next based on the success or failure of a given command. That is probably the least important issue.

Finally, it is possible for the device itself to send data, unrequested, to the Arduino, such as when a call or a text message arrives. These messages include some information, though, to indicate that they are not responses to commands (RING, for example, for an incoming call that needs to be answered (or ignored)).