Can't read incoming SMS sender

Hello, I have Arduino Leonardo and SIM800H and I made a program that can read the time and wait for SMS message to arrive. I don't know where the problem is, but when the answer from AT+CMGR=1 (read the first message) comes the arduino starts reading it, but stops after the first few digits. Please someone tell me where is the problem in my program.

Here's what should happen:

SIM800H: +CMTI: "SM",1 Arduino: AT+CMGR=1 SIM800H: +CMGR: "REC READ","+1234567890","","04/01/01,00:00:00+00" (on a new line) message text

But it gets as far as: +CMGR: "REC READ","+1 (in arduino serial monitor i get debug info)

and hangs.

Thanks!

This is the code: (declarations omitted) (I marked where I think the problem is)

void setup() {
  Serial.begin(9600); //USB to Computer
  Serial1.begin(9600); //UART to Modem
  Serial1.print("ATE0\r"); //Disable echo
}

void loop() {
  if (millis() - currentMillis >= interval) //This is done every second
  {
    if (enableclock == 1) {
      Serial1.print("AT+CCLK?\r");
    }
    Serial.print ("=========");
    //Serial1.print("AT+CCLK?\r"); //ask for the time
    //delay(50);
    Serial.println("");
    Serial.println("=====");
    Serial.println(millis());

    if (enableclock == 1) {
      Serial.println("YEAR: ");
      Serial.print(rtcy1[0]);
      Serial.println(rtcy1[1]);
      Serial.println("MONTH: ");
      Serial.print(rtcm1[0]);
      Serial.println(rtcm1[1]);
      Serial.println("DAY: ");
      Serial.print(rtcd1[0]);
      Serial.println(rtcd1[1]);
      Serial.println("HOUR: ");
      Serial.print(rtch1[0]);
      Serial.println(rtch1[1]);
      Serial.println("MINUTE: ");
      Serial.print(rtcmm1[0]);
      Serial.println(rtcmm1[1]);
      Serial.println("SECOND: ");
      Serial.print(rtcs1[0]);
      Serial.println(rtcs1[1]);
    }
    enabledtime = 1;

    currentMillis = millis();
  }

  if (enabledtime == 1) {
    if (Serial1.available () > 0) {
      foundchar[0] = foundchar[1];
      foundchar[1] = foundchar[2];
      foundchar[2] = foundchar[3];
      foundchar[3] = foundchar[4];
      foundchar[4] = foundchar[5];
      foundchar[5] = Serial1.read();

      if (foundchar[0] == '+' )
      {
        if (foundchar[1] == 'C' )
        {
          if (foundchar[2] == 'C' )
          {
            if (foundchar[3] == 'L' && foundchar[4] == 'K' && foundchar[5] == ':') {
              cclkenabl = 1;
              pointingfinger = 0;
              while (foundchar[0] != '\n') {
                if (Serial1.available() > 0) {
                  serialdata[pointingfinger] = foundchar[0];
                  serialdata[pointingfinger + 1] = 0;
                  pointingfinger++;
                  foundchar[0] = foundchar[1];
                  foundchar[1] = foundchar[2];
                  foundchar[2] = foundchar[3];
                  foundchar[3] = foundchar[4];
                  foundchar[4] = foundchar[5];
                  foundchar[5] = Serial1.read();

                }
              }
              cclkenabl = 0;
              //pointingfinger = 0;
              enabledtime = 0;
              rtcy1[0] = serialdata[searchcharpos + 8]; //getting first char with its offset
              rtcy1[1] = serialdata[searchcharpos + 9];
              rtcy1[2] = '\0';
              rtcm1[0] = serialdata[searchcharpos + 11];
              rtcm1[1] = serialdata[searchcharpos + 12];
              rtcm1[2] = '\0';
              rtcd1[0] = serialdata[searchcharpos + 14];
              rtcd1[1] = serialdata[searchcharpos + 15];
              rtcd1[2] = '\0';
              rtch1[0] = serialdata[searchcharpos + 17];
              rtch1[1] = serialdata[searchcharpos + 18];
              rtch1[2] = '\0';
              rtcmm1[0] = serialdata[searchcharpos + 20];
              rtcmm1[1] = serialdata[searchcharpos + 21];
              rtcmm1[2] = '\0';
              rtcs1[0] = serialdata[searchcharpos + 23];
              rtcs1[1] = serialdata[searchcharpos + 24];
              rtcs1[2] = '\0';
            }
          }
          if (foundchar[2] == 'M')
          {
            if (foundchar[3] == 'T')
            {
              if (foundchar[4] == 'I')
              {
                if (foundchar[5] == ':')
                {
                  cmtienabl = 1;
                  pointingfinger = 0;
                  enableclock = 0;
                  while (foundchar[0] != '\n') {
                    if (Serial1.available() > 0) {
                      serialdata[pointingfinger] = foundchar[0];
                      serialdata[pointingfinger + 1] = 0;
                      pointingfinger++;
                      foundchar[0] = foundchar[1];
                      foundchar[1] = foundchar[2];
                      foundchar[2] = foundchar[3];
                      foundchar[3] = foundchar[4];
                      foundchar[4] = foundchar[5];
                      foundchar[5] = Serial1.read();

                    }
                  }
                  cmtienabl = 0;
                  //pointingfinger = 0;
                  enabledtime = 0;
                  esemesemes[0] = serialdata[searchcharpos + 9]; //getting first char with its offset
                  esemesemes[1] = serialdata[searchcharpos + 10];
                  esemesemes[2] = '\0';
                  esemindex[0] = serialdata[searchcharpos + 13];
                  esemindex[1] = '\0';
                  Serial.println(esemindex[0]);
                  Serial1.println("AT+CMGF=1");
                  delay(100);
                  Serial1.println("AT+CMGR=1");
                }
              }
            }
            if (foundchar[3] == 'G') {
              if (foundchar[4] == 'R') {
                if (foundchar[5] == ':') {
                  while (true) {
                    foundchar[7] = Serial1.read(); //          IS THE PROBLEM HERE?
                    //if (Serial1.available() > 0) {
                      foundchar[0] = foundchar[1];
                      foundchar[1] = foundchar[2];
                      foundchar[2] = foundchar[3];
                      foundchar[3] = foundchar[4];
                      foundchar[4] = foundchar[5];
                      foundchar[5] = foundchar[6];
                      foundchar[6] = foundchar[7];
                      Serial.print(foundchar[6]);
                    //}
                  }
                  Serial.println("PHONE: ");
                  Serial.println(acceptsms);
                  Serial.println("");
                  acceptsms = 0;
                }
              }
            }
          }
        }
      }
    }
  }
}
                  while (true) {
                    foundchar[7] = Serial1.read(); //          IS THE PROBLEM HERE?
                    //if (Serial1.available() > 0) {
                      foundchar[0] = foundchar[1];
                      foundchar[1] = foundchar[2];
                      foundchar[2] = foundchar[3];
                      foundchar[3] = foundchar[4];
                      foundchar[4] = foundchar[5];
                      foundchar[5] = foundchar[6];
                      foundchar[6] = foundchar[7];
                      Serial.print(foundchar[6]);
                    //}
                  }

Once this loop is entered, there is no way to exit the loop. You are reading data even though there may be nothing to read.

You REALLY need to think about using functions. You need to think about the fact that communication with the SIM800H is asynchronous, not synchronous. When you know that the serial buffer contains "GR:", call a function to read and store the COMPLETE response, and nothing but the complete response. Then, parse the data that you stored.