Timo Karppinen / Mbed OS UDP_RoundTripDelay_OS6_K64F

Dependencies:   ntp-client

Revision:
5:837b2680bebb
Parent:
4:4118d38b68e1
diff -r 4118d38b68e1 -r 837b2680bebb main.cpp
--- a/main.cpp	Sun Jan 17 19:09:05 2021 +0000
+++ b/main.cpp	Sat Apr 24 15:28:36 2021 +0000
@@ -9,7 +9,7 @@
  * An other microcontroller with "Round trip echo" will be needed. 
  * NXP FRDM-K64F used for testing.
  *   
- * Timo Karppinen 17.1.2021
+ * Timo Karppinen 24.4.2021
  ***********************************/
 #include "mbed.h"
 #include "EthernetInterface.h"
@@ -17,7 +17,7 @@
 
 #define REMOTE_PORT 5000
 #define LOCAL_PORT 5001
-#define BUFF_SIZE 128  // test with 32, 128, 512, etc
+#define BUFF_SIZE 512  // test with 32, 128, 512
 
 //Network interface
 EthernetInterface net;
@@ -26,7 +26,7 @@
     Thread recv_thread;
 
 // UDP
-SocketAddress clientUDP;  // Client on remote device
+SocketAddress clientUDP;  // Client on remote device. IP address from incoming message.
 UDPSocket serverUDP;   // UDP server in this device
 
 //NTP server is a time server used for delivering timing information for networks.
@@ -40,14 +40,14 @@
 // Functions
 //time_t getNTP();
 void udpReceive( void );
-void udpSend( void );
+void udpSend( int ii );
 
 DigitalIn sw2(SW2);     // sw2 on K64F, button pressed = FALSE
 DigitalOut led2(LED2);  // RGB LED on K64F, FALSE = Green
 int sw2state = 0;
 int sw2old = 1;
 
-char in_data[BUFF_SIZE];
+char in_data[BUFF_SIZE]; // 512 taken as max message size
 int newDatagram = 0;
 int newDatagramOld = 0;
 //time_t timeNTP = 0;
@@ -59,8 +59,8 @@
     printf("\nRound trip delay in UDP messaging (using Ethernet)\n");
     
     //Bring up the network interface
-    //eth.set_network(IP_Adress,GATEWAY,MASK); // leave out if using DHCP
-    net.set_network("192.168.1.10","192.168.1.1","255.255.252.0");
+    //eth.set_network(IP_Adress,MASK, GATEWAY); // leave out if using DHCP
+    net.set_network("192.168.1.10","255.255.252.0","192.168.1.1");
     net.connect();
     
     // Show network address
@@ -89,46 +89,53 @@
   
     while(1) {  
     sw2state = sw2.read();
-    printf( "\nsw2state is  %d\n", sw2state); // Printing showing "I am alive".
+    printf( "\nsw2  %d  -  Push for sending Echo Request\n", sw2state); 
     
     if((sw2state == 0)&&(sw2state != sw2old)) {   // Note! Checking for FALSE sw
-        led2.write(1);
+      led2.write(1);
+      for ( int i=0; i < 5; ++i){
         armedFor.reset();   // reset timer to zero
         stopwatch.reset();   // reset stopwatch timer to zero
         //timeNTP = getNTP();
+        ThisThread::sleep_for(10ms); //time for resetting
         armedFor.start();
         //stopwatch.start();  // moved to udpSend subroutine
-        udpSend();
+        udpSend(i);
         
     
         // Start polling for the incoming "Echo" UDP datagram   
-        while ( armedFor.elapsed_time().count() < 8000000 ){
+        while ( armedFor.elapsed_time().count() <2000000 ){
             if((newDatagram == 1)&&(newDatagram != newDatagramOld)){
                 stopwatch.stop(); 
                 char firstChar;
                 firstChar = in_data[0];
-                printf( "firstChar: %s\n", &firstChar);
+                printf( "\nfirstChar: %s\n", &firstChar);
+                ThisThread::sleep_for(120ms); // waiting for print buffer, 100 ms was just too short
                 for (int k =0; k < BUFF_SIZE; k++){
                     in_data[k] = 0;
                     } 
-            }
+                // printing for testing. Replace with writing once to a SD memory card.
+                //printf("Measured at ( day month hour.min.sec year ) %s\r\n", ctime(&timeNTP));
+                printf("The time taken was %llu microseconds\n", stopwatch.elapsed_time().count());
+            } // if new datagram
             newDatagramOld = newDatagram; //Reading the stopwatch once only 
-            newDatagram = 0;   
-        }
+            newDatagram = 0;  
+        } // while armed
+        ThisThread::sleep_for(70ms); // Delay of 10ms + 120ms + 70ms = 200ms
+      } //for i loop
         
         // printing for testing. Replace with writing once to a SD memory card.
         //printf("Measured at ( day month hour.min.sec year ) %s\r\n", ctime(&timeNTP));
-        printf("The time taken was %llu microseconds\n", stopwatch.elapsed_time().count());
-        
-    }
+        //printf("The time taken was %llu microseconds\n", stopwatch.elapsed_time().count()); 
+    } // if sw2
     sw2old = sw2state; // Once only with pushing the button as long as you like.
     led2.write(0);
     armedFor.stop();
     stopwatch.stop();  // Stop the stopwatch if we did not receive the echo.
     
     ThisThread::sleep_for(1000ms);
-    }
-}
+    } // while loop
+} // main
 
 // The functions
   
@@ -152,24 +159,38 @@
 {
     int bytes;
     while(1) {
-        bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE);
+        bytes = serverUDP.recvfrom(&clientUDP, &in_data, 512); // 512 as largest possible
         newDatagram = 1;    // set this before using time for printing
-        ThisThread::sleep_for(400ms); // waiting for the print buffer
-        printf("\n");
+        ThisThread::sleep_for(50ms); // waiting for the print buffer
         printf("bytes received: %d\n",bytes);
         printf("string: %s\n",in_data);
         printf("client address: %s\n", clientUDP.get_ip_address());
-        printf("\n");
- 
         }
 }
 
-void udpSend()
+void udpSend(int ii)
 {
-        char out_data[BUFF_SIZE];
-        
-        snprintf(out_data, BUFF_SIZE, "UDP message for getting the Echo" );
-        printf("Sending out: %s\n", out_data);
+        int BUFF_SIZE_NOW;
+        switch(ii){
+            case 0:
+            BUFF_SIZE_NOW = BUFF_SIZE/16;
+            break;
+            case 1:
+            BUFF_SIZE_NOW = BUFF_SIZE/8;
+            break;
+            case 2:
+            BUFF_SIZE_NOW = BUFF_SIZE/4;
+            break;
+            case 3:
+            BUFF_SIZE_NOW = BUFF_SIZE/2;
+            break;
+            case 4:
+            BUFF_SIZE_NOW = BUFF_SIZE;
+            break;
+        }
+        char out_data[BUFF_SIZE_NOW];
+        snprintf(out_data, BUFF_SIZE_NOW, "UDP message for getting the Echo" );
+        printf("\nSending out: %s\n", out_data);
         printf("with %d" , sizeof(out_data));
         printf(" data bytes in UDP datagram\n");
         clientUDP.set_port(REMOTE_PORT);