Serial comms between HC-05 and arduino - stumped

I have a HC-05 connected to an arduino micro via a level shifter.

If I put this in my loop function and type AT in the Serial monitor then I get a response from the HC-05

  // READ from HC-05 and WRITE to Arduino Serial Monitor
  if (serialHC05.available())
    Serial.write(serialHC05.read());

  // READ Arduino Serial Monitor and WRITE to HC-05
  if (Serial.available())
    serialHC05.write(Serial.read());

But if I try and use the following function to send AT to the HC-05 I get no response:

void sendATCommand(const char *pchCommand)
{
    uint32_t nStartMillis = millis();
    String strResp;
    
    serialHC05.println(pchCommand);
    Serial.print(String(pchCommand) + "...");

    while (true)
    {
Serial.println(serialHC05.available());
      while (serialHC05.available()) 
      {
        strResp += String((char)serialHC05.read());
Serial.println(strResp);
        if (!serialHC05.available())
        {
          Serial.println(strResp.c_str());
          break;
        }
      }
      if ((millis() - nStartMillis) > 4000)
      {
        Serial.println("no response!");
        break;
      }
    }
}

What am I not understanding here about serial comms on the arduino?

What am I not understanding here about serial comms on the arduino?

Maybe nothing. You are abusing the hell out of the String class, though, and probably trashing memory all to hell and back.

Forget that the String class exists. Learn to use char arrays.

PaulS:
Maybe nothing. You are abusing the hell out of the String class, though, and probably trashing memory all to hell and back.

Forget that the String class exists. Learn to use char arrays.

Why am I abusing the hell out of the string class?

Why am I abusing the hell out of the string class?

That was my question. Perhaps yours was meant to be "How am...".

        strResp += String((char)serialHC05.read());

The += operator is overloaded. It can add a char to a String. There is no reason to invoke the String constructor to create a String containing one character, then invoke the String copy constructor to make a copy for the += operator to use, and then invoke (indirectly) two destructors to delete those two instances of the String class, leaving small holes in memory.

Every time you add a character to a String, space is allocated to hold the string that the instance currently wraps plus the contents of the new string (which can be a single character), then the existing string is copied to the new location and the new data appended, and then the old memory is freed, resulting in another hole in memory.

AT command responses are pretty small. AT devices are not Chatty-Kathy devices. So, using a fixed size char array is a much better practice.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data without using the String class.

It is not a good idea to use the String (capital S) class as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R