routeur done
Dependencies: mbed
Fork of APP4 by
trame.cpp
00001 #include "trame.hpp" 00002 00003 Trame::Trame(PinName _tx, PinName _rx) : 00004 tx(_tx), 00005 rx(_rx) 00006 { 00007 } 00008 00009 void Trame::sendATCommand(const char* command, const char* data, int length) 00010 { 00011 int tmp = length + 8; 00012 char* trm = new char[tmp]; 00013 00014 trm[0] = 0x7E; // Delimiter 00015 trm[1] = static_cast<char>((tmp >> 8) & 0xFF); // MSB de length 00016 trm[2] = static_cast<char>(tmp & 0xFF); // LSB length 00017 trm[3] = 0x08; // Type 00018 trm[4] = 0x01; // API 00019 trm[5] = command[0]; 00020 trm[6] = command[1]; 00021 for (int i = 0; i < length; i++) 00022 trm[7+i] = data[i]; //Data 00023 trm[7+length] = crc8(trm, tmp - 1); 00024 00025 Serial xbee(tx, rx); 00026 for (int i = 0; i < tmp; i++) 00027 xbee.putc(trm[i]); 00028 00029 wait(0.01); 00030 delete trm; 00031 } 00032 00033 void Trame::sendTransmitRequest(const char* destination, const char* data, int length) 00034 { 00035 int tmp = length + 14; 00036 char* trm = new char[tmp + 4]; 00037 00038 trm[0] = 0x7E; // Delimiter 00039 trm[1] = static_cast<char>((tmp >> 8) & 0xFF); // MSB de length 00040 trm[2] = static_cast<char>(tmp & 0xFF); // LSB length 00041 trm[3] = 0x10; // Type 00042 trm[4] = 0x01; // API 00043 for (int i = 0; i < 8; i++) 00044 trm[5 + i] = destination[i]; //Destination 00045 trm[13] = 0xFF; // 16 bits address 00046 trm[14] = 0xFE; // 16 bits address 00047 trm[15] = 0x00; // Radius 00048 trm[16] = 0x00; // Options 00049 00050 for (int i = 0; i < length; i++) 00051 trm[17 + i] = data[i]; // Data 00052 00053 trm[17 + length] = crc8(trm, tmp+3); 00054 00055 Serial xbee(tx, rx); 00056 for (int i = 0; i < tmp+4; i++) 00057 xbee.putc(trm[i]); 00058 00059 wait(0.01); 00060 delete trm; 00061 } 00062 00063 unsigned char Trame::crc8(const char* data, int length) 00064 { 00065 unsigned char crc = 0; 00066 for (int i = 3; i < length; i++) 00067 crc += data[i]; 00068 00069 return (0xFF - crc); 00070 }
Generated on Wed Jul 13 2022 18:11:22 by 1.7.2