AT Command based communication with modem A7/A6 ...

Hello,

I am trying to communicate with a modem module using ESP32 hardware serial ports. Communications is fine as long as it is unidirectional. What I need to be able to do is sent an AT command, wait for an answer and act upon that answer. No matter what I try with code in the style of below, it seems there is a timing issue or characters get lost. Any clue as to how deal with this in a clean reliable way ? Thank You !!!

while (Serial2.available())
ModemMsg = String(Serial2.read());
Serial.print(“ModemMsg=”);
Serial.println(ModemMsg);
if (ModemMsg != “+CREG: 1”) {
Serial.println(“Modem Ready. Network Initialized”);
}

if ( Serial2.available() ) {
    String ModemMsg = "";
    while ( Serial2.available() ) {
      delay(1);
      char c = Serial2.read();
      ModemMsg += c;
    }

and don’t use this if (ModemMsg != “+CREG: 1”)

use this, if ( ModemMsg.indexOf(F(“0,1”)) < 0 )

for c-string , check this out

THANK YOU !!! This will surely help me getting there !