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.