XBee and XBee-PRO ZigBee RF modules provide cost-effective wireless connectivity to electronic devices. They are interoperable with other ZigBee PRO feature set devices, including devices from other vendors.

Dependencies:   BufferedArray

Dependents:   MBEDminiproject

Revision:
1:3dc0ec2f9fd6
Parent:
0:837e6c48e90d
Child:
3:6b205ec8624b
--- a/Core/CoreAPI.cpp	Thu Oct 22 12:28:26 2015 +0000
+++ b/Core/CoreAPI.cpp	Thu Oct 22 20:02:11 2015 +0000
@@ -9,19 +9,17 @@
     this->serial = serial;
     isEscapeMode = escape;
     msg = new APIFrame(INITIAL_FRAME_LENGTH);
+    isChecksum = false;
     isRunning = false;
-    isChecksum = false;
 }
 
 CoreAPI::~CoreAPI()
 {
-    delete msg;
-}
-
+    if (msg != NULL)
+        delete msg;
 
-void CoreAPI::setVerifyChecksum(bool isCheck)
-{
-    isChecksum = isCheck;
+    if (serial != NULL)
+        delete serial;
 }
 
 void CoreAPI::start()
@@ -42,6 +40,11 @@
     }
 }
 
+void CoreAPI::setVerifyChecksum(bool isCheck)
+{
+    isChecksum = isCheck;
+}
+
 void CoreAPI::send(APIFrame * request)
 {
     if (!isRunning)
@@ -65,7 +68,7 @@
 {
     int value = serial->readByte();
 
-    if (isEscapeMode)
+    if (isEscapeMode && value == ESCAPED)
         return serial->readByte() ^ 0x20;
 
     return value;
@@ -86,9 +89,14 @@
 
 APIFrame * CoreAPI::getResponse()
 {
-    if (readByte() != KEY)
+    if (!isRunning)
         return NULL;
 
+    if (!serial->peek())
+        return NULL;
+
+    while (serial->readByte() != KEY);
+
     int length = getLength();
 
     msg->allocate(length);