Zoltan Hudak
/
UdpServer_ENC28J60
Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.
main.cpp
- Committer:
- hudakz
- Date:
- 2017-07-01
- Revision:
- 1:29bb0a32f61d
- Parent:
- 0:8823772081cb
- Child:
- 2:6bbdab30a1c1
File content as of revision 1:29bb0a32f61d:
/* * UIPEthernet UdpServer example. * * UIPEthernet is a TCP/IP stack that can be used with an enc28j60 based * Ethernet-shield. * * UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se> * * ----------------- * * This UdpServer example sets up a udp-server at 192.168.1.181 on port 7 to * send packet via upd to test * * Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de) * * Modified (ported to mbed) by Zoltan Hudak * */ #include "mbed.h" #include "UIPEthernet.h" // MAC address must be unique within the connected network. Modify as appropriate. const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; // IP address must be unique and compatible with your network. const IPAddress MY_IP(192, 168, 1, 181); const uint16_t MY_PORT = 7; const char* message = "Hello World from mbed!"; Serial pc(USBTX, USBRX); UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs UIPUDP udp; /** * @brief * @note * @param * @retval */ int main(void) { uIPEthernet.begin(MY_MAC, MY_IP); IPAddress localIP = uIPEthernet.localIP(); pc.printf("Local IP = %s\r\n", localIP.toString()); pc.printf("Initialization "); if (udp.begin(MY_PORT)) pc.printf("succeeded.\r\n"); else pc.printf("failed.\r\n"); while (1) { int success; int size = udp.parsePacket(); if (size > 0) { do { char* msg = (char*)malloc(size + 1); int len = udp.read(msg, size + 1); msg[len] = 0; printf("received: '%s", msg); free(msg); } while ((size = udp.available()) > 0); //finish reading this packet: udp.flush(); printf("'\r\n"); do { //send new packet back to ip/port of client. This also //configures the current connection to ignore packets from //other clients! success = udp.beginPacket(udp.remoteIP(), udp.remotePort()); if (success) pc.printf("beginPacket: succeeded%\r\n"); else pc.printf("beginPacket: failed%\r\n"); //beginPacket fails if remote ethaddr is unknown. In this case an //arp-request is send out first and beginPacket succeeds as soon //the arp-response is received. } while (!success); success = udp.write((uint8_t*)message, strlen(message)); if (success) pc.printf("bytes written: %d\r\n", success); success = udp.endPacket(); if (success) pc.printf("endPacket: succeeded%\r\n"); else pc.printf("endPacket: failed%\r\n"); udp.stop(); //restart with new connection to receive packets from other clients if (udp.begin(MY_PORT)) pc.printf("restart connection: succeeded%\r\n"); else pc.printf("restart connection: failed%\r\n"); } } }