Python specific packet loss using UDP

58 Views Asked by At

This is an odd one.

I've got a microcontroller on the local network, that is sending UDP data on the broadcast address (192.168.1.255). It sends a response to commands (also sent via UDP), and then churning out a datagram every 5 seconds.

The commands are sent over UDP to 23451, and the data & command response is recieved on 12345.

Using a third party program, I can see the commands, response, and data all being recieved on my machine.

However, with the code below, I can always recieve the data, but I can never recieve the command response.

import socket


response_port = 12345
response_sock = socket.socket(socket.AF_INET, # Internet
                        socket.SOCK_DGRAM) # UDP
response_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Set the SO_REUSEADDR option to True, so we don't get an error when we try to bind to the same port from another program
response_sock.bind(('', response_port))

while True:
    datagram, addr = response_sock.recvfrom(1024)
    print(addr, " : ",datagram)

The response to command (which I can't recieve) looks like: UDP-SPI_0002 in ascii, or 55 44 50 2D 53 50 49 5F 30 30 30 32 00 in hex

The data (which I can recieve) looks like: 33 36 31 34 19 00 0B 00 06 00 02 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15 18 00 00 F1 00 60 02 E2 64 44 58 D9 46 69 3F A5 29 EA 3F E8 A4 F5 3F 00 00 7E 00 37 00 07 00 00 00 79 02 10 4A

The difficulty I'm having is that the data is always recieved, but the command response is never recieved. Things I have tried:

  • Disable the data transmission, and only send the command responses, no effect.
  • Insert delays between setting up the recvfrom and sending the command
  • Put the recvfrom in a separate thread

It's a local network, with no other machine, and no internet access. To confirm, I can reliably see the data and the command response from another tool (https://udp-test-tool.informer.com/) I'm using Windows 10, Python 3.12

0

There are 0 best solutions below