Zoltan Hudak
/
UdpServer_ENC28J60
Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.
Diff: main.cpp
- Revision:
- 1:29bb0a32f61d
- Parent:
- 0:8823772081cb
- Child:
- 2:6bbdab30a1c1
diff -r 8823772081cb -r 29bb0a32f61d main.cpp --- a/main.cpp Fri Jun 30 20:42:13 2017 +0000 +++ b/main.cpp Sat Jul 01 08:16:18 2017 +0000 @@ -1,16 +1,34 @@ +/* + * 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" -#include "UIPUdp.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 }; +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 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; +Serial pc(USBTX, USBRX); +UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs +UIPUDP udp; /** * @brief @@ -20,33 +38,38 @@ */ 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)) + if (udp.begin(MY_PORT)) pc.printf("succeeded.\r\n"); else pc.printf("failed.\r\n"); - while(1) { + while (1) { + int success; int size = udp.parsePacket(); - if(size > 0) { + + if (size > 0) { do { char* msg = (char*)malloc(size + 1); int len = udp.read(msg, size + 1); msg[len] = 0; - } while((size = udp.available()) > 0); + printf("received: '%s", msg); + free(msg); + } while ((size = udp.available()) > 0); //finish reading this packet: udp.flush(); + printf("'\r\n"); - int success; 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) + if (success) pc.printf("beginPacket: succeeded%\r\n"); else pc.printf("beginPacket: failed%\r\n"); @@ -54,17 +77,16 @@ //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); + } while (!success); - char* message = "Hello World from mbed"; success = udp.write((uint8_t*)message, strlen(message)); - if(success) + if (success) pc.printf("bytes written: %d\r\n", success); success = udp.endPacket(); - if(success) + if (success) pc.printf("endPacket: succeeded%\r\n"); else pc.printf("endPacket: failed%\r\n"); @@ -72,7 +94,7 @@ udp.stop(); //restart with new connection to receive packets from other clients - if(udp.begin(MY_PORT)) + if (udp.begin(MY_PORT)) pc.printf("restart connection: succeeded%\r\n"); else pc.printf("restart connection: failed%\r\n");