Ethernet UDPSendReceiveString bugfix?

I have been playing around with the W5100 shield and UDP and stumbled across a bug in the UDPSendReceiveString example located:

\arduino-1.0.4\libraries\Ethernet\examples\UDPSendReceiveString

Problem:
Of you send a UDP packet of say XXXXXXXXXX then immediately afterwards send a packet of YYYYY the packetBuffer char array retains the last packet contents, only overwriting the first 5 elements. So printing out packetBuffer after the second receive yields YYYYYXXXXX.

Fix:
To fix this you either need to print out packetBuffer to the length of packetSize or zero out the array before or after each use.

The easiest way is to add the line:
memset(packetBuffer, 0, sizeof packetBuffer);
or

memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);

Loop listing:

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());

    // Zero out the packetBuffer
    memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); // ********** Added ************ //
    // 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);
}

It may help someone.

thanks

thanks,
memset it is a EthernetUdp library command ?

memset it is a EthernetUdp library command ?

No. It's a core C function.

karma +1 :wink:

I hope it doesn’t seem like I’ve “High Jacked” this thread, but I can’t get the UDPSendReceiveString example to work. (or any UDP examples I have found for that matter). I don’t know if it is my Arduino IDE (1.0.5) sketch config, or if it is MY Processing (2.01) setup.

Adruino Sketch Setup:

I edit the IPAdress with a valid static IP from my router range (192.168.1.201) and I leave the local port at 8888.
Send/compile to UNO. No errors other than the line “#include <EthernetUdp.h>” is all in BLACK. The other include library statements, SPI and Ethernet are in ORANGE. Does this mean anything. Do I need to include a path or add the library EthernetUdp?

I’ve never done anything with Processing so this is new territory for me:

I edit this line to include my UNO address. String ip = “192.168.1.201”; // the remote IP address

It seems to run OK. Hitting the RUN command a little Grey Window pops up.

No amount of text entry into the Arduino Terminal echos back to the Processing app, and the “Hello World” doesn’t get sent to the UNO.

I can Ping my Uno at 192.168.1.201. My lap top is on 192.168.1.49 via DHCP and Wifi. I have turned off my routers Firewall.

Anything obviously wrong here? Any suggestions as to how I might determine which side isn’t working. Are there command line UDP test I can do to see if the UNO will accept a UDP receive?

After I posted, I played a bit more and I discovered my problem is with the Processing part of the example. I searched for a command line UDP send and like all good things in computing, Linux came through for me. I found a cool command line linux app called socat.

After doing a sudo get app for socat I did what this guy from the WEB suggested:

echo “HELLO” | socat – UDP-DATAGRAM:192.168.0.255:5000,broadcast

Checkout some examples:
http://thekissinglink.blogspot.com/2010/01/socat-examples.html

From the linux command line it sent the message to my Arduino. Now to figure out what I am doing wrong in Processing :slight_smile:

thanks for this post !
I hit on this problem and the memset line solved it !
Thanks again :slight_smile: