Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 8:e500ff7ddcaf
- Parent:
- 7:59a37ccd06ef
- Child:
- 9:264e9d879cf5
--- a/main.cpp Mon Nov 23 13:28:40 2020 +0000
+++ b/main.cpp Sun Jan 17 20:11:12 2021 +0000
@@ -15,17 +15,17 @@
#define REMOTE_PORT 5001 // The port numbers the other way round
#define LOCAL_PORT 5000 // than in the "Round Trip Delay"
-#define BUFF_SIZE 128
+#define BUFF_SIZE 128 // test with 32, 128, 512, etc
//Network interface
EthernetInterface net;
//Threads
Thread recv_thread;
- Thread send_thread;
+// Thread send_thread;
// UDP
-uint8_t ip[4] = {192, 168, 1, 104};
+uint8_t ip[4] = {192, 168, 1, 10}; // The remote IP address
SocketAddress clientUDP(ip, NSAPI_IPv4); // The remote device
UDPSocket serverUDP; // UDP server in this device
@@ -54,6 +54,8 @@
printf("\nEcho for the Round Trip Delay application (using Ethernet)\r\n");
//Bring up the network interface
+ //eth.set_network(IP_Adress,GATEWAY,MASK);
+ net.set_network("192.168.1.12","192.168.1.1","255.255.252.0");
net.connect();
// Show network address
@@ -71,7 +73,7 @@
recv_thread.start(udpReceive);
printf("Listening has been started at port number %d\n", LOCAL_PORT);
- send_thread.start(udpSend);
+ //send_thread.start(udpSend);
printf("Sending out \"Echo\" data to port number %d", REMOTE_PORT);
printf(" will be armed for triggering by pushing Blue button.\n");
printf("The IP is taken from the incoming message \n");
@@ -84,34 +86,35 @@
led2.write(1);
armedFor.reset(); // reset timer to zero
armedFor.start();
- snprintf(out_data, BUFF_SIZE, "Echo -server listening" );
+ snprintf(out_data, BUFF_SIZE, "Echo -server listening for 8 sec" );
clientUDP.set_port(REMOTE_PORT);
- udpSend();
+ udpSend();
+
snprintf(out_data, BUFF_SIZE, "Echo" );
- // Start polling for the incomening "Echo" UDP datagram
+ // Start polling for the incoming "Echo" UDP datagram
while ( armedFor.elapsed_time().count() < 8000000 ){
if((newDatagram == 1)&&(newDatagram != newDatagramOld)){
char firstChar;
firstChar = in_data[0];
if (firstChar == 85){ // ASCII symbol 85 = "U" for the incoming
udpSend(); // Sending out first and then printing!
- printf( "firstChar: %s\n", &firstChar);
- }
- for (int k =0; k < BUFF_SIZE; k++){
- in_data[k] = 0;
- }
+ //printf( "firstChar: %s\n", &firstChar);
+ }
}
newDatagramOld = newDatagram; //Sending the "Echo" once only.
+ newDatagram = 0;
}
}
- newDatagram = 0;
sw2old = sw2state; // Once only with pushing the button as long as you like.
led2.write(0);
armedFor.stop(); // Stop the "armed for" if did not receive the request.
+ for (int k =0; k < BUFF_SIZE; k++){
+ in_data[k] = 0;
+ }
- printf("\nWe stopped sending more UDP packets to the server.\nYou can unplug your device!\n");
- ThisThread::sleep_for(4000ms);
+ //printf("\nIn sleep mode for one \n");
+ ThisThread::sleep_for(1000ms);
}
}
@@ -124,12 +127,12 @@
while(1) {
bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE);
newDatagram = 1; // set this before using time for printing
+ ThisThread::sleep_for(100ms);
printf("\n");
printf("bytes received: %d\n",bytes);
printf("string: %s\n",in_data);
printf("client address: %s\n", clientUDP.get_ip_address());
printf("\n");
-
}
}
@@ -142,6 +145,5 @@
serverUDP.sendto(clientUDP, out_data, sizeof(out_data));
printf("Sending out: %s\n", out_data);
printf("with %d" , sizeof(out_data));
- printf(" data bytes in UDP datagram\n");
-
-}
+ printf(" data bytes in UDP datagram\n");
+}
\ No newline at end of file