I have this code to read a SMS:

String read_SMS(int index)
  Serial.println("--START READ SMS POSITION" + String(index) + "--");
  sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
  String tmp;
  tmp = readATcommand("AT+CMGR=1", 4000);
  Serial.println("Content: " + tmp);
  Serial.println("--END READ SMS POSITION" + String(index) + "--");

String readATcommand(String ATcommand, unsigned int timeout)
  char response[800];
  unsigned long previous;
  memset(response, '\0', 800);    // Initialice the string

  int x = 0;

  previous = millis();
  do {
    if (GSMSerial.available() > 0) {
      response[x] = GSMSerial.read();
  } while ((millis() - previous) < timeout);   // Waits for the asnwer with time out

  return (String) response;

// sending AT commands to the Sim808 module
int8_t sendATcommand(String ATcommand, char* expected_answer, unsigned int timeout) {

  uint8_t x = 0, answer = 0;
  char response[200];
  unsigned long previous;

  memset(response, '\0', 200);    // Initialice the string


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

  if (ATcommand[0] != '\0') {
    // Send the AT command
    //Serial.print(F("AT Command: "));

  // Waiting for the answer
  x = 0;
  previous = millis();

  do {

    // if there are data in the UART input buffer, reads it and checks for the asnwer
    if (GSMSerial.available() > 0) {

      response[x] = GSMSerial.read();
      // check if the desired answer (OK) is in the response of the module
      if (strstr(response, expected_answer) != NULL) {
        answer = 1;

  } while ((answer == 0) && ((millis() - previous) < timeout));   // Waits for the asnwer with time out

  //Serial.println(F("START RESPONSE:"));
  //Serial.println(F("END RESPONSE"));
  //if (answer == 0) {
  //  Serial.println("Could not get the expected answer!");
  //else {
  //  Serial.println("Found expected answer!");

  return answer;

What can be wrong in the readATcommand function as it replies with an incomplete response:


+CMGR: "REC READ","+407245402XX","","17/06/29,10:

But, when I run it from a terminal, the AT+CMGR=1 replies with:


+CMGR: "REC READ","+40724540234","","17/06/29,10:03:06+12"


As you can see the message body is not there + the time of the SMS is also incomplete.

Thank you!

  • What is GSMSerial?
    – Majenko
    Jun 30, 2017 at 10:21
  • @Majenko #define GSMSerial Serial3 Jun 30, 2017 at 10:29
  • 1
    My guess is that your gratuitous use of the delay() function is making the serial buffer overflow. There is never any call to use delay() when reading from serial. The AT protocol has proper handshaking signals that you should use, such as looking for "OK". There is no need to delay() while waiting for them, that's just silly.
    – Majenko
    Jun 30, 2017 at 10:32
  • Works now. At first I got to reply so I put the delays. After the delays I got the partial messages. Now without any delays .. I get the full messages. Thanks! Jun 30, 2017 at 10:54

1 Answer 1


Don't delay.

In the three seconds that you wait between sending the AT command and reading the response the entire response has arrived and been placed in the serial RX buffer - which has overflowed because there's more than 64 bytes in the response. So you lose data.

You should never use delay() when reading from serial, it's dangerous (as you see).

Instead you should be just immediately reading until you see the correct response from the command you sent - which will either be "OK" or "ERROR". Everything before that is the content of the response to your command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.