Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ConfigFile EthernetInterface mbed-rtos mbed
Fork of S05APP3 by
Diff: main.cpp
- Revision:
- 4:393738672d08
- Parent:
- 3:fbd4b164e8ad
- Child:
- 6:fd7d91edcf60
--- a/main.cpp Sat Feb 11 01:54:57 2017 +0000 +++ b/main.cpp Sun Feb 12 00:01:37 2017 +0000 @@ -2,25 +2,17 @@ #include "ConfigFile.h" #include "mbed.h" -//Serial avec le Xbee DigitalOut reset(p8); -Serial uart(p13, p14); // tx, rx -uint16_t frameSize = 0; - -//Serial avec le PC +Serial xbee(p13, p14); // tx, rx Serial pc(USBTX, USBRX); // tx, rx -//Socket Buffer -TCPSocketConnection sock; -uint8_t buffer[104] = { 0 }; +// Socket Buffer +uint16_t portNo = 0; char serverAddrvalue[32]; -uint16_t portNo = 0; char portNbValue[4]; - bool boolLED = false; -//Union créée pour mettre deux 8 bits en un 16 bits. -uint16_t getLength(uint8_t value1, uint8_t value2) { +uint16_t getFrameSize(uint8_t value1, uint8_t value2) { union { uint16_t u16_value; uint8_t u8_value[2]; @@ -33,49 +25,51 @@ } void toogleLED() { - - //Numéro 64 bit = 00 13 A2 00 40 8B 41 6E + // Addresse 64 bits = 00 13 A2 00 40 8B 41 6E //uint8_t high[20] = {0x7E, 0x00, 0x10, 0x17, 0x00, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3D}; //uint8_t low[20] = {0x7E, 0x00, 0x10, 0x17, 0x00, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3E}; uint8_t high[20] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3C}; uint8_t low[20] = {0x7E, 0x00, 0x10, 0x17, 0x02, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3C}; - for(int i = 0; i < 20;) { - if(uart.writeable()) { - if(boolLED) uart.putc(high[i]); - else uart.putc(low[i]); + for (int i = 0; i < 20;) { + if (xbee.writeable()) { + if (boolLED) { + xbee.putc(high[i]); + } else { + xbee.putc(low[i]); + } i++; } } - if(boolLED) pc.printf("Et un haut!\n"); - else pc.printf("Et un bas!\n"); - + if (boolLED) { + pc.printf("Et un haut!\n"); + } else { + pc.printf("Et un bas!\n"); + } boolLED = !boolLED; } -//Read the config file -void readConfigFile() -{ +void readConfigFile() { LocalFileSystem local("local"); ConfigFile cfg; char *serverAddr = "serverAddr"; char *tempPortNo = "portNb"; - if (!cfg.read("/local/input.cfg")) error("Erreur dans la lecture du fichier\n"); - else - { + + if (!cfg.read("/local/input.cfg")) { + error("Erreur dans la lecture du fichier\n"); + } else { cfg.getValue(serverAddr, &serverAddrvalue[0], sizeof(serverAddrvalue)); cfg.getValue(tempPortNo, &portNbValue[0], sizeof(portNbValue)); portNo = (uint16_t)strtol(portNbValue, NULL, 10); } } -void initSocket() -{ +void initSocket() { //int repSize; //while (true) { - //sprintf (buffer, (const char *)uart.getc()); + //sprintf (buffer, (const char *)xbee.getc()); //sock.send_all(buffer, sizeof(buffer)-1); //repSize = sock.receive(buffer, sizeof(buffer)-1); @@ -89,63 +83,60 @@ //} } -void checkForError() -{ - if(buffer[0] == 0x97) { - if(buffer[1] == 0x02) { +void checkForError(uint8_t* buffer) { + if (buffer[0] == 0x97) { + if (buffer[1] == 0x02) { pc.printf("Erreur dans la remote AT command pour mettre la DEL a l'etat bas.\n"); } - else if(buffer[1] == 0x01) { + else if (buffer[1] == 0x01) { pc.printf("Erreur dans la remote AT command pour mettre la DEL a l'etat haut\n"); } } } -void verifyChecksum() -{ - //Je valide le checksum pour voir si tout est correct. +void verifyChecksum(uint16_t frameSize, uint8_t* buffer) { uint32_t checkSum = 0; - for(int i = 0; i < frameSize; i++) { + + for (int i = 0; i < frameSize; i++) { checkSum += buffer[i]; - } - if((0xFF - (checkSum & 0xFF)) != buffer[frameSize]) + } + if ((0xFF - (checkSum & 0xFF)) != buffer[frameSize]) { pc.printf("Erreur dans le checksum. \n"); + } - checkForError(); + checkForError(buffer); } void readFrame(){ + uint8_t temp[2] = { 0 }; + uint8_t buffer[104] = { 0 }; - uint8_t temp[2] = { 0 }; - if(uart.readable() && uart.getc() == 0x7E) { - - //On ramasse la taille de la trame - for(int i = 0; i < 2;) - { - if(uart.readable()) { - temp[i] = uart.getc(); + if (xbee.readable() && xbee.getc() == 0x7E) { + for (int i = 0; i < 2;) { + if (xbee.readable()) { + temp[i] = xbee.getc(); i++; } } - frameSize = getLength(temp[0], temp[1]); + uint16_t frameSize = getFrameSize(temp[0], temp[1]); + pc.printf("La grandeur de la frame: %d\n", frameSize); - //Je lis le restant de la tramme et la met dans un buffer - for(int j = 0; j < frameSize + 1;) - { - if(uart.readable()) { - buffer[j] = uart.getc(); + // Je lis le restant de la trame et la met dans un buffer + for (int j = 0; j <= frameSize;) { + if (xbee.readable()) { + buffer[j] = xbee.getc(); j++; } } - verifyChecksum(); - //Debug seulement - for(int k = 0; k < frameSize; k++) - pc.printf("%X-", buffer[k]); - pc.printf("\n---------------\n"); + + verifyChecksum(frameSize, buffer); + pc.printf("Frame: %s\n", buffer); } } int main() { + EthernetInterface eth; + TCPSocketConnection sock; reset = 0; wait(0.4); @@ -153,16 +144,15 @@ readConfigFile(); - //Init RJ45 and use DHCP - EthernetInterface eth; - if(eth.init() != 0) + if (eth.init() != 0) { // Use DHCP pc.printf("Erreur d'initialisation du RJ45.\n"); - if(eth.connect() != 0) + } + if (eth.connect() != 0) { pc.printf("Erreur de connection du RJ45\n"); - - //Init Socket - if(sock.connect(serverAddrvalue, portNo) != 0) + } + if (sock.connect(serverAddrvalue, portNo) != 0) { pc.printf("Erreur de socket.\n"); + } //sock.send_all(buffer, sizeof(buffer)-1); @@ -172,11 +162,11 @@ //Ticker blinkLED; // blinkLED.attach(&toogleLED, 1); - pc.printf("Pret a recevoir des trames! \n"); + pc.printf("Pret a recevoir des trames... \n"); while(1) { readFrame(); - wait_ms(1000); - toogleLED(); + //wait_ms(1000); + //toogleLED(); } }