Catégories
Liens
Ceci est une ancienne révision du document !
Avec Pure Data, Arduino, Raspberry Pi, Teensy, …
Code Arduino
// TEST RECEPTION/ ENVOI UDP #include <SPI.h> #include <Ethernet.h> #include <EthernetUdp.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 102); unsigned int localPort = 8888; char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, char ReplyBuffer[] = "acknowledged"; // a string to send back EthernetUDP Udp; void setup() { Ethernet.begin(mac,ip); Udp.begin(localPort); Serial.begin(9600); } void loop() { 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.beginPacket(Udp.remoteIP(), 5000); Udp.write(ReplyBuffer); Udp.endPacket(); } delay(10); }
Code Pure Data