Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- UDPSendReceive.ino
- This sketch receives UDP message strings, prints them to the serial port
- and sends an "acknowledge" string back to the sender
- A Processing sketch is included at the end of file that can be used to send
- and received messages for testing with a computer.
- Here's using netcat to send udp
- echo -n "foo" | nc -4u -q1 192.168.1.177 8888
- created 21 Aug 2010
- by Michael Margolis
- modified 24 Feb 2014
- by Craig Hollabaugh
- This code is in the public domain.
- */
- #include <Ethernet.h>
- #include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
- #include <cstdint>
- #define EMAC0_BASE 0x400EC000 // Ethernet Controller
- #define EMAC_O_ADDR0H 0x00000040 // Ethernet MAC Address 0 High
- #define EMAC_O_ADDR0L 0x00000044 // Ethernet MAC Address 0 Low
- // Register
- #define EMAC_O_ADDR1H 0x00000048 // Ethernet MAC Address 1 High
- #define EMAC_O_ADDR1L 0x0000004C // Ethernet MAC Address 1 Low
- #define EMAC_O_ADDR2H 0x00000050 // Ethernet MAC Address 2 High
- #define EMAC_O_ADDR2L 0x00000054 // Ethernet MAC Address 2 Low
- #define EMAC_O_ADDR3H 0x00000058 // Ethernet MAC Address 3 High
- #define EMAC_O_ADDR3L 0x0000005C // Ethernet MAC Address 3 Low
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network:
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- IPAddress ip(192,168,1,177);
- unsigned int localPort = 8888; // local port to listen on
- // buffers for receiving and sending data
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
- char ReplyBuffer[] = "acknowledged\n"; // a string to send back
- // An EthernetUDP instance to let us send and receive packets over UDP
- EthernetUDP Udp;
- char hex[16] = {'0', '1', '2', '3', '4', '5','6','7','8','9','A','B','C','D','E','F'};
- void setup() {
- // start the Ethernet and UDP:
- Ethernet.begin(mac,ip);
- Udp.begin(localPort);
- Serial.begin(9600);
- Serial.println("UDPSendReceiveString setup");
- Serial.print("listening on ");
- ip.printTo(Serial);
- Serial.print(":");
- Serial.println(localPort);
- uint8_t b1, b2, b3, b4, b5, b6;
- b1 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR0H))) & 0xff;
- b2 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR0L))) & 0xff;
- b3 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR1H))) & 0xff;
- b4 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR1L))) & 0xff;
- b5 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR2H))) & 0xff;
- b6 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR2L))) & 0xff;
- Serial.print(hex[b1 >> 4]);
- Serial.print(hex[b1 & 0xf]);
- Serial.print(":");
- Serial.print(hex[b2 >> 4]);
- Serial.print(hex[b2 & 0xf]);
- Serial.print(":");
- Serial.print(hex[b3 >> 4]);
- Serial.print(hex[b3 & 0xf]);
- Serial.print(":");
- Serial.print(hex[b4 >> 4]);
- Serial.print(hex[b4 & 0xf]);
- Serial.print(":");
- Serial.print(hex[b5 >> 4]);
- Serial.print(hex[b5 & 0xf]);
- Serial.print(":");
- Serial.print(hex[b6 >> 4]);
- Serial.print(hex[b6 & 0xf]);
- }
- void loop() {
- // if there's data available, read a packet
- int packetSize = Udp.parsePacket();
- if(packetSize) {
- Serial.print("Received packet of size ");
- Serial.println(packetSize);
- Serial.print("From ");
- IPAddress remote = Udp.remoteIP();
- for (int i =0; i < 4; i++) {
- Serial.print(remote[i], DEC);
- if (i < 3) {
- Serial.print(".");
- }
- }
- Serial.print(", port ");
- Serial.println(Udp.remotePort());
- // read the packet into packetBufffer
- Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
- Serial.println("Contents:");
- Serial.println(packetBuffer);
- // send a reply, to the IP address and port that sent us the packet we received
- Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
- Udp.write(ReplyBuffer);
- Udp.endPacket();
- }
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement