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
main.cpp
- Committer:
- marc1119
- Date:
- 2017-02-11
- Revision:
- 3:fbd4b164e8ad
- Parent:
- 2:3fbf13ba290e
- Child:
- 4:393738672d08
File content as of revision 3:fbd4b164e8ad:
#include "EthernetInterface.h" #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 pc(USBTX, USBRX); // tx, rx //Socket Buffer TCPSocketConnection sock; uint8_t buffer[104] = { 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) { union { uint16_t u16_value; uint8_t u8_value[2]; } length; length.u8_value[1] = value1; length.u8_value[0] = value2; return length.u16_value; } void toogleLED() { //Numéro 64 bit = 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]); i++; } } if(boolLED) pc.printf("Et un haut!\n"); else pc.printf("Et un bas!\n"); boolLED = !boolLED; } //Read the config file 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 { cfg.getValue(serverAddr, &serverAddrvalue[0], sizeof(serverAddrvalue)); cfg.getValue(tempPortNo, &portNbValue[0], sizeof(portNbValue)); portNo = (uint16_t)strtol(portNbValue, NULL, 10); } } void initSocket() { //int repSize; //while (true) { //sprintf (buffer, (const char *)uart.getc()); //sock.send_all(buffer, sizeof(buffer)-1); //repSize = sock.receive(buffer, sizeof(buffer)-1); // if (repSize <= 0) { // printf("Error"); // sock.close(); // break; // } //buffer[repSize] = '\0'; //printf("Received %d chars from server:\n%s\n", repSize, buffer); //} } void checkForError() { 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) { 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. uint32_t checkSum = 0; for(int i = 0; i < frameSize; i++) { checkSum += buffer[i]; } if((0xFF - (checkSum & 0xFF)) != buffer[frameSize]) pc.printf("Erreur dans le checksum. \n"); checkForError(); } void readFrame(){ 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(); i++; } } frameSize = getLength(temp[0], temp[1]); //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(); j++; } } verifyChecksum(); //Debug seulement for(int k = 0; k < frameSize; k++) pc.printf("%X-", buffer[k]); pc.printf("\n---------------\n"); } } int main() { reset = 0; wait(0.4); reset = 1; readConfigFile(); //Init RJ45 and use DHCP EthernetInterface eth; if(eth.init() != 0) pc.printf("Erreur d'initialisation du RJ45.\n"); if(eth.connect() != 0) pc.printf("Erreur de connection du RJ45\n"); //Init Socket if(sock.connect(serverAddrvalue, portNo) != 0) pc.printf("Erreur de socket.\n"); //sock.send_all(buffer, sizeof(buffer)-1); //initSocket(); //Set ticker to blink LED at each 1 sec //Ticker blinkLED; // blinkLED.attach(&toogleLED, 1); pc.printf("Pret a recevoir des trames! \n"); while(1) { readFrame(); wait_ms(1000); toogleLED(); } }