Checking for characters in a string that is received from SW serial port

Hallo There

I have defined a SW serial port on my Arduino which I use to communicate with a sim900 gprs module. I have written some basic code whereby I send an "AT" command and check the response that I get back. Below is a snippet of the code that monitors the reply from the sim900 SW RX port (my question to follow further on in mail below).

Please note that when I send "AT" to the sim900, the response I get back is "AT OK" .

Serial.print("Sending AT command: ");
portOne.println("AT");   // Command AT sent to sim900
delay(1000);

while (portOne.available())
{
        
   delay(1000);  
                   
   if (portOne.available() >=0)  //Check if serial data has arrived and how many bytes
   {
      char c = portOne.read();  //Read each byte in sequence into char c
      readString += c;              // Load each character into a predefined string variable readString
      
      Serial.print("\n[");           // print out each character to note what the position of the character is
      Serial.print(portOne.available());
      Serial.print("] = ");
      Serial.print(c);
            
   }
 }
               
if (readString.length() >0)  //Here we print out the whole string "readString"
{
   Serial.print("The string received is: "); 
   Serial.println(readString);
   Serial.print("\n");
}

Below is the serial port text received

[9] = A
[8] = T
[7] = 
[6] = 

[5] = 
[4] = 

[3] = O
[2] = K
[1] = 
[0] = 
The string received is: AT

OK

My goal is to scan through the reply to verify that I got an "AT OK" response. To do this I thought of scanning through the string "readString" and verifying that the characters at position [9] and [8] correspond to 'AT', and the characters at position [3] and [2] correspond to 'OK'. And this is where I get stumped. I cant seem to be able to extract these specific characters. I tried using the readString.CharAt() function as shown below. Here I tried to load the character in position 3 into a char d and then I tried to print this using a "Serial.print(d)" command. But then I find that the "O" is not printed. What am I doing wrong? Why does the "Serial.print(d)" not print anything? Can you see what I am doing incorrectly?

if (readString.length() >0)  //Here we print out the whole string "readString"
{
   Serial.print("The string received is: "); 
   Serial.println(readString);
   Serial.print("\n");
   Serial.println("Lets print position [3] and check that it is the letter 'O' " );
   d = readString.charAt(3);  //Note d is defined as char at very top of program
   Serial.print(d);
}

Below is the result of the new code. Note the "Serial.print(d)" is not executed

[9] = A
[8] = T
[7] = 
[6] = 

[5] = 
[4] = 

[3] = O
[2] = K
[1] = 
[0] = 
The string received is: AT

OK


Lets print position [3] and check that it is the letter 'O'

The value you are printing in [ ] is how many characters are left in the receive buffer, that's why they count down instead of up.

You want the 7th character in the string, not the 3rd.

Hi John Thanls for the reply. I will try as you suggested.