APP3 / Zigbee_communication

Dependents:   Coordinator_node Router_node

Committer:
ShaolinPoutine
Date:
Tue Feb 14 04:05:53 2017 +0000
Revision:
7:78985e92c1c5
Parent:
6:3b97770f30e6
Parent:
5:9b4d93bd6725
Child:
8:ba349a2eeb37
Merged printInt and zigbee transmit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShaolinPoutine 2:0000443a78fe 1 #include "xbee.h"
ShaolinPoutine 2:0000443a78fe 2
EmileArseneault 5:9b4d93bd6725 3 XBee::XBee(PinName reset, PinName transfer, PinName receive, Mail<char, 250>* m) :
EmileArseneault 5:9b4d93bd6725 4 rst(reset), comm(transfer, receive)
EmileArseneault 5:9b4d93bd6725 5 {
EmileArseneault 5:9b4d93bd6725 6 // Constructor
EmileArseneault 5:9b4d93bd6725 7 mail = m;
EmileArseneault 5:9b4d93bd6725 8 rst = 0;
EmileArseneault 5:9b4d93bd6725 9 wait(0.4);
EmileArseneault 5:9b4d93bd6725 10 rst = 1;
EmileArseneault 5:9b4d93bd6725 11 wait(3); // waiting for initiation
EmileArseneault 5:9b4d93bd6725 12 }
EmileArseneault 5:9b4d93bd6725 13
ShaolinPoutine 2:0000443a78fe 14 void XBee::pcPrint(char* c)
ShaolinPoutine 2:0000443a78fe 15 {
ShaolinPoutine 2:0000443a78fe 16 int i = 0;
ShaolinPoutine 2:0000443a78fe 17 while( (c)[i] != '\0')
ShaolinPoutine 2:0000443a78fe 18 {
ShaolinPoutine 2:0000443a78fe 19 mail->put(&(c[i]));
ShaolinPoutine 2:0000443a78fe 20 i++;
ShaolinPoutine 2:0000443a78fe 21 }
ShaolinPoutine 2:0000443a78fe 22 }
ShaolinPoutine 2:0000443a78fe 23
EmileArseneault 5:9b4d93bd6725 24 void XBee::printInt(int i)
EmileArseneault 5:9b4d93bd6725 25 {
EmileArseneault 5:9b4d93bd6725 26 bool signe = i > 0;
EmileArseneault 5:9b4d93bd6725 27 char *c = mail->alloc();
EmileArseneault 5:9b4d93bd6725 28 if (signe)
EmileArseneault 5:9b4d93bd6725 29 {
EmileArseneault 5:9b4d93bd6725 30 *c = '-';
EmileArseneault 5:9b4d93bd6725 31 mail->put(c);
EmileArseneault 5:9b4d93bd6725 32 }
EmileArseneault 5:9b4d93bd6725 33
EmileArseneault 5:9b4d93bd6725 34 int diviseur = 1;
EmileArseneault 5:9b4d93bd6725 35 int modulo = 10;
EmileArseneault 5:9b4d93bd6725 36 int j = 9;
EmileArseneault 5:9b4d93bd6725 37 char chiffre[10];
EmileArseneault 5:9b4d93bd6725 38
EmileArseneault 5:9b4d93bd6725 39 while (i / diviseur > 0 && j >= 0)
EmileArseneault 5:9b4d93bd6725 40 {
EmileArseneault 5:9b4d93bd6725 41 chiffre[j] = (i % modulo)/diviseur;
EmileArseneault 5:9b4d93bd6725 42 modulo *= 10;
EmileArseneault 5:9b4d93bd6725 43 diviseur *= 10;
EmileArseneault 5:9b4d93bd6725 44 j--;
EmileArseneault 5:9b4d93bd6725 45 }
EmileArseneault 5:9b4d93bd6725 46
EmileArseneault 5:9b4d93bd6725 47 j = 0;
EmileArseneault 5:9b4d93bd6725 48 bool numberHasStarted = false;
EmileArseneault 5:9b4d93bd6725 49 while (j < 10)
EmileArseneault 5:9b4d93bd6725 50 {
EmileArseneault 5:9b4d93bd6725 51 if (chiffre[j] != 0 || numberHasStarted || j == 9)
EmileArseneault 5:9b4d93bd6725 52 {
EmileArseneault 5:9b4d93bd6725 53 numberHasStarted = true;
EmileArseneault 5:9b4d93bd6725 54 c = mail->alloc();
EmileArseneault 5:9b4d93bd6725 55 *c = chiffre[j];
EmileArseneault 5:9b4d93bd6725 56 mail->put(c);
EmileArseneault 5:9b4d93bd6725 57 }
EmileArseneault 5:9b4d93bd6725 58 j++;
EmileArseneault 5:9b4d93bd6725 59 }
EmileArseneault 5:9b4d93bd6725 60 }
EmileArseneault 5:9b4d93bd6725 61
ShaolinPoutine 2:0000443a78fe 62 void XBee::printHexa(char c)
ShaolinPoutine 2:0000443a78fe 63 {
ShaolinPoutine 2:0000443a78fe 64 char *msb = mail->alloc();
ShaolinPoutine 2:0000443a78fe 65 *msb = c >> 4;
ShaolinPoutine 2:0000443a78fe 66 char *lsb = mail->alloc();
ShaolinPoutine 2:0000443a78fe 67 *lsb = c & 0xF;
ShaolinPoutine 2:0000443a78fe 68
ShaolinPoutine 2:0000443a78fe 69 if (*msb < 10)
ShaolinPoutine 2:0000443a78fe 70 *msb += 0x30;
ShaolinPoutine 2:0000443a78fe 71 else
ShaolinPoutine 2:0000443a78fe 72 *msb += 0x37;
ShaolinPoutine 2:0000443a78fe 73
ShaolinPoutine 2:0000443a78fe 74 if (*lsb < 10)
ShaolinPoutine 2:0000443a78fe 75 *lsb += 0x30;
ShaolinPoutine 2:0000443a78fe 76 else
ShaolinPoutine 2:0000443a78fe 77 *lsb += 0x37;
ShaolinPoutine 2:0000443a78fe 78
ShaolinPoutine 2:0000443a78fe 79 char * str = "0x";
ShaolinPoutine 2:0000443a78fe 80 pcPrint(str);
ShaolinPoutine 2:0000443a78fe 81 mail->put(msb);
ShaolinPoutine 2:0000443a78fe 82 mail->put(lsb);
ShaolinPoutine 2:0000443a78fe 83 str = " ";
ShaolinPoutine 2:0000443a78fe 84 pcPrint(str);
ShaolinPoutine 2:0000443a78fe 85 }
ShaolinPoutine 2:0000443a78fe 86
ShaolinPoutine 2:0000443a78fe 87 void XBee::SendATCommand(char firstChar, char secondChar, char *optionalParam, int paramLen)
ShaolinPoutine 2:0000443a78fe 88 {
ShaolinPoutine 2:0000443a78fe 89 // Frame Type 0x08
ShaolinPoutine 2:0000443a78fe 90 // Two char as parameters
ShaolinPoutine 2:0000443a78fe 91
ShaolinPoutine 2:0000443a78fe 92 char cmdtosend[10];
ShaolinPoutine 2:0000443a78fe 93 char sum = 0;
ShaolinPoutine 2:0000443a78fe 94 int cmdlength = 8;
ShaolinPoutine 2:0000443a78fe 95 int i = 0;
ShaolinPoutine 2:0000443a78fe 96
ShaolinPoutine 2:0000443a78fe 97 cmdtosend[0] = FRAMEDELIMITER;
ShaolinPoutine 2:0000443a78fe 98 cmdtosend[1] = 0x00;
ShaolinPoutine 2:0000443a78fe 99 cmdtosend[2] = 0x04 + paramLen;
ShaolinPoutine 2:0000443a78fe 100 cmdtosend[3] = 0x08;
ShaolinPoutine 2:0000443a78fe 101 cmdtosend[4] = 0x52;
ShaolinPoutine 2:0000443a78fe 102 cmdtosend[5] = firstChar;
ShaolinPoutine 2:0000443a78fe 103 cmdtosend[6] = secondChar;
ShaolinPoutine 2:0000443a78fe 104
ShaolinPoutine 2:0000443a78fe 105 // Ajouter les parametres au message
ShaolinPoutine 2:0000443a78fe 106 if(optionalParam != NULL)
ShaolinPoutine 2:0000443a78fe 107 {
ShaolinPoutine 2:0000443a78fe 108 i = 0;
ShaolinPoutine 2:0000443a78fe 109 cmdlength += paramLen;
ShaolinPoutine 2:0000443a78fe 110
ShaolinPoutine 2:0000443a78fe 111 while (i < paramLen)
ShaolinPoutine 2:0000443a78fe 112 {
ShaolinPoutine 2:0000443a78fe 113 mail->put(&(optionalParam)[i]);
ShaolinPoutine 2:0000443a78fe 114 cmdtosend[7 + i] = (optionalParam)[i];
ShaolinPoutine 2:0000443a78fe 115 i++;
ShaolinPoutine 2:0000443a78fe 116 }
ShaolinPoutine 2:0000443a78fe 117 pcPrint("\r\n\0");
ShaolinPoutine 2:0000443a78fe 118 }
ShaolinPoutine 2:0000443a78fe 119
ShaolinPoutine 2:0000443a78fe 120 // Calculate checksum
ShaolinPoutine 2:0000443a78fe 121 i = 3;
ShaolinPoutine 2:0000443a78fe 122 while (i < (cmdlength - 1))
ShaolinPoutine 2:0000443a78fe 123 {
ShaolinPoutine 2:0000443a78fe 124 sum += cmdtosend[i];
ShaolinPoutine 2:0000443a78fe 125 i++;
ShaolinPoutine 2:0000443a78fe 126 }
ShaolinPoutine 2:0000443a78fe 127 cmdtosend[cmdlength - 1] = 0xFF - sum;
ShaolinPoutine 2:0000443a78fe 128
ShaolinPoutine 2:0000443a78fe 129 // Envoyer la commande sur UART
ShaolinPoutine 2:0000443a78fe 130 i = 0;
ShaolinPoutine 2:0000443a78fe 131 while (i < cmdlength)
ShaolinPoutine 2:0000443a78fe 132 {
ShaolinPoutine 2:0000443a78fe 133 comm.putc(cmdtosend[i]);
ShaolinPoutine 2:0000443a78fe 134 i++;
ShaolinPoutine 2:0000443a78fe 135 }
ShaolinPoutine 2:0000443a78fe 136
ShaolinPoutine 2:0000443a78fe 137 wait(0.1);
ShaolinPoutine 2:0000443a78fe 138 }
ShaolinPoutine 2:0000443a78fe 139
ShaolinPoutine 2:0000443a78fe 140 char* XBee::InterpretMessage()
ShaolinPoutine 2:0000443a78fe 141 {
ShaolinPoutine 2:0000443a78fe 142 char *response = "\0";
ShaolinPoutine 2:0000443a78fe 143
ShaolinPoutine 2:0000443a78fe 144 if (comm.readable())
ShaolinPoutine 2:0000443a78fe 145 {
ShaolinPoutine 2:0000443a78fe 146
ShaolinPoutine 2:0000443a78fe 147 char start = comm.getc(); // = FRAMEDELIMITER
ShaolinPoutine 2:0000443a78fe 148 //assert
ShaolinPoutine 2:0000443a78fe 149 char len_msb = comm.getc();
ShaolinPoutine 2:0000443a78fe 150 char len_lsb = comm.getc();
ShaolinPoutine 2:0000443a78fe 151
ShaolinPoutine 2:0000443a78fe 152 int len = ((int) len_msb << 4) + (int) len_lsb;
ShaolinPoutine 2:0000443a78fe 153 char frame_data[len];
ShaolinPoutine 2:0000443a78fe 154
ShaolinPoutine 2:0000443a78fe 155 // Resolving frame type
ShaolinPoutine 2:0000443a78fe 156 char type = comm.getc();
ShaolinPoutine 2:0000443a78fe 157 len--;
ShaolinPoutine 2:0000443a78fe 158
ShaolinPoutine 2:0000443a78fe 159 switch (type){
ShaolinPoutine 2:0000443a78fe 160 case 0x88: ATCommandResponse(len);
ShaolinPoutine 2:0000443a78fe 161 break;
ShaolinPoutine 2:0000443a78fe 162 case 0x8A: ModemStatus(len);
ShaolinPoutine 2:0000443a78fe 163 break;
ShaolinPoutine 3:4c1dec78117b 164 case 0x8B: ZigBeeTransmitStatus(len);
ShaolinPoutine 3:4c1dec78117b 165 break;
ShaolinPoutine 3:4c1dec78117b 166 case 0x90: ZigBeeReceivePacket(len);
ShaolinPoutine 3:4c1dec78117b 167 break;
EmileArseneault 5:9b4d93bd6725 168 default: pcPrint("Please implement response of type ");
EmileArseneault 5:9b4d93bd6725 169 printHexa(type);
ShaolinPoutine 2:0000443a78fe 170 pcPrint("\r\n\0");
EmileArseneault 5:9b4d93bd6725 171 for (int i = 0; i <len; i++)
EmileArseneault 5:9b4d93bd6725 172 {
EmileArseneault 5:9b4d93bd6725 173 comm.getc();
EmileArseneault 5:9b4d93bd6725 174 }
ShaolinPoutine 2:0000443a78fe 175 }
ShaolinPoutine 2:0000443a78fe 176 }
ShaolinPoutine 2:0000443a78fe 177 return response;
ShaolinPoutine 2:0000443a78fe 178 }
ShaolinPoutine 2:0000443a78fe 179
ShaolinPoutine 2:0000443a78fe 180 void XBee::ATCommandResponse(int len)
ShaolinPoutine 2:0000443a78fe 181 {
ShaolinPoutine 2:0000443a78fe 182 char total = 0x88;
ShaolinPoutine 2:0000443a78fe 183 char id = comm.getc();
ShaolinPoutine 2:0000443a78fe 184 total += id;
ShaolinPoutine 2:0000443a78fe 185 char* command0 = mail->alloc();
ShaolinPoutine 2:0000443a78fe 186 char* command1 = mail->alloc();
ShaolinPoutine 2:0000443a78fe 187 *command0 = comm.getc();
ShaolinPoutine 2:0000443a78fe 188 total += *command0;
ShaolinPoutine 2:0000443a78fe 189 *command1 = comm.getc();
ShaolinPoutine 2:0000443a78fe 190 total += *command1;
ShaolinPoutine 2:0000443a78fe 191 char status = comm.getc();
ShaolinPoutine 2:0000443a78fe 192 total += status;
ShaolinPoutine 2:0000443a78fe 193 int i = 0;
ShaolinPoutine 2:0000443a78fe 194 len-= 4;
ShaolinPoutine 2:0000443a78fe 195 char data[len];
ShaolinPoutine 2:0000443a78fe 196
ShaolinPoutine 2:0000443a78fe 197 pcPrint("response to command \0");
ShaolinPoutine 2:0000443a78fe 198 mail->put(command0);
ShaolinPoutine 2:0000443a78fe 199 mail->put(command1);
ShaolinPoutine 2:0000443a78fe 200 pcPrint(" is \0");
ShaolinPoutine 2:0000443a78fe 201
ShaolinPoutine 4:e8f4d41c0fbc 202 if (len == 0)
ShaolinPoutine 4:e8f4d41c0fbc 203 {
ShaolinPoutine 4:e8f4d41c0fbc 204 switch (status)
ShaolinPoutine 4:e8f4d41c0fbc 205 {
ShaolinPoutine 4:e8f4d41c0fbc 206 case 0 : pcPrint("OK"); break;
ShaolinPoutine 4:e8f4d41c0fbc 207 case 1 : pcPrint("ERROR"); break;
ShaolinPoutine 4:e8f4d41c0fbc 208 case 2 : pcPrint("Invalid Command"); break;
ShaolinPoutine 4:e8f4d41c0fbc 209 case 3 : pcPrint("Invalid Parameter"); break;
ShaolinPoutine 4:e8f4d41c0fbc 210 case 4 : pcPrint("Tx Failure"); break;
ShaolinPoutine 4:e8f4d41c0fbc 211 default : pcPrint("Unknow error ..."); break;
ShaolinPoutine 4:e8f4d41c0fbc 212 }
ShaolinPoutine 4:e8f4d41c0fbc 213 }
ShaolinPoutine 4:e8f4d41c0fbc 214
ShaolinPoutine 2:0000443a78fe 215 while (i < len)
ShaolinPoutine 2:0000443a78fe 216 {
ShaolinPoutine 2:0000443a78fe 217 if (comm.readable())
ShaolinPoutine 2:0000443a78fe 218 {
ShaolinPoutine 2:0000443a78fe 219 data[i] = comm.getc();
ShaolinPoutine 2:0000443a78fe 220 total += data[i];
ShaolinPoutine 2:0000443a78fe 221 printHexa(data[i]);
ShaolinPoutine 2:0000443a78fe 222 i++;
ShaolinPoutine 2:0000443a78fe 223 }
ShaolinPoutine 2:0000443a78fe 224 }
ShaolinPoutine 2:0000443a78fe 225
ShaolinPoutine 2:0000443a78fe 226 char checksum = comm.getc();
ShaolinPoutine 2:0000443a78fe 227 total += checksum;
ShaolinPoutine 2:0000443a78fe 228 // Verify checksum
ShaolinPoutine 2:0000443a78fe 229 if (total != 0xFF)
ShaolinPoutine 2:0000443a78fe 230 {
ShaolinPoutine 2:0000443a78fe 231 pcPrint("Checksum is wrong\0");
ShaolinPoutine 2:0000443a78fe 232 }
ShaolinPoutine 2:0000443a78fe 233 pcPrint("\r\n\0");
ShaolinPoutine 2:0000443a78fe 234 }
ShaolinPoutine 2:0000443a78fe 235
ShaolinPoutine 2:0000443a78fe 236 void XBee::ModemStatus(int len)
ShaolinPoutine 2:0000443a78fe 237 {
ShaolinPoutine 2:0000443a78fe 238 char status = comm.getc();
ShaolinPoutine 2:0000443a78fe 239
ShaolinPoutine 2:0000443a78fe 240 switch (status){
ShaolinPoutine 2:0000443a78fe 241 case 0 : pcPrint("Hardware reset\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 242 case 1 : pcPrint("Watchdog timer reset\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 243 case 2 : pcPrint("Joined network (routers and end devices)\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 244 case 3 : pcPrint("Disassociated\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 245 case 6 : pcPrint("Coordinator started\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 246 case 7 : pcPrint("Network security key was updated\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 247 case 0x0D : pcPrint("Voltage supply limit exceeded\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 248 case 0x11 : pcPrint("Modem configuration changed while join in progress\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 249 default : pcPrint("stack error\r\n\0"); break;
ShaolinPoutine 2:0000443a78fe 250 }
ShaolinPoutine 2:0000443a78fe 251
ShaolinPoutine 2:0000443a78fe 252 char checksum = comm.getc();
ShaolinPoutine 2:0000443a78fe 253
ShaolinPoutine 2:0000443a78fe 254 checksum += 0x8A + status;
ShaolinPoutine 2:0000443a78fe 255
ShaolinPoutine 2:0000443a78fe 256 if (checksum != 0xFF)
ShaolinPoutine 2:0000443a78fe 257 {
ShaolinPoutine 2:0000443a78fe 258 pcPrint("Checksum is wrong\r\n\0");
ShaolinPoutine 2:0000443a78fe 259 }
ShaolinPoutine 3:4c1dec78117b 260 }
ShaolinPoutine 3:4c1dec78117b 261
ShaolinPoutine 3:4c1dec78117b 262 void XBee::ZigBeeTransmitStatus(int len)
ShaolinPoutine 3:4c1dec78117b 263 {
ShaolinPoutine 3:4c1dec78117b 264 char id = comm.getc();
ShaolinPoutine 3:4c1dec78117b 265 char msb = comm.getc();
ShaolinPoutine 3:4c1dec78117b 266 char lsb = comm.getc();
ShaolinPoutine 3:4c1dec78117b 267 char retry = comm.getc();
ShaolinPoutine 3:4c1dec78117b 268 char status = comm.getc();
ShaolinPoutine 3:4c1dec78117b 269 char discovery = comm.getc();
EmileArseneault 5:9b4d93bd6725 270 char checksum;
ShaolinPoutine 3:4c1dec78117b 271
EmileArseneault 5:9b4d93bd6725 272 pcPrint("Response to transmit # ");
EmileArseneault 5:9b4d93bd6725 273 printInt((int)id);
EmileArseneault 5:9b4d93bd6725 274 pcPrint(" is : ");
ShaolinPoutine 3:4c1dec78117b 275
ShaolinPoutine 3:4c1dec78117b 276 if (status == 0)
ShaolinPoutine 3:4c1dec78117b 277 {
ShaolinPoutine 3:4c1dec78117b 278 pcPrint("Success\r\n");
ShaolinPoutine 3:4c1dec78117b 279 }
ShaolinPoutine 3:4c1dec78117b 280 else
ShaolinPoutine 3:4c1dec78117b 281 {
ShaolinPoutine 3:4c1dec78117b 282 switch (status){
ShaolinPoutine 3:4c1dec78117b 283 case 0x01 : pcPrint("MAC ACK Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 284 case 0x02 : pcPrint("CCA Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 285 case 0x15 : pcPrint("Invalid destination endpoint\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 286 case 0x21 : pcPrint("Network ACK Failure\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 287 case 0x22 : pcPrint("Not Joined to Network\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 288 case 0x23 : pcPrint("Self-addressed\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 289 case 0x24 : pcPrint("Address Not Found\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 290 case 0x25 : pcPrint("Route Not Found\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 291 case 0x26 : pcPrint("Broadcast source failed to hear a neighbor relay the message\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 292 case 0x2B : pcPrint("Invalid binding table index\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 293 case 0x2C : pcPrint("Resource error lack of free buffers, timers, etc.\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 294 case 0x2D : pcPrint("Attempted broadcast with APS transmission\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 295 case 0x2E : pcPrint("Attempted unicast with APS transmission, but EE=0\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 296 case 0x32 : pcPrint("Resource error lack of free buffers, timers, etc.\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 297 case 0x74 : pcPrint("Data payload too large\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 298 default : pcPrint("Unknow error ...\r\n"); break;
ShaolinPoutine 3:4c1dec78117b 299 }
ShaolinPoutine 3:4c1dec78117b 300 }
EmileArseneault 5:9b4d93bd6725 301
EmileArseneault 5:9b4d93bd6725 302 checksum = comm.getc();
EmileArseneault 5:9b4d93bd6725 303 // Validate checksum TODO
ShaolinPoutine 3:4c1dec78117b 304 }
ShaolinPoutine 3:4c1dec78117b 305
ShaolinPoutine 3:4c1dec78117b 306 void XBee::ZigBeeReceivePacket(int len)
ShaolinPoutine 3:4c1dec78117b 307 {
ShaolinPoutine 3:4c1dec78117b 308 int i = 0;
ShaolinPoutine 3:4c1dec78117b 309 char adresse64bit[8];
ShaolinPoutine 3:4c1dec78117b 310 char adresse16bit[2];
ShaolinPoutine 3:4c1dec78117b 311 char receiveOptions;
EmileArseneault 5:9b4d93bd6725 312 char checksum;
ShaolinPoutine 3:4c1dec78117b 313
ShaolinPoutine 3:4c1dec78117b 314 while(i < 8)
ShaolinPoutine 3:4c1dec78117b 315 {
ShaolinPoutine 3:4c1dec78117b 316 adresse64bit[i] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 317 i++;
ShaolinPoutine 3:4c1dec78117b 318 }
ShaolinPoutine 3:4c1dec78117b 319
ShaolinPoutine 3:4c1dec78117b 320 adresse16bit[0] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 321 adresse16bit[1] = comm.getc();
ShaolinPoutine 3:4c1dec78117b 322
ShaolinPoutine 3:4c1dec78117b 323 receiveOptions = comm.getc();
ShaolinPoutine 3:4c1dec78117b 324
ShaolinPoutine 3:4c1dec78117b 325 pcPrint("Data received : ");
ShaolinPoutine 3:4c1dec78117b 326
ShaolinPoutine 3:4c1dec78117b 327 i = 11;
ShaolinPoutine 3:4c1dec78117b 328 while (i < len)
ShaolinPoutine 3:4c1dec78117b 329 {
ShaolinPoutine 3:4c1dec78117b 330 printHexa(comm.getc());
ShaolinPoutine 3:4c1dec78117b 331 i++;
ShaolinPoutine 3:4c1dec78117b 332 }
EmileArseneault 5:9b4d93bd6725 333 pcPrint("\r\n");
ShaolinPoutine 3:4c1dec78117b 334
EmileArseneault 5:9b4d93bd6725 335 checksum = comm.getc();
ShaolinPoutine 3:4c1dec78117b 336 // Validate checksum TODO
ShaolinPoutine 6:3b97770f30e6 337 }
ShaolinPoutine 6:3b97770f30e6 338
ShaolinPoutine 6:3b97770f30e6 339 void XBee::ZigBeeTransmit(int adresse16, int adresse64, char *data, int dataLength)
ShaolinPoutine 6:3b97770f30e6 340 {
ShaolinPoutine 6:3b97770f30e6 341 // Frame Type 0x10
ShaolinPoutine 6:3b97770f30e6 342 // 0x0000000000000000 - Reserved 64-bit address for the coordinator
ShaolinPoutine 6:3b97770f30e6 343 // 0x000000000000FFFF - Broadcast address
ShaolinPoutine 6:3b97770f30e6 344
ShaolinPoutine 6:3b97770f30e6 345 // The Transmit Status frame (0x8B) est la reponse
ShaolinPoutine 6:3b97770f30e6 346 char cmdtosend[25];
ShaolinPoutine 6:3b97770f30e6 347 char checksum = 0x00;
ShaolinPoutine 6:3b97770f30e6 348 int cmdlength = 18;
ShaolinPoutine 6:3b97770f30e6 349 int i = 3;
ShaolinPoutine 6:3b97770f30e6 350
ShaolinPoutine 6:3b97770f30e6 351 //ID command to set/read operating 64 bit PAN ID
ShaolinPoutine 6:3b97770f30e6 352 //WR command to set operating 64 bit PAN ID across reboot
ShaolinPoutine 6:3b97770f30e6 353 //OI command to read operating 16 bit PAN ID
ShaolinPoutine 6:3b97770f30e6 354 //II command to set operating 16 bit PAN ID
ShaolinPoutine 6:3b97770f30e6 355
ShaolinPoutine 6:3b97770f30e6 356 cmdtosend[0] = FRAMEDELIMITER;
ShaolinPoutine 6:3b97770f30e6 357 cmdtosend[1] = 0x00;
ShaolinPoutine 6:3b97770f30e6 358 cmdtosend[2] = 0x0E + dataLength;
ShaolinPoutine 6:3b97770f30e6 359 cmdtosend[3] = 0x10; // Frame type
ShaolinPoutine 6:3b97770f30e6 360 cmdtosend[4] = 0x01; // Frame number
ShaolinPoutine 6:3b97770f30e6 361 cmdtosend[5] = 0x00; // MSB adresse 64-bit
ShaolinPoutine 6:3b97770f30e6 362 cmdtosend[6] = 0x00;
ShaolinPoutine 6:3b97770f30e6 363 cmdtosend[7] = 0x00;
ShaolinPoutine 6:3b97770f30e6 364 cmdtosend[8] = 0x00;
ShaolinPoutine 6:3b97770f30e6 365 cmdtosend[9] = 0x00;
ShaolinPoutine 6:3b97770f30e6 366 cmdtosend[10] = 0x00;
ShaolinPoutine 6:3b97770f30e6 367 cmdtosend[11] = 0xFF;
ShaolinPoutine 6:3b97770f30e6 368 cmdtosend[12] = 0xFF; // LSB adresse 64-bit
ShaolinPoutine 6:3b97770f30e6 369 cmdtosend[13] = adresse16 >> 16; // MSB adresse 16-bit
ShaolinPoutine 6:3b97770f30e6 370 cmdtosend[14] = adresse16 && 0b0000000011111111; // LSB adresse 16-bit
ShaolinPoutine 6:3b97770f30e6 371 cmdtosend[15] = 0x00; // Broadcast Radius
ShaolinPoutine 6:3b97770f30e6 372 cmdtosend[16] = 0x00; // Options
ShaolinPoutine 6:3b97770f30e6 373
ShaolinPoutine 6:3b97770f30e6 374 // Set RF DATA
ShaolinPoutine 6:3b97770f30e6 375 if(data != NULL)
ShaolinPoutine 6:3b97770f30e6 376 {
ShaolinPoutine 6:3b97770f30e6 377 i = 0;
ShaolinPoutine 6:3b97770f30e6 378 cmdlength += dataLength;
ShaolinPoutine 6:3b97770f30e6 379
ShaolinPoutine 6:3b97770f30e6 380 while (i < dataLength)
ShaolinPoutine 6:3b97770f30e6 381 {
ShaolinPoutine 6:3b97770f30e6 382 cmdtosend[17 + i] = (data)[i];
ShaolinPoutine 6:3b97770f30e6 383 i++;
ShaolinPoutine 6:3b97770f30e6 384 }
ShaolinPoutine 6:3b97770f30e6 385 }
ShaolinPoutine 6:3b97770f30e6 386
ShaolinPoutine 6:3b97770f30e6 387 //cmdtosend[17] = 0x00; // Start RF DATA
ShaolinPoutine 6:3b97770f30e6 388 //cmdtosend[18] = 0x00;
ShaolinPoutine 6:3b97770f30e6 389 //cmdtosend[19] = 0x00;
ShaolinPoutine 6:3b97770f30e6 390 //cmdtosend[20] = 0x00;
ShaolinPoutine 6:3b97770f30e6 391 //cmdtosend[21] = 0x00;
ShaolinPoutine 6:3b97770f30e6 392 //cmdtosend[22] = 0x00;
ShaolinPoutine 6:3b97770f30e6 393 //cmdtosend[23] = 0x00;
ShaolinPoutine 6:3b97770f30e6 394 //cmdtosend[24] = 0x00; // End RF DATA
ShaolinPoutine 6:3b97770f30e6 395
ShaolinPoutine 6:3b97770f30e6 396 // Calculate checksum
ShaolinPoutine 6:3b97770f30e6 397 i = 3;
ShaolinPoutine 6:3b97770f30e6 398 while (i < (cmdlength - 1))
ShaolinPoutine 6:3b97770f30e6 399 {
ShaolinPoutine 6:3b97770f30e6 400 checksum += cmdtosend[i];
ShaolinPoutine 6:3b97770f30e6 401 i++;
ShaolinPoutine 6:3b97770f30e6 402 }
ShaolinPoutine 6:3b97770f30e6 403 cmdtosend[cmdlength - 1] = 0xFF - checksum;
ShaolinPoutine 6:3b97770f30e6 404
ShaolinPoutine 6:3b97770f30e6 405 // Envoyer la commande sur UART
ShaolinPoutine 6:3b97770f30e6 406 i = 0;
ShaolinPoutine 6:3b97770f30e6 407 while (i < cmdlength)
ShaolinPoutine 6:3b97770f30e6 408 {
ShaolinPoutine 6:3b97770f30e6 409 comm.putc(cmdtosend[i]);
ShaolinPoutine 6:3b97770f30e6 410 i++;
ShaolinPoutine 6:3b97770f30e6 411 }
ShaolinPoutine 6:3b97770f30e6 412 wait(0.1);
ShaolinPoutine 2:0000443a78fe 413 }