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.
main.cpp
- Committer:
- timo_k2
- Date:
- 2020-11-23
- Revision:
- 3:9615ce1548c4
- Parent:
- 2:150dadff3c6b
- Child:
- 6:11edc8f9d870
File content as of revision 3:9615ce1548c4:
/* * Copyright (c) 2006-2020 Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 *********************************** * Round trip delay meter. This is the "echo" module. * A microcontroller board with an Ethernet interface. * An other microcontroller with "Round Trip Delay" will be needed. * ST NUCLEO H743ZI2 used for testing. * * Timo Karppinen 23.11.2020 ***********************************/ #include "mbed.h" #include "EthernetInterface.h" #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 //Network interface EthernetInterface net; //Threads Thread recv_thread; Thread send_thread; // UDP uint8_t ip[4] = {192, 168, 1, 104}; SocketAddress clientUDP(ip, NSAPI_IPv4); //SocketAddress clientUDP(strIP); // Client on remote device //SocketAddress clientUDP; UDPSocket serverUDP; // UDP server in this device // Functions void udpReceive( void ); void udpSend( void ); DigitalIn sw2(PC_13); // Blue button on H743ZI, button pressed = TRUE DigitalOut led2(PE_1); // Yellow LED on H743ZI int sw2state = 0; int sw2old = 1; char in_data[BUFF_SIZE]; int newDatagram = 0; int newDatagramOld = 0; char out_data[BUFF_SIZE]; //using namespace std::chrono; Timer armedFor; int main() { printf("\nEcho for the Round Trip Delay application (using Ethernet)\r\n"); //Bring up the network interface net.connect(); // Show network address SocketAddress netAddress; net.get_ip_address(&netAddress); printf("\n\n UDPServer IP Address: %s\n", netAddress.get_ip_address() ? netAddress.get_ip_address():"None"); // UDP server serverUDP.open(&net); int err = serverUDP.bind(LOCAL_PORT); printf("Port status is: %d\n",err); recv_thread.start(udpReceive); printf("Listening has been started at port number %d\n", LOCAL_PORT); 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"); while(1) { sw2state = sw2.read(); printf( "sw2state is %d\n", sw2state); if((sw2state == 1)&&(sw2state != sw2old)) { led2.write(1); armedFor.reset(); // reset timer to zero armedFor.start(); snprintf(out_data, BUFF_SIZE, "Echo -server listening" ); clientUDP.set_port(REMOTE_PORT); udpSend(); snprintf(out_data, BUFF_SIZE, "Echo" ); // 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 == 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; } } newDatagramOld = newDatagram; //Sending the "Echo" once only. } } 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. printf("\nWe stopped sending more UDP packets to the server.\nYou can unplug your device!\n"); ThisThread::sleep_for(4000ms); } } // The functions void udpReceive() { int bytes; while(1) { bytes = serverUDP.recvfrom(&clientUDP, &in_data, BUFF_SIZE); newDatagram = 1; // set this before using time for printing 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"); } } void udpSend() { //char out_data[BUFF_SIZE]; //snprintf(out_data, BUFF_SIZE, "Echo" ); //clientUDP.set_port(REMOTE_PORT); 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"); }