code pour recevoir des trames et mettre les char sur le port usb
Dependencies: mbed
Fork of APP4 by
trame.cpp@8:ecc7c75a37ee, 2014-04-29 (annotated)
- Committer:
- RufflesAllD
- Date:
- Tue Apr 29 16:27:28 2014 +0000
- Revision:
- 8:ecc7c75a37ee
- Parent:
- 7:a6f58e5d1188
commentaires
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joGenie | 2:7515831bb5f5 | 1 | #include "trame.hpp" |
joGenie | 2:7515831bb5f5 | 2 | |
joGenie | 2:7515831bb5f5 | 3 | Trame::Trame(PinName _tx, PinName _rx) : |
joGenie | 2:7515831bb5f5 | 4 | tx(_tx), |
joGenie | 2:7515831bb5f5 | 5 | rx(_rx) |
joGenie | 2:7515831bb5f5 | 6 | { |
joGenie | 2:7515831bb5f5 | 7 | } |
joGenie | 2:7515831bb5f5 | 8 | |
joGenie | 3:350f07072089 | 9 | void Trame::sendATCommand(const char* command, const char* data, int length) |
joGenie | 2:7515831bb5f5 | 10 | { |
RufflesAllD | 5:daf08fff7abb | 11 | int tmp = length + 4; // length (grandeur du data à envoyer) 4 (API(1), FrameID(1), AT Command(2)) |
RufflesAllD | 5:daf08fff7abb | 12 | char* trm = new char[tmp + 4]; // 4 (Delimiter(1), Length(2), Checksum(1)) |
joGenie | 3:350f07072089 | 13 | |
joGenie | 3:350f07072089 | 14 | trm[0] = 0x7E; // Delimiter |
joGenie | 3:350f07072089 | 15 | trm[1] = static_cast<char>((tmp >> 8) & 0xFF); // MSB de length |
joGenie | 3:350f07072089 | 16 | trm[2] = static_cast<char>(tmp & 0xFF); // LSB length |
RufflesAllD | 5:daf08fff7abb | 17 | trm[3] = 0x09; // Type |
joGenie | 3:350f07072089 | 18 | trm[4] = 0x01; // API |
joGenie | 3:350f07072089 | 19 | trm[5] = command[0]; |
joGenie | 3:350f07072089 | 20 | trm[6] = command[1]; |
joGenie | 3:350f07072089 | 21 | for (int i = 0; i < length; i++) |
joGenie | 3:350f07072089 | 22 | trm[7+i] = data[i]; //Data |
joGenie | 4:56b1ca37a175 | 23 | trm[7+length] = crc8(trm, tmp + 3); |
joGenie | 3:350f07072089 | 24 | |
joGenie | 2:7515831bb5f5 | 25 | Serial xbee(tx, rx); |
joGenie | 4:56b1ca37a175 | 26 | for (int i = 0; i < tmp + 4; i++) |
joGenie | 4:56b1ca37a175 | 27 | xbee.putc(trm[i]); //Transmition des donnees |
joGenie | 3:350f07072089 | 28 | |
joGenie | 3:350f07072089 | 29 | wait(0.01); |
joGenie | 3:350f07072089 | 30 | delete trm; |
joGenie | 3:350f07072089 | 31 | } |
joGenie | 3:350f07072089 | 32 | |
RufflesAllD | 8:ecc7c75a37ee | 33 | // Calcul du crc8 |
joGenie | 3:350f07072089 | 34 | unsigned char Trame::crc8(const char* data, int length) |
joGenie | 3:350f07072089 | 35 | { |
joGenie | 3:350f07072089 | 36 | unsigned char crc = 0; |
joGenie | 4:56b1ca37a175 | 37 | for (int i = 3; i < length; i++) |
joGenie | 3:350f07072089 | 38 | crc += data[i]; |
joGenie | 3:350f07072089 | 39 | |
joGenie | 3:350f07072089 | 40 | return (0xFF - crc); |
RufflesAllD | 5:daf08fff7abb | 41 | } |
RufflesAllD | 5:daf08fff7abb | 42 | |
RufflesAllD | 8:ecc7c75a37ee | 43 | // fonction de debugage pour afficher toutes les informations sortie de la trame reçue |
RufflesAllD | 5:daf08fff7abb | 44 | void Trame::checkTrame(char *data, char check, int length) |
RufflesAllD | 5:daf08fff7abb | 45 | { |
RufflesAllD | 5:daf08fff7abb | 46 | Serial pc(USBTX, USBRX); |
RufflesAllD | 5:daf08fff7abb | 47 | |
RufflesAllD | 5:daf08fff7abb | 48 | pc.printf("%d : data : ", length); |
RufflesAllD | 5:daf08fff7abb | 49 | |
RufflesAllD | 5:daf08fff7abb | 50 | for (int a = 0; a < length; a++) |
RufflesAllD | 5:daf08fff7abb | 51 | { |
RufflesAllD | 5:daf08fff7abb | 52 | pc.printf("%x ", data[a]); |
RufflesAllD | 5:daf08fff7abb | 53 | } |
RufflesAllD | 5:daf08fff7abb | 54 | pc.printf(": check : %x\n\r", check); |
RufflesAllD | 5:daf08fff7abb | 55 | } |