[Python] Python UDP communication stalled

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to implement Pyhton to send and receive data. Before I started with the Python implementation I checked that what I wanted to do was working properly. A device connected to my computer via UDP sends one-byte data like 0xFF to the computer. The Computer can send data via UDP as well, which is processed by the device. This was implemented and verified with both Wireshark and the UDP Test Tool. I want to achieve the same thing with Pyhton.

    With my Python implementation, I'm having trouble reading data. On the other side, I'm able to send data to the device. The problem I'm currently facing with receiving data stalling any data transmitted over UDP (from both the device and the computer). The only thing I can do to get the communication running again is with the UDP tool sending data. Not sure where the problem is, could someone familiar with Python and the Socket Module and UDP communication have a look and help me get this running?

    import socket

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    sock.bind(("0.0.0.0", 6000))

    receiver_ip = "192.168.0.2"
    reveiver_port = 5000

    while True:
    sock.sendto(bytes([0x11]), (receiver_ip, reveiver_port))
    print("Send Data:", ' '.join(f"0x{b:02X}" for b in bytes([0x11])))
    data, addr = sock.recvfrom(1)
    sender_ip, sender_port = addr
    print("Received Data:", ' '.join(f"0x{b:02X}" for b in data))
    print(sender_ip)
    if data == bytes([0xFF]) and sender_ip == "192.168.0.2" and sender_port == 6000:
    send_data = bytes([0xAB])
    print("Received Data:", ' '.join(f"0x{b:02X}" for b in data))
    sock.sendto(send_data, (receiver_ip, reveiver_port))
    print("Send Data:", ' '.join(f"0x{b:02X}" for b in bytes([0xAA])))
    sock.shutdown()

    Continue reading...

Compartilhe esta Página