Advertisement
wandrake

Untitled

Nov 30th, 2014
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. /*
  2.  UDPSendReceive.ino
  3.  
  4.  This sketch receives UDP message strings, prints them to the serial port
  5.  and sends an "acknowledge" string back to the sender
  6.  
  7.  A Processing sketch is included at the end of file that can be used to send
  8.  and received messages for testing with a computer.
  9.  
  10.  Here's using netcat to send udp
  11.      echo -n "foo" | nc -4u -q1 192.168.1.177 8888
  12.  
  13.  created 21 Aug 2010
  14.  by Michael Margolis
  15.  
  16.  modified 24 Feb 2014
  17.  by Craig Hollabaugh
  18.  
  19.  This code is in the public domain.
  20. */
  21.  
  22.  
  23. #include <Ethernet.h>
  24. #include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  25. #include <cstdint>
  26.  
  27. #define EMAC0_BASE              0x400EC000  // Ethernet Controller
  28. #define EMAC_O_ADDR0H 0x00000040 // Ethernet MAC Address 0 High
  29. #define EMAC_O_ADDR0L 0x00000044 // Ethernet MAC Address 0 Low
  30. // Register
  31. #define EMAC_O_ADDR1H 0x00000048 // Ethernet MAC Address 1 High
  32. #define EMAC_O_ADDR1L 0x0000004C // Ethernet MAC Address 1 Low
  33. #define EMAC_O_ADDR2H 0x00000050 // Ethernet MAC Address 2 High
  34. #define EMAC_O_ADDR2L 0x00000054 // Ethernet MAC Address 2 Low
  35. #define EMAC_O_ADDR3H 0x00000058 // Ethernet MAC Address 3 High
  36. #define EMAC_O_ADDR3L 0x0000005C // Ethernet MAC Address 3 Low
  37.  
  38. // Enter a MAC address and IP address for your controller below.
  39. // The IP address will be dependent on your local network:
  40. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  41. IPAddress ip(192,168,1,177);
  42.  
  43. unsigned int localPort = 8888;      // local port to listen on
  44.  
  45. // buffers for receiving and sending data
  46. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  47. char  ReplyBuffer[] = "acknowledged\n";       // a string to send back
  48.  
  49. // An EthernetUDP instance to let us send and receive packets over UDP
  50. EthernetUDP Udp;
  51.  
  52. char hex[16] = {'0', '1', '2', '3', '4', '5','6','7','8','9','A','B','C','D','E','F'};
  53.  
  54. void setup() {
  55.   // start the Ethernet and UDP:
  56.   Ethernet.begin(mac,ip);
  57.   Udp.begin(localPort);
  58.  
  59.   Serial.begin(9600);
  60.   Serial.println("UDPSendReceiveString setup");
  61.   Serial.print("listening on ");
  62.   ip.printTo(Serial);
  63.   Serial.print(":");
  64.   Serial.println(localPort);
  65.  
  66.   uint8_t b1, b2, b3, b4, b5, b6;
  67.  
  68.   b1 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR0H))) & 0xff;
  69.   b2 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR0L))) & 0xff;
  70.   b3 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR1H))) & 0xff;
  71.   b4 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR1L))) & 0xff;
  72.   b5 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR2H))) & 0xff;
  73.   b6 = (*((uint32_t*)(EMAC0_BASE + EMAC_O_ADDR2L))) & 0xff;
  74.  
  75.   Serial.print(hex[b1 >> 4]);
  76.   Serial.print(hex[b1 & 0xf]);
  77.   Serial.print(":");
  78.   Serial.print(hex[b2 >> 4]);
  79.   Serial.print(hex[b2 & 0xf]);
  80.   Serial.print(":");
  81.   Serial.print(hex[b3 >> 4]);
  82.   Serial.print(hex[b3 & 0xf]);
  83.   Serial.print(":");
  84.   Serial.print(hex[b4 >> 4]);
  85.   Serial.print(hex[b4 & 0xf]);
  86.   Serial.print(":");
  87.   Serial.print(hex[b5 >> 4]);
  88.   Serial.print(hex[b5 & 0xf]);
  89.   Serial.print(":");
  90.   Serial.print(hex[b6 >> 4]);
  91.   Serial.print(hex[b6 & 0xf]);
  92. }
  93.  
  94. void loop() {
  95.   // if there's data available, read a packet
  96.  
  97.   int packetSize = Udp.parsePacket();
  98.   if(packetSize) {
  99.     Serial.print("Received packet of size ");
  100.     Serial.println(packetSize);
  101.     Serial.print("From ");
  102.     IPAddress remote = Udp.remoteIP();
  103.     for (int i =0; i < 4; i++) {
  104.       Serial.print(remote[i], DEC);
  105.       if (i < 3) {
  106.         Serial.print(".");
  107.       }
  108.     }
  109.     Serial.print(", port ");
  110.     Serial.println(Udp.remotePort());
  111.  
  112.     // read the packet into packetBufffer
  113.     Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
  114.     Serial.println("Contents:");
  115.     Serial.println(packetBuffer);
  116.  
  117.     // send a reply, to the IP address and port that sent us the packet we received
  118.     Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  119.     Udp.write(ReplyBuffer);
  120.     Udp.endPacket();
  121.   }
  122.   delay(10);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement