Mdot control and communication for the exosonde

Dependencies:   libmDot-mbed5 mbed

Revision:
1:3bb9d88c2646
Parent:
0:d8d49519a14a
Child:
2:e7e272186bf2
--- a/main.cpp	Mon Aug 28 03:47:42 2017 +0000
+++ b/main.cpp	Mon Aug 28 22:46:55 2017 +0000
@@ -5,19 +5,47 @@
 #include <vector>
 #include <algorithm>
 
-Serial device(USBTX, USBRX); //will need to change thistoi the appropriate pins once connected ot the exosonde
-DigitalOut led1(LED1);
+char sonde_buffer[256];
+void flushRXbuffer(Serial *serial){
+    while(serial -> readable()) serial -> getc();    
+}
+ 
+bool checkforcomma(Serial *debugger){
+    debugger -> printf("received: %s\r\n",sonde_buffer);
+    for(int i = 0; i < strlen(sonde_buffer); i++) {
+        if(sonde_buffer[i] == ','){
+            return true;       
+        }       
+    } 
+    return false;      
+}
+ 
+void getsondedata(Serial *device, Serial *debugger){
+    char datachar;
+    device -> printf("data\r\n");
+    int charcount = 0;
+    while(datachar = device -> getc(),datachar != '\r' && datachar != '\n'){
+        sonde_buffer[charcount] = datachar;
+        charcount++; 
+    }
+    //flush the remaining '\n' character from the buffer
+    flushRXbuffer(device);
+    sonde_buffer[charcount] = '\0';
+}
+
+void setcommadelim(Serial *device){
+    device -> printf("delim 2\r\n");       
+}
 int main() {
-        char datachar;
-        char* recieved = (char *) malloc(sizeof(char) * 50);;
-        int malloced = 50, charcount = 0;
-        bool commaflag = false;
-        while(datachar = device.getc(),datachar != '\r'){
-            if(!((charcount+1)%malloced)){
-                malloced = malloced*2;
-                recieved = realloc(recieved, malloced);       
-            } 
-            recieved[charcount] = datachar;
-            charcount++;  
+    Serial device(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde
+    Serial debugger(USBTX,USBRX);
+    while(1){
+        getsondedata(&device, &debugger);
+        debugger.printf("sonde: %s\r\n",sonde_buffer); 
+        if(!checkforcomma(&debugger)){
+            setcommadelim(&device);
+            continue;    
         }
-}
\ No newline at end of file
+    }
+}
+