Receving several strings via serial port

Hello

I've got a problem and I don't know how can I overcome it. I'm trying to read SMS from GSM Modem using Arduino by sending
specific AT command to it. In return, the modem sends me back the sms, however it is split by carriage returns and line feeds.
In fact, there are 2 parts of sms that I'm interested in. The format send by Modem looks like that :

-confirm, send by, data - <CR><LF>
-actual message- <CR><LF>

I'm willing to retrieve second part, the message, but don't know how. For testing I was using the code :

String rsms="";

void setup()
{
  Serial.begin(9600);
  Serial2.begin(9600);
}

void receive_sms()
{
  Serial2.print("AT+CMGR=1\r");
  if (Serial2.available()>0)
  {
    int l=Serial.available();
    for (int i=0;i<l;i++)
    {
             rsms += (char)Serial.read();
     }
  }
  Serial.println(rsms);
}

void loop()
{
  receive_sms();
  delay(10000);
}

As expected, it only prints out the first line send by Modem, since serial.read stops at . Adding another read right after the first also didn't help. Is there a possibility to read the second part send by modem ?

Thanks in advance for your help

As expected, it only prints out the first line send by Modem, since serial.read stops at .

No, it doesn't. Serial data transmission is ssslllooowww. When that snippet of code runs, not all the response has arrived yet. Yet, you have made the assumption that it has.

Adding another read right after the first also didn't help.

If you didn't wait for more data to arrive, of course it didn't.

Is there a possibility to read the second part send by modem ?

Of course. By now, you might have even imagined how.

So adding a short delay between those two readings should do the trick ? I guess then, that serial reads till the buffer is empty, then I guess I need to wait a bit till it will get new data.

So adding a short delay between those two readings should do the trick ?

Maybe. Maybe not.

The real solution is to read data, and store it, until you get the end of packet marker. In your case, that is the 2nd carriage return and line feed.

Ok, will try it, thanks for the help :slight_smile: