Test client

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
timmey9
Date:
Wed Mar 04 20:20:42 2015 +0000
Revision:
0:52cf7c36ef83
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timmey9 0:52cf7c36ef83 1 #include "mbed.h"
timmey9 0:52cf7c36ef83 2 #include "EthernetInterface.h"
timmey9 0:52cf7c36ef83 3
timmey9 0:52cf7c36ef83 4 #define PORT 54321
timmey9 0:52cf7c36ef83 5
timmey9 0:52cf7c36ef83 6 #define LEN_PACKET 1460
timmey9 0:52cf7c36ef83 7
timmey9 0:52cf7c36ef83 8 #define IP "169.254.225.220"
timmey9 0:52cf7c36ef83 9 #define GATEWAY "169.254.225.1"
timmey9 0:52cf7c36ef83 10 #define MASK "255.255.0.0"
timmey9 0:52cf7c36ef83 11 #define DES_ADDR "169.254.225.206"
timmey9 0:52cf7c36ef83 12 #define DES_LAPTOP "169.254.225.210"
timmey9 0:52cf7c36ef83 13
timmey9 0:52cf7c36ef83 14 #define LEN 18980
timmey9 0:52cf7c36ef83 15 uint16_t sample_array0[LEN];
timmey9 0:52cf7c36ef83 16
timmey9 0:52cf7c36ef83 17 Serial pc(USBTX,USBRX);
timmey9 0:52cf7c36ef83 18
timmey9 0:52cf7c36ef83 19 int main (void) {
timmey9 0:52cf7c36ef83 20 for(int i = 0; i < LEN; i++) sample_array0[i] = ((0x0031<<8) | 0x0032);
timmey9 0:52cf7c36ef83 21 pc.baud(230400);
timmey9 0:52cf7c36ef83 22 pc.printf("Starting test client\r\n");
timmey9 0:52cf7c36ef83 23 EthernetInterface eth;
timmey9 0:52cf7c36ef83 24 eth.init(IP, MASK, GATEWAY);
timmey9 0:52cf7c36ef83 25 eth.connect();
timmey9 0:52cf7c36ef83 26 pc.printf("IP Address is %s\r\n", eth.getIPAddress());
timmey9 0:52cf7c36ef83 27
timmey9 0:52cf7c36ef83 28 TCPSocketConnection mallet;
timmey9 0:52cf7c36ef83 29 mallet.set_blocking(false,5000);
timmey9 0:52cf7c36ef83 30 char buffer[LEN_PACKET];
timmey9 0:52cf7c36ef83 31
timmey9 0:52cf7c36ef83 32 for(int i = 0; i < LEN_PACKET; i++) buffer[i] = i;
timmey9 0:52cf7c36ef83 33 while (true) {
timmey9 0:52cf7c36ef83 34 if(pc.readable() > 0){
timmey9 0:52cf7c36ef83 35 char temp = pc.getc();
timmey9 0:52cf7c36ef83 36 int var = 0;
timmey9 0:52cf7c36ef83 37 switch(temp){
timmey9 0:52cf7c36ef83 38 case 'Q':
timmey9 0:52cf7c36ef83 39 case 'q':
timmey9 0:52cf7c36ef83 40 var = mallet.connect(DES_LAPTOP,5005);
timmey9 0:52cf7c36ef83 41 if(var == 0) pc.printf("Connected to laptop successfully\r\n");
timmey9 0:52cf7c36ef83 42 else if(var == -1) pc.printf("Connecting failed\r\n");
timmey9 0:52cf7c36ef83 43 break;
timmey9 0:52cf7c36ef83 44 case 'A':
timmey9 0:52cf7c36ef83 45 case 'a':
timmey9 0:52cf7c36ef83 46 var = mallet.connect(DES_ADDR,PORT);
timmey9 0:52cf7c36ef83 47 if(var == 0) pc.printf("Connected to FRDM server successfully\r\n");
timmey9 0:52cf7c36ef83 48 else if(var == -1) pc.printf("Connecting failed\r\n");
timmey9 0:52cf7c36ef83 49 break;
timmey9 0:52cf7c36ef83 50 case 'S':
timmey9 0:52cf7c36ef83 51 case 's':
timmey9 0:52cf7c36ef83 52 var = mallet.send(buffer,LEN_PACKET);
timmey9 0:52cf7c36ef83 53 pc.printf("Sent %i bytes\r\n",var);
timmey9 0:52cf7c36ef83 54 break;
timmey9 0:52cf7c36ef83 55 case 'D':
timmey9 0:52cf7c36ef83 56 case 'd':
timmey9 0:52cf7c36ef83 57 var = mallet.receive_all((char *)sample_array0,LEN*2);
timmey9 0:52cf7c36ef83 58 pc.printf("Received %i bytes\r\n",var);
timmey9 0:52cf7c36ef83 59 break;
timmey9 0:52cf7c36ef83 60 case 'F':
timmey9 0:52cf7c36ef83 61 case 'f':
timmey9 0:52cf7c36ef83 62 var = mallet.close();
timmey9 0:52cf7c36ef83 63 if(var == 0) pc.printf("Connection closed\r\n");
timmey9 0:52cf7c36ef83 64 else pc.printf("Error while closing connection\r\n");
timmey9 0:52cf7c36ef83 65 break;
timmey9 0:52cf7c36ef83 66 case 'Z':
timmey9 0:52cf7c36ef83 67 case 'z':
timmey9 0:52cf7c36ef83 68 pc.printf("Status: ");
timmey9 0:52cf7c36ef83 69 if(mallet.is_connected()) pc.printf("Connected\r\n");
timmey9 0:52cf7c36ef83 70 else pc.printf("Not connected\r\n");
timmey9 0:52cf7c36ef83 71 break;
timmey9 0:52cf7c36ef83 72 case 'R':
timmey9 0:52cf7c36ef83 73 case 'r':
timmey9 0:52cf7c36ef83 74 // open connection
timmey9 0:52cf7c36ef83 75 var = mallet.connect(DES_ADDR,PORT);
timmey9 0:52cf7c36ef83 76 if(var == 0) pc.printf("Connected successfully\r\n");
timmey9 0:52cf7c36ef83 77 else if(var == -1) pc.printf("Connection already open\r\n");
timmey9 0:52cf7c36ef83 78
timmey9 0:52cf7c36ef83 79 // send data
timmey9 0:52cf7c36ef83 80 var = mallet.send(buffer,LEN_PACKET);
timmey9 0:52cf7c36ef83 81 pc.printf("Sent %i bytes\r\n",var);
timmey9 0:52cf7c36ef83 82
timmey9 0:52cf7c36ef83 83 // receive data
timmey9 0:52cf7c36ef83 84 var = mallet.receive_all((char *)sample_array0,LEN*2);
timmey9 0:52cf7c36ef83 85 pc.printf("Received %i bytes\r\n",var);
timmey9 0:52cf7c36ef83 86
timmey9 0:52cf7c36ef83 87 // closed connection
timmey9 0:52cf7c36ef83 88 var = mallet.close();
timmey9 0:52cf7c36ef83 89 if(var == 0) pc.printf("Connection closed\r\n\n");
timmey9 0:52cf7c36ef83 90 else pc.printf("Error while closing connection\r\n\n");
timmey9 0:52cf7c36ef83 91 break;
timmey9 0:52cf7c36ef83 92 case 'L':
timmey9 0:52cf7c36ef83 93 case 'l':
timmey9 0:52cf7c36ef83 94 for(int i = 0; i < 1000; i++){
timmey9 0:52cf7c36ef83 95 //wait_ms(100);
timmey9 0:52cf7c36ef83 96 pc.printf("%3i: ",i);
timmey9 0:52cf7c36ef83 97
timmey9 0:52cf7c36ef83 98 // open connection
timmey9 0:52cf7c36ef83 99 var = mallet.connect(DES_ADDR,PORT);
timmey9 0:52cf7c36ef83 100 if(var == -1) {i = 50; break;}
timmey9 0:52cf7c36ef83 101
timmey9 0:52cf7c36ef83 102 // send data
timmey9 0:52cf7c36ef83 103 var = mallet.send(buffer,LEN_PACKET);
timmey9 0:52cf7c36ef83 104 pc.printf("Sent %i: ",var);
timmey9 0:52cf7c36ef83 105 if(var == -1) {i = 50; break;}
timmey9 0:52cf7c36ef83 106
timmey9 0:52cf7c36ef83 107 // receive data
timmey9 0:52cf7c36ef83 108 var = mallet.receive_all((char *)sample_array0,LEN*2);
timmey9 0:52cf7c36ef83 109 pc.printf("Rec %i: ",var);
timmey9 0:52cf7c36ef83 110 if(var == -1) {i = 50; break;}
timmey9 0:52cf7c36ef83 111
timmey9 0:52cf7c36ef83 112 // closed connection
timmey9 0:52cf7c36ef83 113 var = mallet.close();
timmey9 0:52cf7c36ef83 114 if(var == -1) {i = 50; break;}
timmey9 0:52cf7c36ef83 115 pc.printf("\r\n");
timmey9 0:52cf7c36ef83 116
timmey9 0:52cf7c36ef83 117 }
timmey9 0:52cf7c36ef83 118 break;
timmey9 0:52cf7c36ef83 119 }
timmey9 0:52cf7c36ef83 120 }
timmey9 0:52cf7c36ef83 121 }
timmey9 0:52cf7c36ef83 122 }