TASS Belgium / Mbed 2 deprecated UDP_Receive_Test

Dependencies:   PicoTCP lpc1768-picotcp-eth mbed-rtos mbed

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 
00004 #define MEGA               (1024*1024)
00005 #define BUFFER_SIZE        1024
00006 #define NUMBER_OF_SECONDS (20*1000u)  // 20 seconds
00007 
00008 #define REMOTE_IP       "192.168.100.2"
00009 #define REMOTE_PORT      2327
00010 #define NUMBER_OF_FRAMES 1000
00011 
00012 struct UDPStat
00013 {
00014     int BytesReceived;
00015     int FramesReceived;
00016     int LostPackets;
00017 };
00018 
00019 struct UDPStat UDP_Statistics;
00020 
00021 int main() {
00022 
00023     EthernetInterface eth;
00024     eth.init();
00025     eth.connect();
00026     
00027     printf("Started UDP Fast Test...\n");
00028     
00029     UDPSocket server;
00030     Endpoint endp;
00031     
00032     printf("Remote endpoint @ %s:%d\n",REMOTE_IP,REMOTE_PORT);
00033     endp.set_address(REMOTE_IP,REMOTE_PORT);
00034     
00035     printf("Binding result :%d\n", server.bind(REMOTE_PORT));
00036     
00037     server.set_blocking(false,1000);
00038     char buffer[BUFFER_SIZE];
00039     
00040     while(true)
00041     {
00042         unsigned int time = PICO_TIME_MS();
00043         int index = -1;
00044         
00045         memset(&UDP_Statistics,0x0,sizeof(struct UDPStat));
00046         while( (time + NUMBER_OF_SECONDS) >= (unsigned int)PICO_TIME_MS())
00047         {
00048             int size;
00049             size = server.receiveFrom(endp,buffer,sizeof(buffer));
00050             if(size > 0)
00051             {
00052                 int _index;
00053                 UDP_Statistics.BytesReceived += size;
00054                 UDP_Statistics.FramesReceived++;
00055                 _index = *((int *)buffer);
00056                 
00057                 if(index>0)
00058                 {
00059                     UDP_Statistics.LostPackets+= _index-index-1;
00060                 }
00061                 index = _index;
00062             }
00063         }
00064         time = PICO_TIME_MS() - time;
00065         time = time/1000;
00066         
00067         printf("20 seconds Rx statistics\n");
00068         printf("Total bytes received : %d\n",UDP_Statistics.BytesReceived);
00069         printf("UDP Speed :%.3f Mbit/s\n",(UDP_Statistics.BytesReceived *8.0)/(20.0*MEGA));
00070         printf("Loss percentage: %.2f %%\n\n\n",(UDP_Statistics.LostPackets*100.0)/(UDP_Statistics.LostPackets+UDP_Statistics.FramesReceived));
00071     }
00072     
00073     server.close();
00074 }