Éric Bisson / Mbed 2 deprecated S5info_APP4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Files at this revision

API Documentation at this revision

Comitter:
ericbisson
Date:
Mon Mar 06 22:46:10 2017 +0000
Parent:
8:5b87b1f9d91f
Child:
10:8b066285c2e0
Commit message:
ajout crc16

Changed in this revision

CRC16.lib Show annotated file Show diff for this revision Revisions of this file
uart.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CRC16.lib	Mon Mar 06 22:46:10 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/EmLa/code/CRC16/#585ead300cab
--- a/uart.h	Mon Mar 06 22:22:02 2017 +0000
+++ b/uart.h	Mon Mar 06 22:46:10 2017 +0000
@@ -1,5 +1,6 @@
 #include <vector>
 #include "mbed.h"
+#include "CRC16.h"
 using std::vector;
 
 #define RETURN_EMPTYVECTOR(x) return vector<x>();
@@ -8,6 +9,7 @@
 const char END =        0b01111110;
 const char FLAGS =      0x00;
 const char MAX_LENGTH = 80;
+CRC16 mycrc16;
 
 vector<char> uart_read(Serial& COM, Serial& pc)
 {
@@ -16,10 +18,11 @@
     while (COM.readable())
     {
         result.push_back(COM.getc());
-        wait_ms(8); // 9600 bits/secondes, donc ~1ms par bit
+        wait_ms(1); // 9600 bits/secondes, donc ~0.1ms par bit
     }
     if (result.size() > 7)
     {
+        // Validations de base
         if (result[0] != PREAMBULE || 
             result[1] != START ||
             result[2] != result.size() - 7 ||
@@ -28,9 +31,19 @@
             RETURN_EMPTYVECTOR(char);
         }
         
-        result.push_back('\0'); // end of string pour pouvoir l'afficher
+        // Calcul du CRC
+        unsigned short currentCRC = result[result.size()-2] + result[result.size()-3]<<8;
+        
+        vector<char> charge_utile(&result[4], &result[result.size()-4]);
         
-        return vector<char>(&result[4], &result[result.size()-4]);
+        if (currentCRC != mycrc16.calculateCRC16(&charge_utile[0], charge_utile.size()))
+        {
+            RETURN_EMPTYVECTOR(char);
+        }
+        
+        charge_utile.push_back('\0'); // end of string pour pouvoir l'afficher
+        
+        return charge_utile;
     }
     RETURN_EMPTYVECTOR(char);
 }
\ No newline at end of file