Vincent Belanger
/
Receiver
Receiver code
Xbee.cpp@0:f8873e0badb2, 2016-02-18 (annotated)
- Committer:
- vinbel93
- Date:
- Thu Feb 18 13:22:31 2016 +0000
- Revision:
- 0:f8873e0badb2
Receiver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vinbel93 | 0:f8873e0badb2 | 1 | #include "mbed.h" |
vinbel93 | 0:f8873e0badb2 | 2 | #include "Xbee.h" |
vinbel93 | 0:f8873e0badb2 | 3 | |
vinbel93 | 0:f8873e0badb2 | 4 | // Fonction d'assignation du PAN ID |
vinbel93 | 0:f8873e0badb2 | 5 | void setPanId(Serial* xbee, long long panId) |
vinbel93 | 0:f8873e0badb2 | 6 | { |
vinbel93 | 0:f8873e0badb2 | 7 | // Construction de la trame |
vinbel93 | 0:f8873e0badb2 | 8 | const int frameLength = 16; |
vinbel93 | 0:f8873e0badb2 | 9 | char frame[frameLength]; |
vinbel93 | 0:f8873e0badb2 | 10 | frame[0] = 0x7E; // Start delimiter |
vinbel93 | 0:f8873e0badb2 | 11 | frame[1] = 0x00; // Length (MSB) |
vinbel93 | 0:f8873e0badb2 | 12 | frame[2] = 0x0C; // Length (LSB) |
vinbel93 | 0:f8873e0badb2 | 13 | frame[3] = 0x08; // AT Command |
vinbel93 | 0:f8873e0badb2 | 14 | frame[4] = 0x00; // Frame ID |
vinbel93 | 0:f8873e0badb2 | 15 | frame[5] = 'I'; |
vinbel93 | 0:f8873e0badb2 | 16 | frame[6] = 'D'; |
vinbel93 | 0:f8873e0badb2 | 17 | frame[7] = (panId >> 56) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 18 | frame[8] = (panId >> 48) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 19 | frame[9] = (panId >> 40) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 20 | frame[10] = (panId >> 32) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 21 | frame[11] = (panId >> 24) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 22 | frame[12] = (panId >> 16) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 23 | frame[13] = (panId >> 8) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 24 | frame[14] = (panId >> 0) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 25 | frame[15] = checksum(frame, 3, 15); |
vinbel93 | 0:f8873e0badb2 | 26 | |
vinbel93 | 0:f8873e0badb2 | 27 | // Envoi sur le UART |
vinbel93 | 0:f8873e0badb2 | 28 | if (xbee->writeable()) |
vinbel93 | 0:f8873e0badb2 | 29 | { |
vinbel93 | 0:f8873e0badb2 | 30 | for (int i = 0; i < frameLength; i++) |
vinbel93 | 0:f8873e0badb2 | 31 | { |
vinbel93 | 0:f8873e0badb2 | 32 | xbee->putc(frame[i]); |
vinbel93 | 0:f8873e0badb2 | 33 | } |
vinbel93 | 0:f8873e0badb2 | 34 | } |
vinbel93 | 0:f8873e0badb2 | 35 | } |
vinbel93 | 0:f8873e0badb2 | 36 | |
vinbel93 | 0:f8873e0badb2 | 37 | // Envoi d'un message au coordinateur (Transmit Request) |
vinbel93 | 0:f8873e0badb2 | 38 | void transmitRequest(Serial* xbee, char* data, int size) |
vinbel93 | 0:f8873e0badb2 | 39 | { |
vinbel93 | 0:f8873e0badb2 | 40 | // Construction de la trame |
vinbel93 | 0:f8873e0badb2 | 41 | const int frameLength = 18 + size; |
vinbel93 | 0:f8873e0badb2 | 42 | char frame[frameLength]; |
vinbel93 | 0:f8873e0badb2 | 43 | frame[0] = 0x7E; // Start delimiter |
vinbel93 | 0:f8873e0badb2 | 44 | frame[1] = 0x00; // Length (MSB) |
vinbel93 | 0:f8873e0badb2 | 45 | frame[2] = 0x0E + size; // Length (LSB) |
vinbel93 | 0:f8873e0badb2 | 46 | frame[3] = 0x10; // AT Command |
vinbel93 | 0:f8873e0badb2 | 47 | frame[4] = 0x01; // Frame ID |
vinbel93 | 0:f8873e0badb2 | 48 | frame[5] = (MAC_ADDRESS_COORDINATOR >> 56) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 49 | frame[6] = (MAC_ADDRESS_COORDINATOR >> 48) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 50 | frame[7] = (MAC_ADDRESS_COORDINATOR >> 40) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 51 | frame[8] = (MAC_ADDRESS_COORDINATOR >> 32) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 52 | frame[9] = (MAC_ADDRESS_COORDINATOR >> 24) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 53 | frame[10] = (MAC_ADDRESS_COORDINATOR >> 16) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 54 | frame[11] = (MAC_ADDRESS_COORDINATOR >> 8) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 55 | frame[12] = (MAC_ADDRESS_COORDINATOR >> 0) & 0xFF; |
vinbel93 | 0:f8873e0badb2 | 56 | frame[13] = 0xFF; // Broadcast |
vinbel93 | 0:f8873e0badb2 | 57 | frame[14] = 0xFE; // Broadcast |
vinbel93 | 0:f8873e0badb2 | 58 | frame[15] = 0x00; // Broadcast radius |
vinbel93 | 0:f8873e0badb2 | 59 | frame[16] = 0x00; // Options |
vinbel93 | 0:f8873e0badb2 | 60 | |
vinbel93 | 0:f8873e0badb2 | 61 | // Ajout des données utiles |
vinbel93 | 0:f8873e0badb2 | 62 | for (int i = 0; i < size; i++) |
vinbel93 | 0:f8873e0badb2 | 63 | { |
vinbel93 | 0:f8873e0badb2 | 64 | frame[17 + i] = data[i]; |
vinbel93 | 0:f8873e0badb2 | 65 | } |
vinbel93 | 0:f8873e0badb2 | 66 | frame[17 + size] = checksum(frame, 3, 17 + size); |
vinbel93 | 0:f8873e0badb2 | 67 | |
vinbel93 | 0:f8873e0badb2 | 68 | // Envoi sur le UART |
vinbel93 | 0:f8873e0badb2 | 69 | if (xbee->writeable()) |
vinbel93 | 0:f8873e0badb2 | 70 | { |
vinbel93 | 0:f8873e0badb2 | 71 | for (int i = 0; i < frameLength; i++) |
vinbel93 | 0:f8873e0badb2 | 72 | { |
vinbel93 | 0:f8873e0badb2 | 73 | xbee->putc(frame[i]); |
vinbel93 | 0:f8873e0badb2 | 74 | } |
vinbel93 | 0:f8873e0badb2 | 75 | } |
vinbel93 | 0:f8873e0badb2 | 76 | } |
vinbel93 | 0:f8873e0badb2 | 77 | |
vinbel93 | 0:f8873e0badb2 | 78 | // Fonction de calcul de checksum |
vinbel93 | 0:f8873e0badb2 | 79 | char checksum(char* frame, int begin, int end) |
vinbel93 | 0:f8873e0badb2 | 80 | { |
vinbel93 | 0:f8873e0badb2 | 81 | char sum = 0; |
vinbel93 | 0:f8873e0badb2 | 82 | |
vinbel93 | 0:f8873e0badb2 | 83 | // Addition des bits |
vinbel93 | 0:f8873e0badb2 | 84 | for (int i = begin; i < end; i++) |
vinbel93 | 0:f8873e0badb2 | 85 | { |
vinbel93 | 0:f8873e0badb2 | 86 | sum += frame[i]; |
vinbel93 | 0:f8873e0badb2 | 87 | } |
vinbel93 | 0:f8873e0badb2 | 88 | |
vinbel93 | 0:f8873e0badb2 | 89 | return 0xFF - sum; |
vinbel93 | 0:f8873e0badb2 | 90 | } |