Ethernet shield not receiving UDP packets correctly

I’m using an Adafruit Ethernet FeatherWing plugged into an Adafruit Feather 328P and I want to send and receive UDP packets from a Python application. I’m using the stock Arduino UDP code just to see what I’m sending. Here’s my Python code:

def write_Arduino(self):
    sock_readresp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock_readresp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock_readresp.bind((self.config["DEFAULT"]["THIS_IP_ADDRESS"], int(self.config["DEFAULT"]["RECEIVE_PORT"] )))
    sock_readresp.settimeout(.2)

    MESSAGE = struct.pack("30c", b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9',
                              b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9',
                              b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9')

    print("Message is {}".format(MESSAGE))
    sock_read = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock_read.setblocking(0)
    sock_read.bind((self.config["DEFAULT"]["THIS_IP_ADDRESS"], 0))
    sock_read.sendto(MESSAGE,(self.config["DEFAULT"]["ARDUINO_IP_ADDRESS"],int(self.config["DEFAULT"]["SEND_PORT"])))
    sock_read.close()

My settings are:

THIS_IP_ADDRESS = 192.168.121.1
ARDUINO_IP_ADDRESS = 192.168.121.2
SEND_PORT = 8888
RECEIVE_PORT = 32001

and I’ve updated the Arduino code to reflect that. When I send this packet, I can confirm it through Wireshark on my PC as in the attachment. I seem to be sending exactly what I think I am. A string of “012345678901234567890123456789” (Wireshark shows the ASCII characters in hex, as seen here). However, receiving it on the Arduino looks like this:

Received packet of size 30
From 192.168.121.1, port 64143
Contents:
012345678901234567890123D6789

The 25th and 26th byte always show up like that, and I’m missing the actual data. What could be going on here?

Two issues, firstly the default receive buffer size is too small for the amount of data you are sending.

Secondly, the demo program you are using is designed to receive null terminated data whereas your data doesn't have a null at the end.

To fix, increase the size of the receive buffer, and print single characters from the receive buffer in a loop rather than passing the whole buffer to println.

mikb55:
Two issues, firstly the default receive buffer size is too small for the amount of data you are sending.

Secondly, the demo program you are using is designed to receive null terminated data whereas your data doesn't have a null at the end.

To fix, increase the size of the receive buffer, and print single characters from the receive buffer in a loop rather than passing the whole buffer to println.

null terminated data over network? no. the size is sent

The network doesn't care, but this line does:

Serial.println(packetBuffer);

Pieter

PieterP:
The network doesn't care, but this line does:

Serial.println(packetBuffer);

Pieter

but sending it null terminated will not help on receiver side.
the example is wrong. with this and with the strange UDP_TX_PACKET_MAX_SIZE define

I created an issue for the define UDP_TX_PACKET_MAX_SIZE used only in UDPSendReceiveString.ino example as size of RX buffer · Issue #109 · arduino-libraries/Ethernet · GitHub

EDIT: sorry. if the 0 is sent as part of data, it will work with println

Thanks for your point Juraj. I don't really need UDP_TX_PACKET_MAX_SIZE, I just used "100" and everything works fine. Thanks again.