I use the SeeedStudio GRPS shield v3 with Arduino UNO or MEGA (HardwareSerial or SoftwareSerial)
Below 2 sketches very similars but who don't use the same method to handle modem responses.
In the first sketch, the at_recv function reads data until the response is read and timeout if not.
In the second one, the at_recv function reads data until the number of lines given in parameter is read. When we send an AT command, we know how many lines the modem will give us back and we never timeout even if the modem response is not the one we wait for.
I've also written a complete library which use PROGMEM storage for all the strings, but I have not yet documented enough to publish it and it exists some like tinyGSM wich are very complete.
int at_recv(const char* resp, char *buff, int len, unsigned long timeout) {
unsigned long rxstart = millis();
int rxmatch = 0, rxlen = 0;
while (1) {
if (gprs.available()) {
char c = (char) gprs.read();
if (buff != NULL) {
buff[rxlen++] = c;
if (rxlen == len) break;
}
rxmatch = (c == resp[rxmatch]) ? (rxmatch + 1) : 0;
if (resp[rxmatch] == '\0') break;
} else delay(10);
if ((millis() - rxstart) > timeout) return 0;
}
return rxlen;
}
int at_recv(char* buff, int len, int clf, unsigned long timeout) {
unsigned long rxstart = millis();
int rxlen = 0, rxclf = 0;
do {
if (gprs.available()) {
byte c = gprs.read();
buff[rxlen++] = c;
if (c == 10) rxclf++;
}else delay(10);
if ((millis() - rxstart) > timeout) break;
} while ((rxlen != len) && (rxclf != clf));
return rxlen;
}
test1.ino (6.41 KB)
test2.ino (6.66 KB)