Library used to receive data through a wireless channel using XBEE.

Dependencies:   mbed

Dependents:   nxp_can

Fork of frdm_xbee by Jose Pesado

Revision:
2:ccf471fbd56c
Parent:
1:11f87e6215f7
Child:
3:377a8165aae1
--- a/xbee.cpp	Sat Oct 18 16:46:04 2014 +0000
+++ b/xbee.cpp	Sat Oct 18 19:58:20 2014 +0000
@@ -4,16 +4,14 @@
 #define XBEE_CLIENT         1
 #define SERIAL_BAUDRATE     115200
 #define BUFFER_SIZE         14
-#define BUFFER_FORMAT       "%X %X %X %X %X %X %X %X %X %X %X %X %X %X"
-#define BUFFER_FORMAT_RX    "%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
+#define BUFFER_FORMAT       "%X %X %X %X %X %X %X %X %X"
+#define BUFFER_FORMAT_RX    "%c%c%c%c%c%c%c%c%c%c%c%c"
 #define BUFFER_RX_DATA_WR   &CommandRx[0],&CommandRx[1],&CommandRx[2],&CommandRx[3],\
                             &CommandRx[4],&CommandRx[5],&CommandRx[6],&CommandRx[7],\
-                            &CommandRx[8],&CommandRx[9],&CommandRx[10],&CommandRx[11],\
-                            &CommandRx[12],&CommandRx[13]
+                            &CommandRx[8],&CommandRx[9],&CommandRx[10],&CommandRx[11]
 #define BUFFER_RX_DATA_RD   CommandRx[0],CommandRx[1],CommandRx[2],CommandRx[3],\
                             CommandRx[4],CommandRx[5],CommandRx[6],CommandRx[7],\
-                            CommandRx[8],CommandRx[9],CommandRx[10],CommandRx[11],\
-                            CommandRx[12],CommandRx[13]
+                            CommandRx[8]
 
 Serial PC(USBTX, USBRX);
 DigitalOut Indicator(LED_GREEN);
@@ -36,18 +34,45 @@
 int XBee_boRxAllowed = 1;
 
 /* This function is called when a character goes into the RX buffer.*/
-void XBeeClientRx() {
+void XBeeClientRx() 
+{
+    int hits = 0;
+    char cStartOfFrame[1];
+    memset(CommandRx,0,BUFFER_SIZE);
     if(XBee_boRxAllowed == 1)
     {
-        PC.printf("\r\nRx... ");
-        XBee_u8RxResult = 0;
-        XBee_u8RxResult = XBeeClient.scanf( BUFFER_FORMAT_RX, BUFFER_RX_DATA_WR );
-        if(XBee_u8RxResult>0)
+        XBee_boRxIndication = 0;
+        while(1)
+        {
+            cStartOfFrame[0] = XBeeClient.getc();
+            if(cStartOfFrame[0]==0x55)
+            {
+                hits++;
+            }
+            if(hits==2)
+            {
+                break;
+            }
+        }
+        if(hits==2)
         {
-            XBee_boRxIndication = 1;
-        } 
-        PC.printf("\r\nRx done [%i]. ",XBee_u8RxResult);
-        PC.printf( BUFFER_FORMAT, BUFFER_RX_DATA_RD );
+            XBee_u8RxResult = 0;
+            XBee_u8RxResult = XBeeClient.scanf( BUFFER_FORMAT_RX, BUFFER_RX_DATA_WR );
+            if(XBee_u8RxResult>0)
+            {
+                if(CommandRx[BUFFER_SIZE-5]==0xAA && CommandRx[BUFFER_SIZE-4]==0xAA)
+                {
+                    /* next BUFFER_SIZE - 5 chars are the payload */
+                    XBee_boRxIndication = 1;   
+                }
+            }
+        }
+         
+        if(XBee_boRxIndication == 1)
+        {
+            PC.printf("\r\nRx done [%i]. ",XBee_u8RxResult);
+            PC.printf( BUFFER_FORMAT, BUFFER_RX_DATA_RD );
+        }
     }
     return;
 }