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.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of Ethernet_UDP_server by
main.cpp
00001 /*------------------------------------------------------------------------------------*/ 00002 /* Ethernet UDP Server (to be used with Ethernet_UDP_client) */ 00003 /*------------------------------------------------------------------------------------*/ 00004 00005 /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/ 00006 /* NXP mareikeFSL 2015.12.23 rev 1.0 initial */ 00007 /* */ 00008 /*------------------------------------------------------------------------------------*/ 00009 /* This "Hello World" program is used in conjunction with the Ethernet_UDP_client */ 00010 /* program. It communicates between two FRDM-K64F boards via the Ethernet protocol. */ 00011 /* To use this program, you need to do the following: */ 00012 /* - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable */ 00013 /* is not required). */ 00014 /* - Flash one board with Ethernet_UDP_client and the other with */ 00015 /* Ethernet_UDP_server */ 00016 /* - [optional] If you would like to see the "Hello World" output on your */ 00017 /* monitor, install and open a terminal. Tera Term is used in the Wiki for */ 00018 /* this program. */ 00019 /*------------------------------------------------------------------------------------*/ 00020 00021 00022 /*--INCLUDES----------------------------------------------------------------------------*/ 00023 #include "mbed.h" 00024 #include "EthernetInterface.h" 00025 00026 00027 /*--DEFINES-----------------------------------------------------------------------------*/ 00028 00029 00030 00031 /*--CONSTANTS---------------------------------------------------------------------------*/ 00032 const int PORT = 7; //arbitrary port 00033 00034 static const char* SERVER_IP = "192.168.1.101"; //IP of server board 00035 static const char* MASK = "255.255.255.0"; //mask 00036 static const char* GATEWAY = "192.168.1.1"; //gateway 00037 00038 00039 /*--INITS-------------------------------------------------------------------------------*/ 00040 Serial pc(USBTX, USBRX); //create PC interface 00041 EthernetInterface eth; //create ethernet 00042 UDPSocket server; //creat server 00043 Endpoint client; //create endpoint 00044 00045 DigitalOut red(LED_RED); //debug led 00046 DigitalOut green(LED_GREEN); //debug led 00047 00048 00049 /*--VARIABLES---------------------------------------------------------------------------*/ 00050 int n; //size of received message 00051 char counter[1] = {0}; //sample receive/send buffer 00052 00053 00054 /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/ 00055 void init_usb(void); //initializes pc.printf if required 00056 void init_eth(void); //initializes Ethernet 00057 void receive(void); //receives packets 00058 int main(void); //main 00059 00060 00061 /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/ 00062 00063 /*****************************************************************************INIT_USB***/ 00064 void init_usb(void) 00065 { 00066 pc.baud(9600); //baud 00067 00068 } //end init_usb() 00069 00070 /*****************************************************************************INIT_ETH***/ 00071 void init_eth(void) 00072 { 00073 eth.init(SERVER_IP, MASK, GATEWAY); //set up IP 00074 eth.connect(); //connect ethernet 00075 pc.printf("\nSERVER - Server IP Address is %s\r\n", eth.getIPAddress()); //get server IP address; 00076 00077 server.bind(PORT); //bind server 00078 00079 } //end init_eth() 00080 00081 /******************************************************************************RECEIVE***/ 00082 void receive(void) 00083 { 00084 pc.printf("\nSERVER - Waiting for UDP packet...\r\n"); //wait for packet 00085 n = server.receiveFrom(client, counter, sizeof(counter)); //receive message from client 00086 counter[n] = '\0'; //add \0 to end of message 00087 00088 pc.printf("SERVER - Received '%i' from client %s\r\n", counter[0], client.get_address()); //print message and client 00089 pc.printf("SERVER - Sending '%i' back to client %s\r\n", counter[0], client.get_address()); //print sending back 00090 server.sendTo(client, counter, n); //send message 00091 00092 } //end receive() 00093 00094 /*********************************************************************************MAIN***/ 00095 int main(void) 00096 { 00097 red = 1; 00098 green = 0; //server 00099 00100 init_usb(); //initialize the PC interface 00101 init_eth(); //initialize the Ethernet connection 00102 00103 while (true) //repeat forever 00104 { 00105 receive(); //wait for message 00106 } 00107 00108 } //end main()
Generated on Tue Jul 12 2022 21:11:44 by
1.7.2
