Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

Dependents:   ESP8266 Final_Project_2 Final_Project CHEN_Final_Project

Revision:
2:4d68f546861c
Parent:
1:66a14afe650a
Child:
3:32915b9467d2
--- 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