19
19
20
20
#include " Modem.h"
21
21
22
+ #define MODEM_MIN_RESPONSE_OR_URC_WAIT_TIME_MS 20
23
+
22
24
bool ModemClass::_debug = false ;
23
25
ModemUrcHandler* ModemClass::_urcHandlers[MAX_URC_HANDLERS] = { NULL };
24
26
@@ -28,6 +30,7 @@ ModemClass::ModemClass(Uart& uart, unsigned long baud, int resetPin, int dtrPin)
28
30
_resetPin(resetPin),
29
31
_dtrPin(dtrPin),
30
32
_lowPowerMode(false ),
33
+ _lastResponseOrUrcMillis(0 ),
31
34
_atCommandState(AT_COMMAND_IDLE),
32
35
_ready(1 ),
33
36
_responseDataStorage(NULL )
@@ -171,8 +174,12 @@ void ModemClass::send(const char* command)
171
174
delay (5 );
172
175
}
173
176
174
- unsigned long dif=millis ()-_uartMillis;
175
- if (dif<20 ) delay (20 -dif);
177
+ // compare the time of the last response or URC and ensure
178
+ // at least 20ms have passed before sending a new command
179
+ unsigned long delta = millis () - _lastResponseOrUrcMillis;
180
+ if (delta < MODEM_MIN_RESPONSE_OR_URC_WAIT_TIME_MS) {
181
+ delay (MODEM_MIN_RESPONSE_OR_URC_WAIT_TIME_MS - delta);
182
+ }
176
183
177
184
_uart->println (command);
178
185
_uart->flush ();
@@ -220,7 +227,6 @@ void ModemClass::poll()
220
227
{
221
228
while (_uart->available ()) {
222
229
char c = _uart->read ();
223
- _uartMillis=millis ();
224
230
225
231
if (_debug) {
226
232
Serial.write (c);
@@ -239,6 +245,8 @@ void ModemClass::poll()
239
245
_buffer.trim ();
240
246
241
247
if (_buffer.length ()) {
248
+ _lastResponseOrUrcMillis = millis ();
249
+
242
250
for (int i = 0 ; i < MAX_URC_HANDLERS; i++) {
243
251
if (_urcHandlers[i] != NULL ) {
244
252
_urcHandlers[i]->handleUrc (_buffer);
@@ -254,6 +262,8 @@ void ModemClass::poll()
254
262
255
263
case AT_RECEIVING_RESPONSE: {
256
264
if (c == ' \n ' ) {
265
+ _lastResponseOrUrcMillis = millis ();
266
+
257
267
int responseResultIndex = _buffer.lastIndexOf (" OK\r\n " );
258
268
if (responseResultIndex != -1 ) {
259
269
_ready = 1 ;
0 commit comments