PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

140 lines
3.7KB

  1. /*
  2. UDPSendReceiveString:
  3. This sketch receives UDP message strings, prints them to the serial port
  4. and sends an "acknowledge" string back to the sender
  5. A Processing sketch is included at the end of file that can be used to send
  6. and received messages for testing with a computer.
  7. created 21 Aug 2010
  8. by Michael Margolis
  9. This code is in the public domain.
  10. */
  11. #include <Ethernet.h>
  12. #include <EthernetUdp.h>
  13. // Enter a MAC address and IP address for your controller below.
  14. // The IP address will be dependent on your local network:
  15. byte mac[] = {
  16. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  17. };
  18. IPAddress ip(192, 168, 1, 177);
  19. unsigned int localPort = 8888; // local port to listen on
  20. // buffers for receiving and sending data
  21. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
  22. char ReplyBuffer[] = "acknowledged"; // a string to send back
  23. // An EthernetUDP instance to let us send and receive packets over UDP
  24. EthernetUDP Udp;
  25. void setup() {
  26. // You can use Ethernet.init(pin) to configure the CS pin
  27. //Ethernet.init(10); // Most Arduino shields
  28. //Ethernet.init(5); // MKR ETH shield
  29. //Ethernet.init(0); // Teensy 2.0
  30. //Ethernet.init(20); // Teensy++ 2.0
  31. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  32. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  33. // start the Ethernet
  34. Ethernet.begin(mac, ip);
  35. // Open serial communications and wait for port to open:
  36. Serial.begin(9600);
  37. while (!Serial) {
  38. ; // wait for serial port to connect. Needed for native USB port only
  39. }
  40. // Check for Ethernet hardware present
  41. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  42. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  43. while (true) {
  44. delay(1); // do nothing, no point running without Ethernet hardware
  45. }
  46. }
  47. if (Ethernet.linkStatus() == LinkOFF) {
  48. Serial.println("Ethernet cable is not connected.");
  49. }
  50. // start UDP
  51. Udp.begin(localPort);
  52. }
  53. void loop() {
  54. // if there's data available, read a packet
  55. int packetSize = Udp.parsePacket();
  56. if (packetSize) {
  57. Serial.print("Received packet of size ");
  58. Serial.println(packetSize);
  59. Serial.print("From ");
  60. IPAddress remote = Udp.remoteIP();
  61. for (int i=0; i < 4; i++) {
  62. Serial.print(remote[i], DEC);
  63. if (i < 3) {
  64. Serial.print(".");
  65. }
  66. }
  67. Serial.print(", port ");
  68. Serial.println(Udp.remotePort());
  69. // read the packet into packetBufffer
  70. Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  71. Serial.println("Contents:");
  72. Serial.println(packetBuffer);
  73. // send a reply to the IP address and port that sent us the packet we received
  74. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  75. Udp.write(ReplyBuffer);
  76. Udp.endPacket();
  77. }
  78. delay(10);
  79. }
  80. /*
  81. Processing sketch to run with this example
  82. =====================================================
  83. // Processing UDP example to send and receive string data from Arduino
  84. // press any key to send the "Hello Arduino" message
  85. import hypermedia.net.*;
  86. UDP udp; // define the UDP object
  87. void setup() {
  88. udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
  89. //udp.log( true ); // <-- printout the connection activity
  90. udp.listen( true ); // and wait for incoming message
  91. }
  92. void draw()
  93. {
  94. }
  95. void keyPressed() {
  96. String ip = "192.168.1.177"; // the remote IP address
  97. int port = 8888; // the destination port
  98. udp.send("Hello World", ip, port ); // the message to send
  99. }
  100. void receive( byte[] data ) { // <-- default handler
  101. //void receive( byte[] data, String ip, int port ) { // <-- extended handler
  102. for(int i=0; i < data.length; i++)
  103. print(char(data[i]));
  104. println();
  105. }
  106. */