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@1:ceb3f8ba8793, 2017-02-10 (annotated)
- Committer:
- marc1119
- Date:
- Fri Feb 10 19:25:34 2017 +0000
- Revision:
- 1:ceb3f8ba8793
- Parent:
- 0:ec23a7ae804c
- Child:
- 2:3fbf13ba290e
Premier Commit
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:ceb3f8ba8793 | 8 | uint8_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 | 1:ceb3f8ba8793 | 14 | uint8_t buffer[104] = {0}; |
marc1119 | 0:ec23a7ae804c | 15 | char serverAddrvalue[32]; |
marc1119 | 1:ceb3f8ba8793 | 16 | TCPSocketConnection sock; |
marc1119 | 0:ec23a7ae804c | 17 | //Read the config file |
marc1119 | 0:ec23a7ae804c | 18 | void readConfigFile() |
marc1119 | 0:ec23a7ae804c | 19 | { |
marc1119 | 0:ec23a7ae804c | 20 | LocalFileSystem local("local"); |
marc1119 | 0:ec23a7ae804c | 21 | ConfigFile cfg; |
marc1119 | 0:ec23a7ae804c | 22 | char *serverAddr = "serverAddr"; |
marc1119 | 0:ec23a7ae804c | 23 | if (!cfg.read("/local/input.cfg")) error("Failure to read a configuration file.\n"); |
marc1119 | 0:ec23a7ae804c | 24 | else cfg.getValue(serverAddr, &serverAddrvalue[0], sizeof(serverAddrvalue)); |
marc1119 | 0:ec23a7ae804c | 25 | } |
marc1119 | 0:ec23a7ae804c | 26 | |
marc1119 | 0:ec23a7ae804c | 27 | //Init RJ45 and use DHCP |
marc1119 | 0:ec23a7ae804c | 28 | void initRJ45() |
marc1119 | 0:ec23a7ae804c | 29 | { |
marc1119 | 1:ceb3f8ba8793 | 30 | EthernetInterface eth; |
marc1119 | 1:ceb3f8ba8793 | 31 | eth.init(); |
marc1119 | 1:ceb3f8ba8793 | 32 | eth.connect(); |
marc1119 | 0:ec23a7ae804c | 33 | } |
marc1119 | 0:ec23a7ae804c | 34 | |
marc1119 | 0:ec23a7ae804c | 35 | void initSocket() |
marc1119 | 0:ec23a7ae804c | 36 | { |
marc1119 | 0:ec23a7ae804c | 37 | //Init Socket |
marc1119 | 1:ceb3f8ba8793 | 38 | //sock.connect(serverAddrvalue, 502); |
marc1119 | 1:ceb3f8ba8793 | 39 | sock.connect("192.168.0.103", 502); |
marc1119 | 0:ec23a7ae804c | 40 | sock.send_all(buffer, sizeof(buffer)-1); |
marc1119 | 0:ec23a7ae804c | 41 | |
marc1119 | 0:ec23a7ae804c | 42 | int repSize; |
marc1119 | 1:ceb3f8ba8793 | 43 | //while (true) { |
marc1119 | 1:ceb3f8ba8793 | 44 | |
marc1119 | 1:ceb3f8ba8793 | 45 | //sprintf (buffer, (const char *)uart.getc()); |
marc1119 | 1:ceb3f8ba8793 | 46 | //sock.send_all(buffer, sizeof(buffer)-1); |
marc1119 | 1:ceb3f8ba8793 | 47 | |
marc1119 | 1:ceb3f8ba8793 | 48 | //repSize = sock.receive(buffer, sizeof(buffer)-1); |
marc1119 | 1:ceb3f8ba8793 | 49 | // if (repSize <= 0) { |
marc1119 | 1:ceb3f8ba8793 | 50 | // printf("Error"); |
marc1119 | 1:ceb3f8ba8793 | 51 | // sock.close(); |
marc1119 | 1:ceb3f8ba8793 | 52 | // break; |
marc1119 | 1:ceb3f8ba8793 | 53 | // } |
marc1119 | 1:ceb3f8ba8793 | 54 | //buffer[repSize] = '\0'; |
marc1119 | 1:ceb3f8ba8793 | 55 | //printf("Received %d chars from server:\n%s\n", repSize, buffer); |
marc1119 | 1:ceb3f8ba8793 | 56 | //} |
marc1119 | 1:ceb3f8ba8793 | 57 | } |
marc1119 | 1:ceb3f8ba8793 | 58 | |
marc1119 | 1:ceb3f8ba8793 | 59 | void checkChecksum() |
marc1119 | 1:ceb3f8ba8793 | 60 | { |
marc1119 | 1:ceb3f8ba8793 | 61 | //On additionne tout les bytes sauf le API Frame et le Length et on fait FF - résultat = checksum |
marc1119 | 1:ceb3f8ba8793 | 62 | for(int i = 2; i < frameSize; i++) |
marc1119 | 1:ceb3f8ba8793 | 63 | { |
marc1119 | 1:ceb3f8ba8793 | 64 | int checkSum += buffer[i] |
marc1119 | 1:ceb3f8ba8793 | 65 | } |
marc1119 | 1:ceb3f8ba8793 | 66 | if((0xFF- checkSum) == buffer[frameSize]) |
marc1119 | 1:ceb3f8ba8793 | 67 | { |
marc1119 | 1:ceb3f8ba8793 | 68 | //CheckSum ok |
marc1119 | 1:ceb3f8ba8793 | 69 | } |
marc1119 | 1:ceb3f8ba8793 | 70 | else |
marc1119 | 1:ceb3f8ba8793 | 71 | { |
marc1119 | 1:ceb3f8ba8793 | 72 | //Error |
marc1119 | 0:ec23a7ae804c | 73 | } |
marc1119 | 0:ec23a7ae804c | 74 | } |
marc1119 | 0:ec23a7ae804c | 75 | |
marc1119 | 0:ec23a7ae804c | 76 | int main() { |
marc1119 | 1:ceb3f8ba8793 | 77 | reset = 0; |
marc1119 | 1:ceb3f8ba8793 | 78 | wait(0.4); |
marc1119 | 1:ceb3f8ba8793 | 79 | reset = 1; |
marc1119 | 0:ec23a7ae804c | 80 | |
marc1119 | 1:ceb3f8ba8793 | 81 | //readConfigFile(); |
marc1119 | 0:ec23a7ae804c | 82 | initRJ45(); |
marc1119 | 0:ec23a7ae804c | 83 | initSocket(); |
marc1119 | 0:ec23a7ae804c | 84 | |
marc1119 | 1:ceb3f8ba8793 | 85 | while(1) { |
marc1119 | 1:ceb3f8ba8793 | 86 | if(uart.readable()) { |
marc1119 | 1:ceb3f8ba8793 | 87 | pc.printf("%i", uart.getc()); |
marc1119 | 1:ceb3f8ba8793 | 88 | } |
marc1119 | 1:ceb3f8ba8793 | 89 | } |
marc1119 | 0:ec23a7ae804c | 90 | } |
marc1119 | 0:ec23a7ae804c | 91 |