How to read serial data within a time frame

I'm writing code for an arduino-gsm/gps module interface. Basically I write AT commands to the gsm/gps module and I am expecting a response within a time frame. For the gsm/gps module I am using, the AT command AT+CGATT=1 has a maximum response time of 75secs per the datasheet. So I am expecting the module to respond with an "OK" or "ERROR" within that time frame. Here is my arduino code to do that;

void sendCommand(const char *cmd,unsigned long timeout){
  // this is used to clear the buffer of any previous data
  while (gps.available() > 0){
    gps.read();
  }

  char c;
  char receiveBuffer[255];
  memset(receiveBuffer,0,sizeof(char)*255);
  uint8_t idx = 0;
  bool replyMatch = false;
  unsigned long timer = millis();
  gps.println(cmd);
  while (!replyMatch && (millis() - timer <= timeout)){
   if (gps.available() > 0){
      c = gps.read();

      if (c != "\n"){
        receiveBuffer[idx] = c;
        idx++;
      } else {
        //receiveBuffer[idx - 1] = "\0";
        replyMatch = true;
      }
    }
  }
  Serial.println(receiveBuffer);
}

And I call it with;

sendCommand("AT+CGATT=1",75000);

Although the code works, it always waits for 75 seconds before printing out the result to the serial monitor although the gsm/gps module responded earlier. I tried changing the time to 100 seconds and again it waited that long to print the response on the serial monitor. In my case however the CGATT command actually executes in about 10secs and I am expecting the serial monitor to print out the result in that time even if I set the maximum timeout to 75secs. Any help on how to get this to work as intended is greatly appreciated. Thanks in advance.

I have not read everything but this sticks out

That’s comparing a char with a pointer. The compiler should give you a warning (and the test will likely never be false - ie it’s always different so you never see the line feed)

You want single quotes for a character

                replyMatch = true;

At this point in the code you have received a message so you can break out of the while loop

are you sure the last char is a \n?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.