Class containing functions usefull to communication between PC and Xbee device
Dependents: Coordinator_node Router_node
Diff: xbee.cpp
- Revision:
- 20:e119856dbc5e
- Parent:
- 19:8da8068315da
--- a/xbee.cpp Wed Feb 15 02:20:19 2017 +0000 +++ b/xbee.cpp Wed Feb 15 05:55:34 2017 +0000 @@ -1,6 +1,6 @@ #include "xbee.h" -char TransmissionNumber = 1; +char TransmissionNumber = 2; Mutex mTransmissionNumber; char GetTransmissionNumber() @@ -11,7 +11,7 @@ nextnumber = TransmissionNumber; if(nextnumber == 255) { - TransmissionNumber = 1; + TransmissionNumber = 2; } else { @@ -22,12 +22,13 @@ return nextnumber; } -XBee::XBee(PinName reset, PinName transfer, PinName receive, Mail<char, 250>* m, Mail<char[256], 16>* w) : +XBee::XBee(PinName reset, PinName transfer, PinName receive, Mail<char, 250>* m, Mail<char[256], 16>* w, Mail<char[254], 25>* r) : rst(reset), comm(transfer, receive) { // Constructor mail = m; webmail = w; + frameresponsemail = r; rst = 0; wait(0.4); rst = 1; @@ -150,7 +151,7 @@ pcPrint(str); } -void XBee::SendATCommand(char firstChar, char secondChar, char *optionalParam, int paramLen) +char XBee::SendATCommand(char firstChar, char secondChar, char *optionalParam, int paramLen) { // Frame Type 0x08 // Two char as parameters @@ -200,6 +201,7 @@ } wait(0.2); + return cmdtosend[4]; } void XBee::InterpretMessage() @@ -246,25 +248,36 @@ void XBee::ATCommandResponse(int len) { + //char *receivedResponse = (char *)frameresponsemail->alloc(); + //receivedResponse[0] = FRAMEDELIMITER; + //receivedResponse[1] = len >> 4; + //receivedResponse[2] = len; + //receivedResponse[3] = 0x88; + char total = 0x88; - char id = getChar(); - total += id; - char* command0 = mail->alloc(); - char* command1 = mail->alloc(); - *command0 = getChar(); - total += *command0; - *command1 = getChar(); - total += *command1; - char status = getChar(); - total += status; + char id; + char command[2]; + char status; int i = 0; - len-= 4; - char data[len]; + char data[255]; + char checksum = 0; + + id = getChar(); + + command[0] = getChar(); + command[1] = getChar(); + total += command[0]; + total += command[1]; + + status = getChar(); + + len -= 4; + pcPrint("ID:"); printHexa(id); pcPrint(" response to command \0"); - mail->put(command0); - mail->put(command1); + printHexa(command[0]); + printHexa(command[1]); pcPrint(" is \0"); if (len == 0) @@ -280,20 +293,32 @@ } } + //receivedResponse[4] = id; + //receivedResponse[5] = command[0]; + //receivedResponse[6] = command[1]; + //receivedResponse[7] = status; + while (i < len) { if (comm.readable()) { data[i] = getChar(); + //receivedResponse[8 + i] = data[i]; total += data[i]; printHexa(data[i]); i++; } } - char checksum = getChar(); + checksum = getChar(); + //receivedResponse[8 + i] = checksum; + //frameresponsemail->put((char(*)[254])&receivedResponse[0]); + + // Verify checksum + total += id; + total += status; total += checksum; - // Verify checksum + if (total != 0xFF) { pcPrint("Checksum is wrong\0");