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

Fork of Ethernet_UDP_client by Freescale

Committer:
mareikeFSL
Date:
Wed Dec 23 21:00:27 2015 +0000
Revision:
0:29858159c001
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mareikeFSL 0:29858159c001 1 /*------------------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 2 /* Ethernet UDP Client (to be used with Ethernet_UDP_server) */
mareikeFSL 0:29858159c001 3 /*------------------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 4
mareikeFSL 0:29858159c001 5 /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/
mareikeFSL 0:29858159c001 6 /* NXP mareikeFSL 2015.12.23 rev 1.0 initial */
mareikeFSL 0:29858159c001 7 /* */
mareikeFSL 0:29858159c001 8 /*------------------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 9 /* This "Hello World" program is used in conjunction with the Ethernet_UDP_server */
mareikeFSL 0:29858159c001 10 /* program. It communicates between two FRDM-K64F boards via the Ethernet protocol. */
mareikeFSL 0:29858159c001 11 /* To use this program, you need to do the following: */
mareikeFSL 0:29858159c001 12 /* - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable */
mareikeFSL 0:29858159c001 13 /* is not required). */
mareikeFSL 0:29858159c001 14 /* - Flash one board with Ethernet_UDP_client and the other with */
mareikeFSL 0:29858159c001 15 /* Ethernet_UDP_server */
mareikeFSL 0:29858159c001 16 /* - [optional] If you would like to see the "Hello World" output on your */
mareikeFSL 0:29858159c001 17 /* monitor, install and open a terminal. Tera Term is used in the Wiki for */
mareikeFSL 0:29858159c001 18 /* this program. */
mareikeFSL 0:29858159c001 19 /*------------------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 20
mareikeFSL 0:29858159c001 21
mareikeFSL 0:29858159c001 22 /*--INCLUDES----------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 23 #include "mbed.h"
mareikeFSL 0:29858159c001 24 #include "EthernetInterface.h"
mareikeFSL 0:29858159c001 25
mareikeFSL 0:29858159c001 26
mareikeFSL 0:29858159c001 27 /*--DEFINES-----------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 28
mareikeFSL 0:29858159c001 29
mareikeFSL 0:29858159c001 30
mareikeFSL 0:29858159c001 31 /*--CONSTANTS---------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 32 const int PORT = 7;
mareikeFSL 0:29858159c001 33
mareikeFSL 0:29858159c001 34 static const char* SERVER_IP = "192.168.1.101"; //IP of server board
mareikeFSL 0:29858159c001 35 static const char* CLIENT_IP = "192.168.1.102"; //IP of client board
mareikeFSL 0:29858159c001 36 static const char* MASK = "255.255.255.0"; //mask
mareikeFSL 0:29858159c001 37 static const char* GATEWAY = "192.168.1.1"; //gateway
mareikeFSL 0:29858159c001 38
mareikeFSL 0:29858159c001 39
mareikeFSL 0:29858159c001 40 /*--INITS-------------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 41 Serial pc(USBTX, USBRX); //create PC interface
mareikeFSL 0:29858159c001 42 EthernetInterface eth; //create ethernet
mareikeFSL 0:29858159c001 43 UDPSocket sock; //creat Ethernet socket
mareikeFSL 0:29858159c001 44 Endpoint server; //create endpoint
mareikeFSL 0:29858159c001 45
mareikeFSL 0:29858159c001 46 DigitalOut red(LED_RED); //debug led
mareikeFSL 0:29858159c001 47 DigitalOut green(LED_GREEN); //debug led
mareikeFSL 0:29858159c001 48
mareikeFSL 0:29858159c001 49
mareikeFSL 0:29858159c001 50 /*--VARIABLES---------------------------------------------------------------------------*/
mareikeFSL 0:29858159c001 51 int n; //size of received message
mareikeFSL 0:29858159c001 52 char in_buffer[1]; //create receive buffer
mareikeFSL 0:29858159c001 53 char counter[1] = {0}; //sample send buffer
mareikeFSL 0:29858159c001 54
mareikeFSL 0:29858159c001 55
mareikeFSL 0:29858159c001 56 /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
mareikeFSL 0:29858159c001 57 void init_usb(void); //initializes pc.printf if required
mareikeFSL 0:29858159c001 58 void init_eth(void); //initializes Ethernet
mareikeFSL 0:29858159c001 59 void end_eth(void); //closes Ethernet socket
mareikeFSL 0:29858159c001 60 int main(void); //main
mareikeFSL 0:29858159c001 61
mareikeFSL 0:29858159c001 62
mareikeFSL 0:29858159c001 63 /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
mareikeFSL 0:29858159c001 64
mareikeFSL 0:29858159c001 65 /*****************************************************************************INIT_USB***/
mareikeFSL 0:29858159c001 66 void init_usb(void)
mareikeFSL 0:29858159c001 67 {
mareikeFSL 0:29858159c001 68 pc.baud(9600); //baud
mareikeFSL 0:29858159c001 69
mareikeFSL 0:29858159c001 70 } //end init_usb()
mareikeFSL 0:29858159c001 71
mareikeFSL 0:29858159c001 72 /*****************************************************************************INIT_ETH***/
mareikeFSL 0:29858159c001 73 void init_eth(void)
mareikeFSL 0:29858159c001 74 {
mareikeFSL 0:29858159c001 75 eth.init(CLIENT_IP, MASK, GATEWAY); //set up IP
mareikeFSL 0:29858159c001 76 eth.connect(); //connect ethernet
mareikeFSL 0:29858159c001 77 pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress()); //get client IP address
mareikeFSL 0:29858159c001 78
mareikeFSL 0:29858159c001 79 sock.init(); //initialize socket
mareikeFSL 0:29858159c001 80
mareikeFSL 0:29858159c001 81 server.set_address(SERVER_IP, PORT); //set address of server
mareikeFSL 0:29858159c001 82
mareikeFSL 0:29858159c001 83 } //end init_eth()
mareikeFSL 0:29858159c001 84
mareikeFSL 0:29858159c001 85 /******************************************************************************END_ETH***/
mareikeFSL 0:29858159c001 86 void end_eth(void)
mareikeFSL 0:29858159c001 87 {
mareikeFSL 0:29858159c001 88 sock.close(); //close socket
mareikeFSL 0:29858159c001 89 eth.disconnect(); //close Ethernet
mareikeFSL 0:29858159c001 90
mareikeFSL 0:29858159c001 91 } //end end_eth()
mareikeFSL 0:29858159c001 92
mareikeFSL 0:29858159c001 93 /*********************************************************************************MAIN***/
mareikeFSL 0:29858159c001 94 int main(void)
mareikeFSL 0:29858159c001 95 {
mareikeFSL 0:29858159c001 96 red = 0; //client
mareikeFSL 0:29858159c001 97 green = 1;
mareikeFSL 0:29858159c001 98
mareikeFSL 0:29858159c001 99 init_usb(); //initialize the PC interface
mareikeFSL 0:29858159c001 100 init_eth(); //initialize the Ethernet connection
mareikeFSL 0:29858159c001 101
mareikeFSL 0:29858159c001 102 while(true) //repeat forever
mareikeFSL 0:29858159c001 103 {
mareikeFSL 0:29858159c001 104 pc.printf("\nCLIENT - Sending '%i' to server %s\r\n", counter[0], SERVER_IP); //print message to send
mareikeFSL 0:29858159c001 105 sock.sendTo(server, counter, sizeof(counter)); //send message
mareikeFSL 0:29858159c001 106
mareikeFSL 0:29858159c001 107 pc.printf("CLIENT - Waiting for UDP packet...\r\n"); //wait for message
mareikeFSL 0:29858159c001 108
mareikeFSL 0:29858159c001 109 n = sock.receiveFrom(server, in_buffer, sizeof(in_buffer)); //receive message from server
mareikeFSL 0:29858159c001 110
mareikeFSL 0:29858159c001 111 in_buffer[n] = '\0'; //add \0 to end of message
mareikeFSL 0:29858159c001 112 pc.printf("CLIENT - Received '%i' from server %s\r\n", in_buffer[0], SERVER_IP); //print message received
mareikeFSL 0:29858159c001 113
mareikeFSL 0:29858159c001 114 counter[0] = (counter[0] + 1)%11; //only count up to 10, then reset to 0
mareikeFSL 0:29858159c001 115
mareikeFSL 0:29858159c001 116 wait(1); //wait 1 second
mareikeFSL 0:29858159c001 117 }
mareikeFSL 0:29858159c001 118
mareikeFSL 0:29858159c001 119 } //end main()