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:
- 6:fd7d91edcf60
- Parent:
- 4:393738672d08
- Child:
- 7:ea6d3c6ac200
--- a/main.cpp Sun Feb 12 00:04:54 2017 +0000 +++ b/main.cpp Sun Feb 12 02:27:01 2017 +0000 @@ -1,3 +1,4 @@ +// COORDINATOR #include "EthernetInterface.h" #include "ConfigFile.h" #include "mbed.h" @@ -6,12 +7,46 @@ Serial xbee(p13, p14); // tx, rx Serial pc(USBTX, USBRX); // tx, rx -// Socket Buffer -uint16_t portNo = 0; -char serverAddrvalue[32]; -char portNbValue[4]; +uint16_t portNumber = 0; +uint16_t panId = 0; +char serverAddress[32]; +char portNbr[4]; +char panIdChar[5]; bool boolLED = false; +void printBuffer(uint8_t bufferSize, uint8_t* buffer) { + for(int k = 0; k < bufferSize; k++) { + pc.printf("%X-", buffer[k]); + } + printf("\n"); +} + +/* +* Read a configuration file from a mbed. +*/ +void readConfigFile() { + LocalFileSystem local("local"); + ConfigFile cfg; + char *serverAddressStr = "serverAddr"; + char *portNumberStr = "portNb"; + char *panIdStr = "panID"; + + if (!cfg.read("/local/input.cfg")) { + error("Erreur dans la lecture du fichier de configuration.\n"); + } else { + cfg.getValue(panIdStr, &panIdChar[0], sizeof(panIdChar)); + cfg.getValue(serverAddressStr, &serverAddress[0], sizeof(serverAddress)); + cfg.getValue(portNumberStr, &portNbr[0], sizeof(portNbr)); + + portNumber = (uint16_t)strtol(portNbr, NULL, 10); + panId = (uint16_t)strtol(panIdChar, NULL, 10); + + printf("The server address is %s\n", serverAddress); + printf("The port number is %i\n", portNumber); + printf("The PAN ID is %i\n", panId); + } +} + uint16_t getFrameSize(uint8_t value1, uint8_t value2) { union { uint16_t u16_value; @@ -50,21 +85,6 @@ 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) { @@ -108,20 +128,19 @@ } void readFrame(){ - uint8_t temp[2] = { 0 }; + uint8_t sizeBytes[2] = { 0 }; uint8_t buffer[104] = { 0 }; + while (xbee.readable() == 0){} // Wait for UART to have new data + pc.printf("new char"); + if (xbee.readable() && xbee.getc() == 0x7E) { for (int i = 0; i < 2;) { - if (xbee.readable()) { - temp[i] = xbee.getc(); - i++; - } + sizeBytes[i] = xbee.getc(); + i++; } - uint16_t frameSize = getFrameSize(temp[0], temp[1]); - pc.printf("La grandeur de la frame: %d\n", frameSize); + uint16_t frameSize = getFrameSize(sizeBytes[0], sizeBytes[1]); - // 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(); @@ -130,30 +149,37 @@ } verifyChecksum(frameSize, buffer); - pc.printf("Frame: %s\n", buffer); + printBuffer(frameSize, buffer); } } int main() { + pc.printf("Starting a coordinator... \n"); + EthernetInterface eth; TCPSocketConnection sock; reset = 0; - wait(0.4); + wait_ms(1); reset = 1; + wait_ms(1); readConfigFile(); + xbee.baud(9600); // Set baud rate + xbee.printf("ATID %i\r",panId); // Set the 64-bit PAN ID + xbee.printf("ATWR \r"); // + xbee.printf("ATCN \r"); + 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) { + if (sock.connect(serverAddress, portNumber) != 0) { pc.printf("Erreur de socket.\n"); } - //sock.send_all(buffer, sizeof(buffer)-1); //initSocket(); @@ -161,8 +187,7 @@ //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);