Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of CRIC_RFID by
Diff: main.cpp
- Revision:
- 2:41df6e176f9a
- Parent:
- 1:6713f9d95bad
- Child:
- 3:1a298994f6ba
--- a/main.cpp Wed Nov 12 10:52:12 2014 +0000
+++ b/main.cpp Thu Nov 13 15:48:01 2014 +0000
@@ -6,24 +6,34 @@
Serial comRFID(p13, p14); // tx, rx //Communication entre le mbed et le RFID SM130
void flashingLED();
-void sendFrame(unsigned char command, unsigned char lengthData, unsigned char *data);
+
+void TxFrame(unsigned char command, unsigned char lengthData, unsigned char *data);
+unsigned int RxFrame(unsigned char *data);
+unsigned char calcCSUM(unsigned char lengthData, unsigned char command, unsigned char *valData);
int main() {
- unsigned char tabData[20];
+ unsigned char tabDataTx[20], tabDataRx[20];
debugPC.baud(19200);
+ comRFID.baud(19200);
+
+ debugPC.printf("\n\n\n\rAttente 2 secondes...");
+ wait(2); //Attente démarrage du mbed
debugPC.printf("\n\rHello World!\n");
while(1) {
- sendFrame(0x80, 0x00, tabData);
+ TxFrame(0x81, 0x00, tabDataTx);
if(comRFID.readable() == 1){
- debugPC.printf("\nOK");
+ debugPC.printf("\n\r\tRx IN!");
+ if(RxFrame(tabDataRx) != 0)
+ debugPC.printf("\n\r\tRx PROBLEM!");
+ debugPC.printf("\n\r\tRx OUT!");
}
flashingLED();
}
}
-void sendFrame(unsigned char command, unsigned char lengthData, unsigned char *data){
+void TxFrame(unsigned char command, unsigned char lengthData, unsigned char *dataTx){
unsigned char csum = 0;
lengthData += 1; // +1 pour ajout taille commande
@@ -33,27 +43,82 @@
comRFID.putc(lengthData ); //LENGTH
comRFID.putc(command); //COMMAND
for(unsigned int cpt; cpt<lengthData; cpt++){ //DATA
- comRFID.putc(*(data+cpt));
+ comRFID.putc(*(dataTx+cpt));
}
- //Calcul du checksum
- csum += lengthData;
- csum += command;
- for(unsigned int cpt; cpt<lengthData-1; cpt++)
- csum += *(data + cpt);
-
- comRFID.putc(csum); //CSUM
- //Affiche debug
- debugPC.printf("\n\rTX :\t");
+ csum = calcCSUM(command, lengthData, dataTx);
+ comRFID.putc(csum); //CSUM
+
+ //Affiche debug __________________________________________
+ debugPC.printf("\n\rTx :\t");
debugPC.printf("FF ");
debugPC.printf("00 ");
- debugPC.printf("%x ", lengthData );
- debugPC.printf("%x ", command);
- for(unsigned int cpt; cpt<lengthData; cpt++)
- debugPC.printf("%x ", *(data+cpt));
- debugPC.printf("%x ", csum);
+ debugPC.printf("%02x ", lengthData );
+ debugPC.printf("%02x ", command);
+ for(unsigned char cpt; cpt<lengthData; cpt++)
+ debugPC.printf("%02x ", *(dataTx+cpt));
+ debugPC.printf("%02x ", csum);
}
+unsigned int RxFrame(unsigned char *dataRx){
+ unsigned char csum;
+
+ //Reception HEADER
+ if(comRFID.readable() == 1)
+ *(dataRx + 0) = comRFID.getc();
+ if(*(dataRx + 0) != 0xFF)
+ return 1;
+ debugPC.printf("\n\r\tRx HEADER %02x", *(dataRx + 0));
+
+ //Reception RESERVED
+ if(comRFID.readable() == 1)
+ *(dataRx + 1) = comRFID.getc();
+ if(*(dataRx + 1) != 0x00)
+ return 1;
+ debugPC.printf("\n\r\tRx RESERVED %02x", *(dataRx + 1));
+
+ //Reception LENGTH
+ if(comRFID.readable() == 1)
+ *(dataRx + 2) = comRFID.getc();
+ debugPC.printf("\n\r\tRx LENGTH %02x\n\r\tRx BOUCLE", *(dataRx + 2));
+
+ //Reception COMMAND, DATA, CSUM
+ for(unsigned char cpt = 3; cpt<*(dataRx + 2); cpt++){
+ if(comRFID.readable() == 1){
+ debugPC.printf(".");
+ *(dataRx + cpt) = comRFID.getc();
+ debugPC.printf(" (cpt : %02x) %02x .", cpt, *(dataRx + cpt));
+ }
+ else {
+ debugPC.printf("\n\r\tRx BOUCLE OUT");
+ return 1;
+ }
+ }
+
+ csum = calcCSUM(*(dataRx+0), *(dataRx+1), (dataRx + 2));
+ debugPC.printf("\n\r\tRx VAL CALC CSUM %02x\n\r\t", csum);
+ if(csum != *(dataRx + *(dataRx+2) + 3))
+ return 1;
+
+ //Affiche debug __________________________________________
+ debugPC.printf("\n\rRx :\t");
+ for(unsigned char cpt = 0; cpt<*(dataRx + 2) + 2; cpt++)
+ debugPC.printf("%02x ", *(dataRx+cpt));
+
+ return 0;
+}
+
+unsigned char calcCSUM(unsigned char command, unsigned char lengthData, unsigned char *valData){
+ unsigned char csum = 0;
+
+ csum += command;
+ csum += lengthData;
+ for(unsigned int cpt; cpt<lengthData-1; cpt++)
+ csum += *(valData + cpt);
+
+ return csum;
+}
+
void flashingLED() {
myled = 1;
wait(0.2);
