UDP Communication Errors

Previously I posted this topic under "Microcontrollers", with no feedback so far. I think "Networking, Protocols, and Devices" is more appropriate.

I need to transmit messages of a few hundred bytes continuously between two arduinos.
I conducted some Ethernet UDP tests and I got some strange results:
The setup is:

  • ARDUINO UNO connected to ARDUINO MEGA through Ethernet
  • With Ethernet shield on each one (including W5100), SPI 4 Mbits or 8 Mbits
  • Using Ethernet Library,
  • UNO transmitting continuously UDP packets of 128 bytes
  • MEGA receiving the packets and printing out a message every time a particular byte within the packet is different from the transmitted value.
    Results: An error message within an average of every 50 packets.
    What is more interesting is that:
  • The error rate for a particular byte in the message increases with the position of this byte in the message. For example, if for byte #40 the error rate is 1 every 50 messages, for byte #100 the error rate is 1 out of only 20 messages.
  • The reading of these error values occurs at the same messages. For example if we order the transmitted messages from start with 0, 1, .....the errors would occur always at messages 8, 120, 180, ...

I would appreciate any comments about these results, including any idea for decreasing those error rates.

Are you numbering the packets so you are certain the packets are all arriving and in the correct order?

Yes, there is a packet counter in one of the bytes in the transmitted buffer. If I focus on one of the bytes, I can see that for this byte the error occurs at some packet numbers and that repeats after each program restart.
OK, I need to record these Packet numbers at the receive side and check them for missing packets or for wrong order. and I will report in this post.
Thank you


If you are still having problems, post your code.

OK, I attached 3 files:

  • The UDP Write sketch
  • The UDP READ sketch
  • A printout of the ARDUINO Monitor for different transmit Buffer sizes


  • In EthernetUDP.h file, changed UDP_TX_PACKET_MAX_SIZE from 24 to 2046
  • In w5100.h file, changed SPI_ETHERNET_SETTINGS SPISettings from 14000000 clock to 4000000 clock
  • In w5100.h file, changed SPI_ETHERNET_SETTINGS ETHERNET_SHIELD_SPI_CS,SPISettings to 4000000 clock

As you can see in the results file, the number of corrupted messages increases with the size of the transmitted message.

Thank you for willingness to assist in this issue.

Ethernet_write_example_to_ARD.ino (1.34 KB)

Ethernet_read_example_to_ARD.ino (2.07 KB)

Result File.doc (42.5 KB)

UDP is an connectionless unreliable protocol - if you require reliable communications either use TCP or add your own error correction to UDP

Check the return value of endPacket. If ‘1’, send was successful. If ‘0’, send failed.

// change this
    int b=Udp.endPacket();//Serial.println(b);

// to this
    int b=Udp.endPacket();

Thank you for this feedback.
Actually I remember I made this test and all endPackets returned a "1". Anyways I repeated the test now. For 600 consecutive messages, all endPackets returned "1".

Sounds like it is working ok then. Since the devices are both on the same localnet, the destination device is receiving all packets. You might want to print only messages when the return value is '0' (fail).

Although all returned endPacket values are 1’s, there are errors in the received packets.
At the receiving side, all packets are received (Packet size is OK), but the contents have errors. Number of Errors for a byte in the packet increases with its position within the packet: bytes closer to end of packet have more errors than the ones closer to beginning of packet.

if you are running on a local network the possibility of UDP corruption is low
I would always add a CRC check to a UDP packet so I check if any data loss or corruption is in the transmitter or receiver software or the transmission over the network

The transmitting ARDUINO is connected to the receiving ARDUINO by a direct ethernet cable.
I am monitoring directly received data by printing some received bytes in the received packet. And I see errors. The endPacket instruction at the transmitting side returns always a 1, meaning that the packets transmission was successful.

are you sure you are transmitting valid data and not some area of memory which contains random data
I suggest printing the datagram contents on both the transmitter and receiver so can you show us examples of the data you transmitted and received?
also you could monitor the ethernet with a package such as Wireshark which would enble you to examine the datagram contents

Your UDP packets may be outrunning your serial output. The serial buffer may be the reason you are losing characters.

// change this

// to this

Can I use Wireshark to monitor communication between 2 ARDUINOs? I understand that Wireshark runs on Windows.
One thing I forgot to mention: If I use packets including 8 bytes of data, I do not obserne a single error in thousands of packets!!

Did I mention that if you send twice the data in each packet, you could be losing data on the serial end? Did you increase the serial speed?

if you are using WiFi or ethernet to link the Arduinos connect the PC to the same network
I have used wireshark to monitor Ethernet networks connecting multiple Microchip PIC24 and dsPIC microprocessors

If small packets work OK it could be that you are overrunning the serial communications as mentioned by SurferTim - what baud rate are you using ?

Good news!!
I just received Ethernet Shields with W5200. I downloaded a software library for W5200.

With thes shiels, there are no errors (checked with in a loop of 5000 messages of 1024 bytes each). Bit rate is about 6 times better.
Really the W5100 is very slow.
Thank you for your help.

Now I get to the size of transmitted packets. I need to transmit packets of up to 2000 bytes. I noticed that all packets up to a size of exactly 1472 pass with no problem. Beyond this size, for a size of x, parsePacket function returns the difference between x and 1472, meaning that only the bytes 1473, 1474,.. x are received.
I have set the UDP_TX_PACKET_MAX_SIZE in Udp.h to 2000, it did not help.

Thanks for any idea for solving this issue.


The maximum size of a UDP packet is about 1492 bytes. There is a header involved also.

Can it be changed through the Ethernet library files?