BraceletUS / Mbed 2 deprecated S05APP3

Dependencies:   ConfigFile EthernetInterface mbed-rtos mbed

Fork of S05APP3 by App S5

Committer:
marc1119
Date:
Fri Feb 10 21:58:27 2017 +0000
Revision:
2:3fbf13ba290e
Parent:
1:ceb3f8ba8793
Child:
3:fbd4b164e8ad
Lecture du fichier fonctionnel,; Lecture de la trame fonctionnel,; V?rification du Checksum fonctionnel,

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marc1119 0:ec23a7ae804c 1 #include "EthernetInterface.h"
marc1119 0:ec23a7ae804c 2 #include "ConfigFile.h"
marc1119 0:ec23a7ae804c 3 #include "mbed.h"
marc1119 0:ec23a7ae804c 4
marc1119 1:ceb3f8ba8793 5 //Serial avec le Xbee
marc1119 0:ec23a7ae804c 6 DigitalOut reset(p8);
marc1119 1:ceb3f8ba8793 7 Serial uart(p13, p14); // tx, rx
marc1119 2:3fbf13ba290e 8 uint16_t frameSize = 0;
marc1119 1:ceb3f8ba8793 9
marc1119 1:ceb3f8ba8793 10 //Serial avec le PC
marc1119 1:ceb3f8ba8793 11 Serial pc(USBTX, USBRX); // tx, rx
marc1119 0:ec23a7ae804c 12
marc1119 0:ec23a7ae804c 13 //Socket Buffer
marc1119 2:3fbf13ba290e 14 TCPSocketConnection sock;
marc1119 2:3fbf13ba290e 15 uint8_t buffer[104] = { 0 };
marc1119 0:ec23a7ae804c 16 char serverAddrvalue[32];
marc1119 2:3fbf13ba290e 17 uint16_t portNo = 0;
marc1119 2:3fbf13ba290e 18 char portNbValue[4];
marc1119 2:3fbf13ba290e 19
marc1119 2:3fbf13ba290e 20 //Union créée pour mettre deux 8 bits en un 16 bits.
marc1119 2:3fbf13ba290e 21 uint16_t getLength(uint8_t value1, uint8_t value2) {
marc1119 2:3fbf13ba290e 22 union {
marc1119 2:3fbf13ba290e 23 uint16_t u16_value;
marc1119 2:3fbf13ba290e 24 uint8_t u8_value[2];
marc1119 2:3fbf13ba290e 25 } length;
marc1119 2:3fbf13ba290e 26
marc1119 2:3fbf13ba290e 27 length.u8_value[1] = value1;
marc1119 2:3fbf13ba290e 28 length.u8_value[0] = value2;
marc1119 2:3fbf13ba290e 29
marc1119 2:3fbf13ba290e 30 return length.u16_value;
marc1119 2:3fbf13ba290e 31 }
marc1119 2:3fbf13ba290e 32
marc1119 0:ec23a7ae804c 33 //Read the config file
marc1119 0:ec23a7ae804c 34 void readConfigFile()
marc1119 0:ec23a7ae804c 35 {
marc1119 0:ec23a7ae804c 36 LocalFileSystem local("local");
marc1119 0:ec23a7ae804c 37 ConfigFile cfg;
marc1119 0:ec23a7ae804c 38 char *serverAddr = "serverAddr";
marc1119 2:3fbf13ba290e 39 char *tempPortNo = "portNb";
marc1119 2:3fbf13ba290e 40 if (!cfg.read("/local/input.cfg")) error("Erreur dans la lecture du fichier\n");
marc1119 2:3fbf13ba290e 41 else
marc1119 2:3fbf13ba290e 42 {
marc1119 2:3fbf13ba290e 43 cfg.getValue(serverAddr, &serverAddrvalue[0], sizeof(serverAddrvalue));
marc1119 2:3fbf13ba290e 44 cfg.getValue(tempPortNo, &portNbValue[0], sizeof(portNbValue));
marc1119 2:3fbf13ba290e 45 portNo = (uint16_t)strtol(portNbValue, NULL, 10);
marc1119 2:3fbf13ba290e 46 pc.printf("%i", portNo);
marc1119 2:3fbf13ba290e 47 }
marc1119 0:ec23a7ae804c 48 }
marc1119 0:ec23a7ae804c 49
marc1119 0:ec23a7ae804c 50 void initSocket()
marc1119 0:ec23a7ae804c 51 {
marc1119 2:3fbf13ba290e 52
marc1119 0:ec23a7ae804c 53
marc1119 2:3fbf13ba290e 54 //int repSize;
marc1119 1:ceb3f8ba8793 55 //while (true) {
marc1119 1:ceb3f8ba8793 56
marc1119 1:ceb3f8ba8793 57 //sprintf (buffer, (const char *)uart.getc());
marc1119 1:ceb3f8ba8793 58 //sock.send_all(buffer, sizeof(buffer)-1);
marc1119 1:ceb3f8ba8793 59
marc1119 1:ceb3f8ba8793 60 //repSize = sock.receive(buffer, sizeof(buffer)-1);
marc1119 1:ceb3f8ba8793 61 // if (repSize <= 0) {
marc1119 1:ceb3f8ba8793 62 // printf("Error");
marc1119 1:ceb3f8ba8793 63 // sock.close();
marc1119 1:ceb3f8ba8793 64 // break;
marc1119 1:ceb3f8ba8793 65 // }
marc1119 1:ceb3f8ba8793 66 //buffer[repSize] = '\0';
marc1119 1:ceb3f8ba8793 67 //printf("Received %d chars from server:\n%s\n", repSize, buffer);
marc1119 1:ceb3f8ba8793 68 //}
marc1119 1:ceb3f8ba8793 69 }
marc1119 1:ceb3f8ba8793 70
marc1119 2:3fbf13ba290e 71 void verifyChecksum()
marc1119 1:ceb3f8ba8793 72 {
marc1119 2:3fbf13ba290e 73 //Je valide le checksum pour voir si tout est correct.
marc1119 2:3fbf13ba290e 74 uint32_t checkSum = 0;
marc1119 2:3fbf13ba290e 75 for(int i = 0; i < frameSize; i++) {
marc1119 2:3fbf13ba290e 76 checkSum += buffer[i];
marc1119 1:ceb3f8ba8793 77 }
marc1119 2:3fbf13ba290e 78 if((0xFF - (checkSum & 0xFF)) != buffer[frameSize])
marc1119 2:3fbf13ba290e 79 pc.printf("Erreur dans le checksum. \n");
marc1119 2:3fbf13ba290e 80 }
marc1119 2:3fbf13ba290e 81
marc1119 2:3fbf13ba290e 82 void readFrame(){
marc1119 2:3fbf13ba290e 83
marc1119 2:3fbf13ba290e 84 uint8_t temp[2] = { 0 };
marc1119 2:3fbf13ba290e 85 if(uart.readable() && uart.getc() == 0x7E) {
marc1119 2:3fbf13ba290e 86
marc1119 2:3fbf13ba290e 87 //On ramasse la taille de la trame
marc1119 2:3fbf13ba290e 88 for(int i = 0; i < 2;)
marc1119 2:3fbf13ba290e 89 {
marc1119 2:3fbf13ba290e 90 if(uart.readable()) {
marc1119 2:3fbf13ba290e 91 temp[i] = uart.getc();
marc1119 2:3fbf13ba290e 92 i++;
marc1119 2:3fbf13ba290e 93 }
marc1119 2:3fbf13ba290e 94 }
marc1119 2:3fbf13ba290e 95 frameSize = getLength(temp[0], temp[1]);
marc1119 2:3fbf13ba290e 96
marc1119 2:3fbf13ba290e 97 //Je lis le restant de la tramme et la met dans un buffer
marc1119 2:3fbf13ba290e 98 for(int j = 0; j < frameSize + 1;)
marc1119 2:3fbf13ba290e 99 {
marc1119 2:3fbf13ba290e 100 if(uart.readable()) {
marc1119 2:3fbf13ba290e 101 buffer[j] = uart.getc();
marc1119 2:3fbf13ba290e 102 j++;
marc1119 2:3fbf13ba290e 103 }
marc1119 2:3fbf13ba290e 104 }
marc1119 2:3fbf13ba290e 105 verifyChecksum();
marc1119 0:ec23a7ae804c 106 }
marc1119 0:ec23a7ae804c 107 }
marc1119 0:ec23a7ae804c 108
marc1119 0:ec23a7ae804c 109 int main() {
marc1119 2:3fbf13ba290e 110
marc1119 1:ceb3f8ba8793 111 reset = 0;
marc1119 1:ceb3f8ba8793 112 wait(0.4);
marc1119 1:ceb3f8ba8793 113 reset = 1;
marc1119 0:ec23a7ae804c 114
marc1119 2:3fbf13ba290e 115 readConfigFile();
marc1119 2:3fbf13ba290e 116
marc1119 2:3fbf13ba290e 117 //Init RJ45 and use DHCP
marc1119 2:3fbf13ba290e 118 EthernetInterface eth;
marc1119 2:3fbf13ba290e 119 if(eth.init() != 0)
marc1119 2:3fbf13ba290e 120 pc.prinf("Erreur d'initialisation du RJ45\n");
marc1119 2:3fbf13ba290e 121 if(eth.connect() != 0)
marc1119 2:3fbf13ba290e 122 pc.prinf("Erreur de connection du RJ45\n");
marc1119 2:3fbf13ba290e 123
marc1119 2:3fbf13ba290e 124 //Init Socket
marc1119 2:3fbf13ba290e 125 if(sock.connect(serverAddrvalue, portNo) != 0)
marc1119 2:3fbf13ba290e 126 pc.printf("Erreur de socket\n");
marc1119 2:3fbf13ba290e 127
marc1119 2:3fbf13ba290e 128 //sock.send_all(buffer, sizeof(buffer)-1);
marc1119 2:3fbf13ba290e 129
marc1119 2:3fbf13ba290e 130 //initSocket();
marc1119 1:ceb3f8ba8793 131 while(1) {
marc1119 2:3fbf13ba290e 132 readFrame();
marc1119 1:ceb3f8ba8793 133 }
marc1119 0:ec23a7ae804c 134 }
marc1119 0:ec23a7ae804c 135
marc1119 2:3fbf13ba290e 136