Serial comunication from modem gsm to arduino

hello all
I’m trying to make a simple sketch to read a sms from modem gsm via serial port.The problem is how can I do this on the easy way.
The modem gives me a answer with a big output of data and I try to put all in a array (I dont know if this is the better way)
I just need the last 3 digits of the answer but how can I remove it from the serial buffer without destroying the last 3.
here is my sample code
I already try it with lots of changes but I still can’t get it right

char sms[100];
int i = 0 ;

void setup()  
  Serial.println("Read SMS!");

  Serial1.println("AT+CMGF=1\r\n");  // SetSerial1 mode to text (vs pdu)
  // Serial1.flush();
  //  delay(300);
  Serial1.print("AT+CMGR=1");  //Read sms at 1 location


void loop()                     // run over and over again
  while (Serial1.available() > 0 && (i<=120)) {
    sms[i] =;
    Serial.print(sms[i], BYTE);
//    Serial.println(i);

This is the output of the code in arduino serial

Read SMS!


+CMGR: "REC READ","+35196XXXXXXX",,"12/02/08,10:23:30+00"


Like I say I only need the “123” information all the rest is garbage!!!

char sms[100];

while (Serial1.available() > 0 && (i<=120)) {
sms =;
So, stuffing 21 extra characters in the array won’t trash anything else? I don’t think so.
* *    delay(30);* *
There’s data to read, but let’s stand around like dorks for a while anyway…
* *    delay(100);* *
And then, just for giggles, lets stand around doing nothing some more.
* *  Serial1.flush();* *
Finally, after making sure the serial buffer is empty, because we’ve read everything, let’s delete all unread data.
> I already try it with lots of changes but I still can’t get it right
You don’t have ANY code to parse the array after filling it. How are we supposed to know what you tried?

I know my code is far away of perfection. I have no idea how can I do this in a good way so I'm just trying and trying.In fact I haven't noting that tells me that I already have all data out of the buffer.I was trying to control it incrementing var i and then just look at the array position that I need but this way is very danger way if I get unexpected data thought serial.Also if the sms gets more data the array will be bigger. Any extra idea?

Before trying to store and parse the data, you need to know what a message consists of. There must be something that separates the portion of the message that you want from what you don't want. In your original port, there appears to be a carriage return.

There must be something that indicates the end of the message that you are interested in. Typically, the end of the message is indicated by two consecutive carriage returns.

Print the data that you get from the device, without trying to do anything with it. Print each character twice:

char aChar =;
Serial.print("Got a ");
Serial.print(" (");
Serial.print(aChar, DEC);

If you see something like this: 0 (48) 0 (48) " (34) (10) (13) 1 (49) 2 (50) 3 (51) (10) (13) (10) (13) you will know what separates the data that you want from that that you do not want.

You need then store only the data that you do want.