Test Server
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@0:762cc0d8a4fa, 2015-03-04 (annotated)
- Committer:
- timmey9
- Date:
- Wed Mar 04 20:21:43 2015 +0000
- Revision:
- 0:762cc0d8a4fa
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timmey9 | 0:762cc0d8a4fa | 1 | #include "mbed.h" |
timmey9 | 0:762cc0d8a4fa | 2 | #include "EthernetInterface.h" |
timmey9 | 0:762cc0d8a4fa | 3 | |
timmey9 | 0:762cc0d8a4fa | 4 | #define ECHO_SERVER_PORT 54321 |
timmey9 | 0:762cc0d8a4fa | 5 | |
timmey9 | 0:762cc0d8a4fa | 6 | #define IP "169.254.225.206" |
timmey9 | 0:762cc0d8a4fa | 7 | #define GATEWAY "169.254.225.1" |
timmey9 | 0:762cc0d8a4fa | 8 | #define MASK "255.255.0.0" |
timmey9 | 0:762cc0d8a4fa | 9 | |
timmey9 | 0:762cc0d8a4fa | 10 | #define LEN 18980 |
timmey9 | 0:762cc0d8a4fa | 11 | uint16_t sample_array0[LEN]; |
timmey9 | 0:762cc0d8a4fa | 12 | |
timmey9 | 0:762cc0d8a4fa | 13 | DigitalOut led_red(LED_RED); |
timmey9 | 0:762cc0d8a4fa | 14 | DigitalOut led_green(LED_GREEN); |
timmey9 | 0:762cc0d8a4fa | 15 | DigitalOut led_blue(LED_BLUE); |
timmey9 | 0:762cc0d8a4fa | 16 | |
timmey9 | 0:762cc0d8a4fa | 17 | Serial pc(USBTX,USBRX); |
timmey9 | 0:762cc0d8a4fa | 18 | |
timmey9 | 0:762cc0d8a4fa | 19 | int main (void) { |
timmey9 | 0:762cc0d8a4fa | 20 | for(int i = 0; i < LEN; i++) sample_array0[i] = ((0x0031<<8) | 0x0032); |
timmey9 | 0:762cc0d8a4fa | 21 | sample_array0[0] = ('S'<<8)|'S'; |
timmey9 | 0:762cc0d8a4fa | 22 | sample_array0[LEN-1] = ('F'<<8) | 'F'; |
timmey9 | 0:762cc0d8a4fa | 23 | pc.baud(230400); |
timmey9 | 0:762cc0d8a4fa | 24 | pc.printf("Starting test server\r\n"); |
timmey9 | 0:762cc0d8a4fa | 25 | |
timmey9 | 0:762cc0d8a4fa | 26 | EthernetInterface eth; |
timmey9 | 0:762cc0d8a4fa | 27 | eth.init(IP, MASK, GATEWAY); |
timmey9 | 0:762cc0d8a4fa | 28 | eth.connect(); |
timmey9 | 0:762cc0d8a4fa | 29 | pc.printf("IP Address is %s\r\n", eth.getIPAddress()); |
timmey9 | 0:762cc0d8a4fa | 30 | |
timmey9 | 0:762cc0d8a4fa | 31 | TCPSocketServer server; |
timmey9 | 0:762cc0d8a4fa | 32 | server.bind(ECHO_SERVER_PORT); |
timmey9 | 0:762cc0d8a4fa | 33 | server.listen(); |
timmey9 | 0:762cc0d8a4fa | 34 | |
timmey9 | 0:762cc0d8a4fa | 35 | led_blue = 1; |
timmey9 | 0:762cc0d8a4fa | 36 | led_green = 1; |
timmey9 | 0:762cc0d8a4fa | 37 | led_red = 1; |
timmey9 | 0:762cc0d8a4fa | 38 | |
timmey9 | 0:762cc0d8a4fa | 39 | while (true) { |
timmey9 | 0:762cc0d8a4fa | 40 | //pc.printf("\r\nWait for new connection...\r\n"); |
timmey9 | 0:762cc0d8a4fa | 41 | |
timmey9 | 0:762cc0d8a4fa | 42 | |
timmey9 | 0:762cc0d8a4fa | 43 | TCPSocketConnection client; |
timmey9 | 0:762cc0d8a4fa | 44 | server.accept(client); |
timmey9 | 0:762cc0d8a4fa | 45 | client.set_blocking(true, 5000); |
timmey9 | 0:762cc0d8a4fa | 46 | |
timmey9 | 0:762cc0d8a4fa | 47 | //pc.printf("Connection from: %s\r\n", client.get_address()); |
timmey9 | 0:762cc0d8a4fa | 48 | char buffer[1460]; // size of IP payload |
timmey9 | 0:762cc0d8a4fa | 49 | while (true) { |
timmey9 | 0:762cc0d8a4fa | 50 | int n = client.receive(buffer, sizeof(buffer)); |
timmey9 | 0:762cc0d8a4fa | 51 | if (n <= 0) break; |
timmey9 | 0:762cc0d8a4fa | 52 | pc.printf("Rec: %i: ",n); |
timmey9 | 0:762cc0d8a4fa | 53 | |
timmey9 | 0:762cc0d8a4fa | 54 | n = client.send_all((char *)sample_array0, LEN*2); |
timmey9 | 0:762cc0d8a4fa | 55 | |
timmey9 | 0:762cc0d8a4fa | 56 | if (n <= 0) {led_blue = 0; break;} |
timmey9 | 0:762cc0d8a4fa | 57 | pc.printf("Sent %i: ",n); |
timmey9 | 0:762cc0d8a4fa | 58 | } |
timmey9 | 0:762cc0d8a4fa | 59 | |
timmey9 | 0:762cc0d8a4fa | 60 | client.close(); |
timmey9 | 0:762cc0d8a4fa | 61 | pc.printf("\r\n"); |
timmey9 | 0:762cc0d8a4fa | 62 | } |
timmey9 | 0:762cc0d8a4fa | 63 | } |