Timo Karppinen / Mbed OS UDP_RoundTripEcho_OS6_H743ZI
Revision:
2:150dadff3c6b
Parent:
1:961861d73841
Child:
3:9615ce1548c4
Child:
5:fb39c8c13b34
--- a/main.cpp	Fri Nov 20 16:56:46 2020 +0000
+++ b/main.cpp	Sat Nov 21 16:03:30 2020 +0000
@@ -48,10 +48,12 @@
 
 char in_data[BUFF_SIZE];
 int newDatagram = 0;
+int newDatagramOld = 0;
 time_t timeNTP = 0;
 
 //using namespace std::chrono;
 Timer stopwatch;
+Timer armedFor;
     
 int main() {
     printf("\nNTP Client example (using Ethernet)\r\n");
@@ -88,37 +90,43 @@
     
     if((sw2state == 0)&&(sw2state != sw2old)) {
         led2.write(0);
+        armedFor.reset();   // reset timer to zero
         stopwatch.reset();   // reset stopwatch timer to zero
         timeNTP = getNTP();
+        armedFor.start();
         stopwatch.start();
         udpSend();
         
     
-        // Start polling for the incomening "echo" UDP datagram   
-        while ( stopwatch.elapsed_time().count() < 8000000 ){
-        if(newDatagram == 1){
-            char firstChar;
-            firstChar = in_data[0];
-            if (firstChar == 83){    // ASCII symbol 83 = "S" 
-                stopwatch.stop();
-                }
-            newDatagram = 0;
+        // Start polling for the incomening "Echo" UDP datagram   
+        while ( armedFor.elapsed_time().count() < 8000000 ){
+            if((newDatagram == 1)&&(newDatagram != newDatagramOld)){
+                char firstChar;
+                firstChar = in_data[0];
+                if (firstChar == 69){    // ASCII symbol 69 = "E" for the Echo
+                    stopwatch.stop();
+                    printf( "firstChar: %s\n", &firstChar);
+                    }
+                for (int k =0; k < BUFF_SIZE; k++){
+                    in_data[k] = 0;
+                    }  
             }
-        for (int k =0; k < BUFF_SIZE; k++){
-            in_data[k] = 0;
-            }
+            newDatagramOld = newDatagram; //Reading the stopwatch once only
+             
         }
+        
+        // printing for testing. Replace with writing once to a SD memory card.
+        printf("Current time in day month hour.min.sec year is  %s\r\n", ctime(&timeNTP));
+        printf("The time taken was %llu microseconds\n", stopwatch.elapsed_time().count());
     }
+    newDatagram = 0;
     sw2old = sw2state; // Once only with pushing the button as long as you like.
     led2.write(1);
+    armedFor.stop();
     stopwatch.stop();  // Stop the stopwatch if we did not receive the echo.
-    ThisThread::sleep_for(10000ms);
-    // printing for testing. Replace with writing once to a SD memory card.
-    printf("Current time in day month hour.min.sec year is  %s\r\n", ctime(&timeNTP));
-    printf("The time taken was %llu microseconds\n", stopwatch.elapsed_time().count());
     
     printf("\nWe stopped sending more UDP packets to the server.\nYou can unplug your device!\n");
-    //ThisThread::sleep_for(2s);
+    ThisThread::sleep_for(4000ms);
     }
 }
 
@@ -150,7 +158,7 @@
         printf("string: %s\n",in_data);
         printf("client address: %s\n", clientUDP.get_ip_address());
         printf("\n");
-        newDatagram = 1;;
+        newDatagram = 1;
         }
 }
 
@@ -158,7 +166,7 @@
 {
         char out_data[BUFF_SIZE];
         
-        snprintf(out_data, BUFF_SIZE, "UDP message for getting the echo" );
+        snprintf(out_data, BUFF_SIZE, "UDP message for getting the Echo" );
         clientUDP.set_port(REMOTE_PORT);
         serverUDP.sendto(clientUDP, out_data, sizeof(out_data));
         printf("Sending out: %s\n", out_data);