Skip to content

Commit 9415de4

Browse files
committed
Rename var and update timestamp after receiving response or URC
1 parent d6e325b commit 9415de4

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Modem.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "Modem.h"
2121

22+
#define MODEM_MIN_RESPONSE_OR_URC_WAIT_TIME_MS 20
23+
2224
bool ModemClass::_debug = false;
2325
ModemUrcHandler* ModemClass::_urcHandlers[MAX_URC_HANDLERS] = { NULL };
2426

@@ -28,6 +30,7 @@ ModemClass::ModemClass(Uart& uart, unsigned long baud, int resetPin, int dtrPin)
2830
_resetPin(resetPin),
2931
_dtrPin(dtrPin),
3032
_lowPowerMode(false),
33+
_lastResponseOrUrcMillis(0),
3134
_atCommandState(AT_COMMAND_IDLE),
3235
_ready(1),
3336
_responseDataStorage(NULL)
@@ -171,8 +174,12 @@ void ModemClass::send(const char* command)
171174
delay(5);
172175
}
173176

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+
}
176183

177184
_uart->println(command);
178185
_uart->flush();
@@ -220,7 +227,6 @@ void ModemClass::poll()
220227
{
221228
while (_uart->available()) {
222229
char c = _uart->read();
223-
_uartMillis=millis();
224230

225231
if (_debug) {
226232
Serial.write(c);
@@ -239,6 +245,8 @@ void ModemClass::poll()
239245
_buffer.trim();
240246

241247
if (_buffer.length()) {
248+
_lastResponseOrUrcMillis = millis();
249+
242250
for (int i = 0; i < MAX_URC_HANDLERS; i++) {
243251
if (_urcHandlers[i] != NULL) {
244252
_urcHandlers[i]->handleUrc(_buffer);
@@ -254,6 +262,8 @@ void ModemClass::poll()
254262

255263
case AT_RECEIVING_RESPONSE: {
256264
if (c == '\n') {
265+
_lastResponseOrUrcMillis = millis();
266+
257267
int responseResultIndex = _buffer.lastIndexOf("OK\r\n");
258268
if (responseResultIndex != -1) {
259269
_ready = 1;

src/Modem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ModemClass {
6868
int _resetPin;
6969
int _dtrPin;
7070
bool _lowPowerMode;
71-
unsigned long _uartMillis;
71+
unsigned long _lastResponseOrUrcMillis;
7272

7373
enum {
7474
AT_COMMAND_IDLE,

0 commit comments

Comments
 (0)