This "Hello World" program is used in conjunction with the Ethernet_UDP_server program. It communicates between two FRDM-K64F boards via the Ethernet protocol.

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*------------------------------------------------------------------------------------*/
00002 /*  Ethernet UDP Client (to be used with Ethernet_UDP_server)                         */
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_server    */
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;
00033 
00034 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
00035 static const char* CLIENT_IP = "192.168.1.102"; //IP of client board
00036 static const char* MASK = "255.255.255.0";      //mask
00037 static const char* GATEWAY = "192.168.1.1";     //gateway
00038 
00039 
00040 /*--INITS-------------------------------------------------------------------------------*/
00041 Serial pc(USBTX, USBRX);        //create PC interface
00042 EthernetInterface eth;          //create ethernet
00043 UDPSocket sock;                 //creat Ethernet socket
00044 Endpoint server;                //create endpoint
00045 
00046 DigitalOut red(LED_RED);        //debug led
00047 DigitalOut green(LED_GREEN);    //debug led
00048 
00049 
00050 /*--VARIABLES---------------------------------------------------------------------------*/
00051 int n;                  //size of received message
00052 char in_buffer[1];      //create receive buffer
00053 char counter[1] = {0};  //sample send buffer
00054 
00055 
00056 /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
00057 void init_usb(void);    //initializes pc.printf if required
00058 void init_eth(void);    //initializes Ethernet
00059 void end_eth(void);     //closes Ethernet socket
00060 int main(void);         //main
00061 
00062 
00063 /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
00064 
00065 /*****************************************************************************INIT_USB***/
00066 void init_usb(void)
00067 {
00068     pc.baud(9600);    //baud
00069     
00070 }   //end init_usb()
00071 
00072 /*****************************************************************************INIT_ETH***/
00073 void init_eth(void)
00074 {
00075     eth.init(CLIENT_IP, MASK, GATEWAY);                                         //set up IP
00076     eth.connect();                                                              //connect ethernet
00077     pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress());    //get client IP address
00078     
00079     sock.init();                                                                //initialize socket
00080     
00081     server.set_address(SERVER_IP, PORT);                                        //set address of server
00082     
00083 }   //end init_eth()
00084 
00085 /******************************************************************************END_ETH***/
00086 void end_eth(void)
00087 {
00088     sock.close();       //close socket
00089     eth.disconnect();   //close Ethernet
00090     
00091 }   //end end_eth()
00092 
00093 /*********************************************************************************MAIN***/
00094 int main(void)
00095 {
00096     red = 0;                                                                                //client
00097     green = 1;
00098         
00099     init_usb();                                                                             //initialize the PC interface
00100     init_eth();                                                                             //initialize the Ethernet connection
00101 
00102     while(true)                                                                             //repeat forever
00103     {
00104         pc.printf("\nCLIENT - Sending '%i' to server %s\r\n", counter[0], SERVER_IP);       //print message to send
00105         sock.sendTo(server, counter, sizeof(counter));                                      //send message
00106         
00107         pc.printf("CLIENT - Waiting for UDP packet...\r\n");                                //wait for message
00108         
00109         n = sock.receiveFrom(server, in_buffer, sizeof(in_buffer));                         //receive message from server
00110         
00111         in_buffer[n] = '\0';                                                                //add \0 to end of message
00112         pc.printf("CLIENT - Received '%i' from server %s\r\n", in_buffer[0], SERVER_IP);    //print message received
00113         
00114         counter[0] = (counter[0] + 1)%11;                                                   //only count up to 10, then reset to 0
00115         
00116         wait(1);                                                                            //wait 1 second
00117     }
00118     
00119 }   //end main()