Thomas Murphy / Mbed 2 deprecated udp_testing_echo_data

Dependencies:   EthernetInterface FXOS8700CQ mbed-rtos mbed

Fork of UDPEchoClient by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "FXOS8700CQ.h"
00004 
00005 const char* ECHO_SERVER_ADDRESS = "10.0.0.73";
00006 const int ECHO_SERVER_PORT = 7001; // changed from example code
00007 
00008 // TODO: figure out why this line breaks the code:
00009 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
00010 
00011 RawSerial pc(USBTX, USBRX);
00012 
00013 Timer t; // Microsecond timer, 32 bit int, maximum count of ~30 minutes
00014 
00015 int main()
00016 {
00017     pc.baud(115200);
00018     
00019     printf("Starting setup.\n");
00020 
00021     EthernetInterface eth;
00022     eth.init(); // Use DHCP
00023     eth.connect();
00024     printf("IP Address is %s\n", eth.getIPAddress());
00025 
00026     UDPSocket sock;
00027     sock.init();
00028 
00029     Endpoint echo_server;
00030     echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
00031 
00032     char out_buffer[] = "Hello World";
00033     char in_buffer[256];
00034 
00035     t.reset();
00036     t.start();
00037 
00038     for(int i = 0; i < 20; ++i) {
00039         sock.sendTo(echo_server, out_buffer, sizeof(out_buffer));
00040 
00041         int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
00042 
00043         in_buffer[n] = '\0';
00044         printf("%d, %d: %s\n", t.read_us(), i, in_buffer);
00045         
00046         t.reset();
00047     }
00048     
00049     sock.close();
00050     eth.disconnect();
00051     
00052     printf("Disconnected and closed.");
00053     
00054     while(1) {}
00055 }