Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

Dependents:   ESP8266 Final_Project_2 Final_Project CHEN_Final_Project

Files at this revision

API Documentation at this revision

Comitter:
geky
Date:
Thu Jul 16 22:50:43 2015 +0000
Parent:
1:66a14afe650a
Child:
3:32915b9467d2
Commit message:
Fixed issue with uninitialized variable after failed scanf

Changed in this revision

ATParser.cpp Show annotated file Show diff for this revision Revisions of this file
ATParser.h Show annotated file Show diff for this revision Revisions of this file
--- a/ATParser.cpp	Thu Jul 16 20:42:44 2015 +0000
+++ b/ATParser.cpp	Thu Jul 16 22:50:43 2015 +0000
@@ -22,7 +22,7 @@
 #include <cstdarg>
 
 // This can be defined to assist in debugging
-//#define AT_ECHO 1
+#define AT_ECHO 1
 
 
 // getc/putc handling with timeouts
@@ -168,11 +168,11 @@
                 return false;
             }
             
-            int count;
+            int count = -1;
             sscanf(_buffer+offset, _buffer, &count);
             
             // We only succeed if all characters in the response is matched
-            if ((_buffer+offset)[count] == 0) {
+            if (count >= 0 && (_buffer+offset)[count] == 0) {
                 // Reuse the front end of the buffer
                 int j;
                 for (j = 0; j < i; j++) {
--- a/ATParser.h	Thu Jul 16 20:42:44 2015 +0000
+++ b/ATParser.h	Thu Jul 16 22:50:43 2015 +0000
@@ -35,6 +35,9 @@
     char *_buffer;
     int _timeout;
     
+    // Parsing information
+    const char *_delimiter;
+    
     // Helper methods for putc/getc with timeout
     int _putc(char c);
     int _getc();
@@ -55,11 +58,14 @@
     * @param buffer_size size of internal buffer for transaction
     * @param timeout timeout of the connection
     * @param echo flag to indicate if an echo of sent characters should be expected
+    * @param delimiter string of characters to use as line delimiters
     */
-    ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000) :
+    ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000,
+             const char *delimiter = "\r\n") :
             _serial(serial),
             _buffer_size(buffer_size),
-            _timeout(timeout) {
+            _timeout(timeout),
+            _delimiter(delimiter) {
         _buffer = new char[buffer_size];
     }
     
@@ -78,16 +84,37 @@
     void setTimeout(int timeout) {
         _timeout = timeout;
     }
+    
+    /**
+    * Sets string of characters to use as line delimiters
+    *
+    * @param delimiter string of characters to use as line delimiters
+    */
+    void setDelimiter(const char *delimiter) {
+        _delimiter = delimiter;
+    }
             
     /**
-    * Issue AT commands with specified command and expected response
-    * Uses printf/scanf like format strings to be able to parse the results
+    * Issue AT commands
+    *
+    * Sends formatted command and waits for formatted response.
+    * Any recieved data that does not match the specified response 
+    * is ignored until the timeout has passed.
+    *
+    * Both the command and response use format strings that are internally
+    * passed to printf and scanf respectively.
+    * @see printf
+    * @see scanf
+    *
+    * Commands are expected to be formatted with specified delimiters.
+    * Sent commands are appended with the delimiter, and responses are
+    * seperated by delimiters before attempting to parse.
     *
     * Here are some examples:
     * @code
     * at.command("AT", "OK");
     * at.command("AT+CWMODE=%d", "OK", 3);
-    * at.command("AT+CWMODE?", "+CWMODE:%d OK", &result);
+    * at.command("AT+CWMODE?", "+CWMODE:%d\r\nOK", &result);
     * @endcode
     *
     * @param command printf-like format string of command to send