Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

Fork of ATParser by NetworkSocketAPI

Revision:
16:8f98e042b9b4
Parent:
13:46a18ad08efc
--- a/ATParser.cpp	Fri Apr 22 13:40:54 2016 +0100
+++ b/ATParser.cpp	Mon May 22 18:05:51 2017 +0000
@@ -178,8 +178,8 @@
     }
 
     // Finish with newline
-    for (int i = 0; _delimiter[i]; i++) {
-        if (putc(_delimiter[i]) < 0) {
+    for (int i = 0; _delimiter_send[i]; i++) {
+        if (putc(_delimiter_send[i]) < 0) {
             return false;
         }
     }
@@ -200,7 +200,7 @@
         int offset = 0;
 
         while (response[i]) {
-            if (memcmp(&response[i+1-_delim_size], _delimiter, _delim_size) == 0) {
+            if (memcmp(&response[i+1-_delim_size_close], _delimiter_close, _delim_size_close) == 0) {
                 i++;
                 break;
             } else if (response[i] == '%' && response[i+1] != '%' && response[i+1] != '*') {
@@ -227,6 +227,7 @@
         // We keep trying the match until we succeed or some other error
         // derails us.
         int j = 0;
+        _delim_check = 0;
 
         while (true) {
             // Recieve next character
@@ -242,24 +243,38 @@
             sscanf(_buffer+offset, _buffer, &count);
 
             // We only succeed if all characters in the response are matched
-            if (count == j) {
-                debug_if(dbg_on, "AT= %s\r\n", _buffer+offset);
-                // Reuse the front end of the buffer
-                memcpy(_buffer, response, i);
-                _buffer[i] = 0;
+            if (count != -1)
+            {
+                if (j > count) {
+                    
+                    if(c == _delimiter_close[_delim_check]) {
+                        
+                        _delim_check++;
+                        
+                        if(_delim_check == _delim_size_close) {
+                            
+                            debug_if(dbg_on, "AT= %s\r\n", _buffer+offset);
+                            // Reuse the front end of the buffer
+                            memcpy(_buffer, response, i);
+                            _buffer[i] = 0;
 
-                // Store the found results
-                vsscanf(_buffer+offset, _buffer, args);
+                            // Store the found results
+                            vsscanf(_buffer+offset, _buffer, args);
 
-                // Jump to next line and continue parsing
-                response += i;
-                break;
+                            // Jump to next line and continue parsing
+                            response += i;
+                            break;
+                        }
+                    } else {
+                        _delim_check = 0;
+                    }
+                }
             }
 
             // Clear the buffer when we hit a newline or ran out of space
             // running out of space usually means we ran into binary data
             if (j+1 >= _buffer_size - offset ||
-                strcmp(&_buffer[offset + j-_delim_size], _delimiter) == 0) {
+                strcmp(&_buffer[offset + j-_delim_size_close], _delimiter_close) == 0) {
 
                 debug_if(dbg_on, "AT< %s", _buffer+offset);
                 j = 0;