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:
- benjaminroy
- Date:
- 2017-02-12
- Revision:
- 4:393738672d08
- Parent:
- 3:fbd4b164e8ad
- Child:
- 6:fd7d91edcf60
File content as of revision 4:393738672d08:
#include "EthernetInterface.h" #include "ConfigFile.h" #include "mbed.h" DigitalOut reset(p8); Serial xbee(p13, p14); // tx, rx Serial pc(USBTX, USBRX); // tx, rx // Socket Buffer uint16_t portNo = 0; char serverAddrvalue[32]; char portNbValue[4]; bool boolLED = false; uint16_t getFrameSize(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() { // 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 (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"); } boolLED = !boolLED; } 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 *)xbee.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(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) { pc.printf("Erreur dans la remote AT command pour mettre la DEL a l'etat haut\n"); } } } void verifyChecksum(uint16_t frameSize, uint8_t* buffer) { 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(buffer); } void readFrame(){ uint8_t temp[2] = { 0 }; uint8_t buffer[104] = { 0 }; if (xbee.readable() && xbee.getc() == 0x7E) { for (int i = 0; i < 2;) { if (xbee.readable()) { temp[i] = xbee.getc(); i++; } } uint16_t frameSize = getFrameSize(temp[0], temp[1]); pc.printf("La grandeur de la frame: %d\n", frameSize); // 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(frameSize, buffer); pc.printf("Frame: %s\n", buffer); } } int main() { EthernetInterface eth; TCPSocketConnection sock; reset = 0; wait(0.4); reset = 1; readConfigFile(); if (eth.init() != 0) { // Use DHCP pc.printf("Erreur d'initialisation du RJ45.\n"); } if (eth.connect() != 0) { pc.printf("Erreur de connection du RJ45\n"); } 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(); } }