@@ -162,14 +162,16 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
162
162
unsigned int result_parse = 0 ;
163
163
bool restart = false ;
164
164
165
+ // I expect the answer to be in this form: "ERROR<CR><LF>" "OK<CR><LF>"
166
+ // if prompt == DO_NOT_CHECK_CMD
167
+ const bool check_prompt = (prompt != DO_NOT_CHECK_CMD);
165
168
166
169
if (_serial_debug && _debug_level >= 1 ) {
167
170
_serial_debug->print (" RAW: " );
168
171
}
169
172
170
173
unsigned long start_time = millis ();
171
- while (state != at_parse_state_t ::Completed &&
172
- state != at_parse_state_t ::ParseError) {
174
+ while (state != at_parse_state_t ::Completed) {
173
175
174
176
if (millis () - start_time > _timeout) {
175
177
res = Timeout;
@@ -212,16 +214,23 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
212
214
*/
213
215
214
216
if (c == ' +' ) {
217
+ // the answer doesn't match the expected form, we need to restart
218
+ restart = !check_prompt;
219
+
215
220
commandName += c; // prompt includes also '+'
216
221
state = at_parse_state_t ::Cmd;
217
- } else if (c == RESULT_OK[result_parse]) { // FIXME this should also account for following characters
222
+ } else if (c == RESULT_OK[result_parse]) { // FIXME this should also account for following character
223
+ // the answer doesn't match the expected form, we need to restart
224
+ restart = check_prompt;
225
+
218
226
state = at_parse_state_t ::Ok;
219
227
result_parse++;
220
228
} else if (c == RESULT_ERROR[result_parse]) { // FIXME this should also account for following characters
229
+ // the answer doesn't match the expected form, we need to restart
230
+ restart = check_prompt;
231
+
221
232
state = at_parse_state_t ::Error;
222
233
result_parse++;
223
- } else {
224
- data_res = " " ;
225
234
}
226
235
// if we uncomment this we can force strict response matching
227
236
// else {
@@ -238,7 +247,7 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
238
247
if (c == ' :' || c == ' =' ) {
239
248
commandName += c; // prompt includes also ':'
240
249
241
- if (prompt != DO_NOT_CHECK_CMD && commandName != prompt) {
250
+ if (check_prompt && commandName != prompt) {
242
251
// the response we got is not the one we were expecting, parse the wrong response till the end
243
252
// and start the parse of the next response
244
253
restart = true ;
@@ -333,13 +342,7 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
333
342
334
343
if (result_parse == strlen (RESULT_OK)) {
335
344
res = Ok;
336
-
337
- if (restart) {
338
- state = at_parse_state_t ::Begin;
339
- restart = false ;
340
- } else {
341
- state = at_parse_state_t ::Completed;
342
- }
345
+ state = at_parse_state_t ::Completed;
343
346
}
344
347
break ;
345
348
case at_parse_state_t ::Error:
@@ -353,27 +356,23 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
353
356
354
357
if (result_parse == strlen (RESULT_ERROR)) {
355
358
res = Error;
356
-
357
- if (restart) {
358
- state = at_parse_state_t ::Begin;
359
- restart = false ;
360
- } else {
361
- state = at_parse_state_t ::Completed;
362
- }
359
+ state = at_parse_state_t ::Completed;
363
360
}
364
361
break ;
365
362
case at_parse_state_t ::ParseError:
366
363
res = ParseError;
367
- if (restart) {
368
- state = at_parse_state_t ::Begin;
369
- restart = false ;
370
- } else {
371
- state = at_parse_state_t ::Completed;
372
- }
364
+ // if we get a parseError, we go back from the beginning and try again to parse, unitl the timeout expires
365
+ state = at_parse_state_t ::Begin;
366
+ restart = false ;
373
367
break ;
374
368
case at_parse_state_t ::Completed:
375
369
break ;
376
370
}
371
+
372
+ if (restart && state == at_parse_state_t ::Completed) {
373
+ state = at_parse_state_t ::Begin;
374
+ restart = false ;
375
+ }
377
376
}
378
377
379
378
if (_serial_debug && _debug_level >= 3 ) {
0 commit comments