voor willem test

Dependencies:   4DGL MODSERIAL mbed mbos

Files at this revision

API Documentation at this revision

Comitter:
LvdK
Date:
Mon Nov 26 14:54:26 2012 +0000
Parent:
0:6f71ca095e78
Child:
2:cdc3ccd10040
Commit message:
werkt goed

Changed in this revision

USB_receive.cpp Show annotated file Show diff for this revision Revisions of this file
decode_1.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USB_receive.cpp	Mon Nov 26 12:45:02 2012 +0000
+++ b/USB_receive.cpp	Mon Nov 26 14:54:26 2012 +0000
@@ -14,20 +14,24 @@
 
 char rx_buf[max_rx_buffer + 1];  //  !!!// : main serial receiving buffer
 char string_received[max_rx_buffer + 1]; //  !!!// : string starting with $ and ending with CR/LF
-int  string_complete = FALSE; // : no complete string received with % and CR/LF
+int  string_complete = FALSE; // : no complete string received with $ and CR/LF
 
 void char_received()
 {   // -- This functiom will be called on each RX interrupt --
-    // it fills the RX buffer and sets the string_complete flag when a string
-    // starting with $ and ending with CR/LF has been received.
+    // It fills the RX buffer and sets the string_complete flag TRUE when a string
+    // starting with $ and ending with CR/LF has been received with at least 10 characters.
     char rx_char;
-    static int $_detected = FALSE;      // : no begin of string detected
-    static int rx_buf_pntr = 0;         // : bufferpointer at begin of buffer 
+    static int $_detected = FALSE;      // : no begin of string detected, ONLY first call init !
+    static int rx_buf_pntr = 0;         // : bufferpointer at begin of buffer, ONLY first call init !
 
-    led2 = !led2;           // : toggle LED2 to show RX interrupt ( TEST ! )
+    led2 = 1    ;           // : set LED2 ON to show start of RX interrupt ( TEST ! )
     rx_char = USB.getc();   // : reading also clears receive interrupt
     USB.putc(rx_char);      // : echo character back to PC ( TEST ! ) 
     
+    
+    
+    
+    
     if ( rx_char == '$' && $_detected == FALSE ){
         $_detected = TRUE;  // : begin of string detected ( $ )
         rx_buf_pntr = 0;    // : set pointer to begin of buffer again
@@ -42,26 +46,32 @@
         $_detected = FALSE;
         string_complete = FALSE;
     }
+    
+    if ( rx_char == '\n' && $_detected == TRUE ) { 
+        // String is valid when:
+        // it starts with '$'
+        // it ends on CR
+        // it has at least 10 characters 
         
-    if ( rx_char == '\n' && $_detected == TRUE) {
-        string_complete = TRUE;
-        strncpy(string_received,rx_buf,rx_buf_pntr); 
-     
-        // Decode string:
-                       
-        //USB.printf("%s",string_received);  // show string ( TEST !)
-        
+        if ( rx_buf_pntr > 10 ) { // : check minimum length
+        strncpy(string_received,rx_buf,rx_buf_pntr);
+        string_complete = TRUE;     // : set flag !
+        // Make ready for receiving new command:
+            $_detected = FALSE;
+            rx_buf_pntr = 0;
+             
         led3 = 1;       // ( TEST ! )
         
         decode_string();
-                  
-        led3 = 0;       // ( TEST ! )
         
-        // make ready for receiving new command:
-        $_detected = FALSE;
-        string_complete = FALSE;
-        rx_buf_pntr = 0;
-    }        
+        led3 = 0;       // ( TEST ! )
+
+        }
+        else  rx_buf_pntr = 0; 
+        
+    } 
+    
+    led2 = 0;           // : set LED2 OFF to show end of RX interrupt ( TEST ! )       
 }
     
     
--- a/decode_1.cpp	Mon Nov 26 12:45:02 2012 +0000
+++ b/decode_1.cpp	Mon Nov 26 14:54:26 2012 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include "ctype.h"
 
 #define TRUE  1
 #define FALSE 0
@@ -49,13 +50,10 @@
 
     int char_cntr,i;
     int equal;
-    int command_possible;
-    int command_number;
     char key_id;
 
-    if (string_complete == TRUE) {
+    if (string_complete == TRUE) {  // : check flag
        led4 = 1;
-        command_possible = FALSE;
         command_valid = FALSE;
         key_id = '0';
 
@@ -65,7 +63,6 @@
         // Check on 5 char "$PCDU" header:
         equal = strncmp(string_received,"$PCDU",5);
         if (equal != 0) {
-            command_possible = FALSE;
             USB.printf("header is not $PCDU but : %5s\n",string_received );  // print first 5 chars ( TEST !)
         }
         else {
@@ -83,7 +80,6 @@
             } while ( char_cntr < 10);
 
             if (char_cntr == 9) { // : command found with 4 char.
-                command_possible = TRUE;
                 strncpy(command_string,&string_received[5],4);
                 USB.printf("commando is : %4s\n",command_string );  // show command with 4 chars ( TEST !)
 
@@ -95,11 +91,12 @@
                     i++;
                 } while ( i < 15);
 
-                if (equal == 0) USB.printf("gevonden commando4nummer is : %d\n",i );  // show command4 number  (TEST ! )
-
+                if (equal == 0) {
+                     USB.printf("gevonden commando4nummer is : %d\n",i );  // show command4 number  (TEST ! )
+                     command_valid = TRUE;
+                }
             }
             else if (char_cntr == 8) { // : command found with 3 char.
-                command_possible = TRUE;
                 strncpy(command_string,&string_received[5],3);
                 USB.printf("commando is : %3s\n",command_string );  // show command with 3 chars ( TEST !)
 
@@ -112,9 +109,11 @@
 
                 if (equal == 0) {
                     USB.printf("gevonden commando3nummer is : %d\n",i );  // show command3 number  (TEST ! )
+                    command_valid = TRUE;
                 }
                 else {       
                     // Test on special key commands LxT, LxS, RxT, RxS :
+                    key_id = 'x';
                     if (string_received[5] == 'L') {    // : command is LxT or LxS
                         if (string_received[7] == 'T') {
                             i = 7; // : command 7 = LxT found
@@ -135,12 +134,23 @@
                             key_id = string_received[6];
                         }                     
                     }
-                    if (key_id != '0') USB.printf("gevonden key commando is : %d, key %c \n",i, key_id );  // show command3 number and key (TEST ! )
+                    if (isdigit(key_id)) {
+                       USB.printf("gevonden key commando is : %d, key %c \n",i, key_id );  // show command3 number and key (TEST ! )
+                       command_valid = TRUE;
+                    }
+                     
                 } // end of special test <<<<<<<<<<<<
 
              }// end of char 3 test <<<<<<<<<<
 
         }// end of header test <<<<<<<<<<
+        
+        string_complete = FALSE;  // : reset flag again, because string has been analyzed
         led4 = 0;
     } // end of string complete test
+  
+ if (command_valid == TRUE) {
+        // call function to get the rest of the data fields :
+        
+ }  
 }
\ No newline at end of file