Reading responses from Serial not working

Hi guys, I'm working on a project where I send some AT commands, and I want to send a new commands only after recieving the "OK", so I made this function where the at commands are the inputs:

int WaitResp(const char* a){

String incomingByte="";   // for incoming Serial data
int len=0;
int i=0;

Serial.flush();
delay(1000);
Serial.write(a);
 
delay(300);
        
               if(Serial.available()>0) {
                // read the incoming byte:
                incomingByte = Serial.readString();// read the incoming data as string
                // say what you got:
                
                
                len = incomingByte.length();
                char reso[len]={};
                incomingByte.toCharArray(reso, 50);
                i=len;
               
                         
                if(reso[len-3]=='K')
                return 1;
                else {
                return 0;
                }
        }    
}

it worked only for the first commands, when I did a small loop to see what's happening

for(i=len;i>1;i=i-1){

            Serial.print(reso[len-i]);
          }

it show this :

AT
AT
OK
AT+CGNSTST=0
AT
OK

OK
AT+CGNSTST=0
OK

And it's stop like this, even tho there're more commands to come.

can anyone help me to resolve this please?

NB: I'm using a hardware serial.

String incomingByte="";   // for incoming Serial data

That is a horrendously stupid name. A byte is NOT a String.

                len = incomingByte.length();
                char reso[len]={};
                incomingByte.toCharArray(reso, 50);

The second argument to the toCharArray() method is the size of the array to write to, not some number you pulled out of your ass.

Post complete code. The above output is not from the posted code. Don't use the String class, use C-style strings (null byte terminated character arrays).

Are you sure that your communication partner is always able to respond to your command within 300ms?