w5Ethernetserver
Dependencies: EthernetInterface mbed-rtos mbed
Fork of exe5_3_Ethernet_UDP_server by
main.cpp@0:86b7ebac3e60, 2016-12-01 (annotated)
- Committer:
- steveshun
- Date:
- Thu Dec 01 14:12:26 2016 +0000
- Revision:
- 0:86b7ebac3e60
Exercise 5: Ethernet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steveshun | 0:86b7ebac3e60 | 1 | /*------------------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 2 | /* Ethernet UDP Server (to be used with Ethernet_UDP_client) */ |
steveshun | 0:86b7ebac3e60 | 3 | /*------------------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 4 | |
steveshun | 0:86b7ebac3e60 | 5 | /*--COMPANY-----AUTHOR------DATE------------REVISION----NOTES-------------------------*/ |
steveshun | 0:86b7ebac3e60 | 6 | /* NXP mareikeFSL 2015.12.23 rev 1.0 initial */ |
steveshun | 0:86b7ebac3e60 | 7 | /* */ |
steveshun | 0:86b7ebac3e60 | 8 | /*------------------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 9 | /* This "Hello World" program is used in conjunction with the Ethernet_UDP_client */ |
steveshun | 0:86b7ebac3e60 | 10 | /* program. It communicates between two FRDM-K64F boards via the Ethernet protocol. */ |
steveshun | 0:86b7ebac3e60 | 11 | /* To use this program, you need to do the following: */ |
steveshun | 0:86b7ebac3e60 | 12 | /* - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable */ |
steveshun | 0:86b7ebac3e60 | 13 | /* is not required). */ |
steveshun | 0:86b7ebac3e60 | 14 | /* - Flash one board with Ethernet_UDP_client and the other with */ |
steveshun | 0:86b7ebac3e60 | 15 | /* Ethernet_UDP_server */ |
steveshun | 0:86b7ebac3e60 | 16 | /* - [optional] If you would like to see the "Hello World" output on your */ |
steveshun | 0:86b7ebac3e60 | 17 | /* monitor, install and open a terminal. Tera Term is used in the Wiki for */ |
steveshun | 0:86b7ebac3e60 | 18 | /* this program. */ |
steveshun | 0:86b7ebac3e60 | 19 | /*------------------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 20 | |
steveshun | 0:86b7ebac3e60 | 21 | |
steveshun | 0:86b7ebac3e60 | 22 | /*--INCLUDES----------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 23 | #include "mbed.h" |
steveshun | 0:86b7ebac3e60 | 24 | #include "EthernetInterface.h" |
steveshun | 0:86b7ebac3e60 | 25 | |
steveshun | 0:86b7ebac3e60 | 26 | |
steveshun | 0:86b7ebac3e60 | 27 | /*--DEFINES-----------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 28 | |
steveshun | 0:86b7ebac3e60 | 29 | |
steveshun | 0:86b7ebac3e60 | 30 | |
steveshun | 0:86b7ebac3e60 | 31 | /*--CONSTANTS---------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 32 | const int PORT = 7; //arbitrary port |
steveshun | 0:86b7ebac3e60 | 33 | |
steveshun | 0:86b7ebac3e60 | 34 | static const char* SERVER_IP = "192.168.1.101"; //IP of server board |
steveshun | 0:86b7ebac3e60 | 35 | static const char* MASK = "255.255.255.0"; //mask |
steveshun | 0:86b7ebac3e60 | 36 | static const char* GATEWAY = "192.168.1.1"; //gateway |
steveshun | 0:86b7ebac3e60 | 37 | |
steveshun | 0:86b7ebac3e60 | 38 | |
steveshun | 0:86b7ebac3e60 | 39 | /*--INITS-------------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 40 | Serial pc(USBTX, USBRX); //create PC interface |
steveshun | 0:86b7ebac3e60 | 41 | EthernetInterface eth; //create ethernet |
steveshun | 0:86b7ebac3e60 | 42 | UDPSocket server; //creat server |
steveshun | 0:86b7ebac3e60 | 43 | Endpoint client; //create endpoint |
steveshun | 0:86b7ebac3e60 | 44 | |
steveshun | 0:86b7ebac3e60 | 45 | DigitalOut red(LED_RED); //debug led |
steveshun | 0:86b7ebac3e60 | 46 | DigitalOut green(LED_GREEN); //debug led |
steveshun | 0:86b7ebac3e60 | 47 | |
steveshun | 0:86b7ebac3e60 | 48 | |
steveshun | 0:86b7ebac3e60 | 49 | /*--VARIABLES---------------------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 50 | int n; //size of received message |
steveshun | 0:86b7ebac3e60 | 51 | char counter[1] = {0}; //sample receive/send buffer |
steveshun | 0:86b7ebac3e60 | 52 | |
steveshun | 0:86b7ebac3e60 | 53 | |
steveshun | 0:86b7ebac3e60 | 54 | /*--FUNCTION DECLARATIONS---------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 55 | void init_usb(void); //initializes pc.printf if required |
steveshun | 0:86b7ebac3e60 | 56 | void init_eth(void); //initializes Ethernet |
steveshun | 0:86b7ebac3e60 | 57 | void receive(void); //receives packets |
steveshun | 0:86b7ebac3e60 | 58 | int main(void); //main |
steveshun | 0:86b7ebac3e60 | 59 | |
steveshun | 0:86b7ebac3e60 | 60 | |
steveshun | 0:86b7ebac3e60 | 61 | /*--FUNCTION DEFINITIONS----------------------------------------------------------------*/ |
steveshun | 0:86b7ebac3e60 | 62 | |
steveshun | 0:86b7ebac3e60 | 63 | /*****************************************************************************INIT_USB***/ |
steveshun | 0:86b7ebac3e60 | 64 | void init_usb(void) |
steveshun | 0:86b7ebac3e60 | 65 | { |
steveshun | 0:86b7ebac3e60 | 66 | pc.baud(9600); //baud |
steveshun | 0:86b7ebac3e60 | 67 | |
steveshun | 0:86b7ebac3e60 | 68 | } //end init_usb() |
steveshun | 0:86b7ebac3e60 | 69 | |
steveshun | 0:86b7ebac3e60 | 70 | /*****************************************************************************INIT_ETH***/ |
steveshun | 0:86b7ebac3e60 | 71 | void init_eth(void) |
steveshun | 0:86b7ebac3e60 | 72 | { |
steveshun | 0:86b7ebac3e60 | 73 | eth.init(SERVER_IP, MASK, GATEWAY); //set up IP |
steveshun | 0:86b7ebac3e60 | 74 | eth.connect(); //connect ethernet |
steveshun | 0:86b7ebac3e60 | 75 | pc.printf("\nSERVER - Server IP Address is %s\r\n", eth.getIPAddress()); //get server IP address; |
steveshun | 0:86b7ebac3e60 | 76 | |
steveshun | 0:86b7ebac3e60 | 77 | server.bind(PORT); //bind server |
steveshun | 0:86b7ebac3e60 | 78 | |
steveshun | 0:86b7ebac3e60 | 79 | } //end init_eth() |
steveshun | 0:86b7ebac3e60 | 80 | |
steveshun | 0:86b7ebac3e60 | 81 | /******************************************************************************RECEIVE***/ |
steveshun | 0:86b7ebac3e60 | 82 | void receive(void) |
steveshun | 0:86b7ebac3e60 | 83 | { |
steveshun | 0:86b7ebac3e60 | 84 | pc.printf("\nSERVER - Waiting for UDP packet...\r\n"); //wait for packet |
steveshun | 0:86b7ebac3e60 | 85 | n = server.receiveFrom(client, counter, sizeof(counter)); //receive message from client |
steveshun | 0:86b7ebac3e60 | 86 | counter[n] = '\0'; //add \0 to end of message |
steveshun | 0:86b7ebac3e60 | 87 | |
steveshun | 0:86b7ebac3e60 | 88 | pc.printf("SERVER - Received '%i' from client %s\r\n", counter[0], client.get_address()); //print message and client |
steveshun | 0:86b7ebac3e60 | 89 | pc.printf("SERVER - Sending '%i' back to client %s\r\n", counter[0], client.get_address()); //print sending back |
steveshun | 0:86b7ebac3e60 | 90 | server.sendTo(client, counter, n); //send message |
steveshun | 0:86b7ebac3e60 | 91 | |
steveshun | 0:86b7ebac3e60 | 92 | } //end receive() |
steveshun | 0:86b7ebac3e60 | 93 | |
steveshun | 0:86b7ebac3e60 | 94 | /*********************************************************************************MAIN***/ |
steveshun | 0:86b7ebac3e60 | 95 | int main(void) |
steveshun | 0:86b7ebac3e60 | 96 | { |
steveshun | 0:86b7ebac3e60 | 97 | red = 1; |
steveshun | 0:86b7ebac3e60 | 98 | green = 0; //server |
steveshun | 0:86b7ebac3e60 | 99 | |
steveshun | 0:86b7ebac3e60 | 100 | init_usb(); //initialize the PC interface |
steveshun | 0:86b7ebac3e60 | 101 | init_eth(); //initialize the Ethernet connection |
steveshun | 0:86b7ebac3e60 | 102 | |
steveshun | 0:86b7ebac3e60 | 103 | while (true) //repeat forever |
steveshun | 0:86b7ebac3e60 | 104 | { |
steveshun | 0:86b7ebac3e60 | 105 | receive(); //wait for message |
steveshun | 0:86b7ebac3e60 | 106 | } |
steveshun | 0:86b7ebac3e60 | 107 | |
steveshun | 0:86b7ebac3e60 | 108 | } //end main() |