UDP Packets can be visualized in Wireshark but unable to read using Python script

116 Views Asked by At

I'm sending data from a microcontroller (MCU) to a PC via ethernet port I can successfully verify the data transmission using Wireshark, but my Python script is unable to read it.

I'm sending data to MCU using python script (which is working fine) and expecting response from MCU (Microcontroller)

Response from MCU is transmitted to PC using Ethernet and data can be seen in Wireshark

Configuration for IPv4 in PC

IPv4 Address : 192.168.0.3 Subnet Mask : 255.255.255.0 Default gateway : 192.168.0.1

Configuration for Microcontroller

#define ETHERNET_CONF_IPADDR0                         192
#define ETHERNET_CONF_IPADDR1                         168
#define ETHERNET_CONF_IPADDR2                         0
#define ETHERNET_CONF_IPADDR3                         2


#define ETHERNET_CONF_GATEWAY_ADDR0                   192
#define ETHERNET_CONF_GATEWAY_ADDR1                   168
#define ETHERNET_CONF_GATEWAY_ADDR2                   0
#define ETHERNET_CONF_GATEWAY_ADDR3                   250

/** The network mask being used. */
#define ETHERNET_CONF_NET_MASK0                       255
#define ETHERNET_CONF_NET_MASK1                       255
#define ETHERNET_CONF_NET_MASK2                       255
#define ETHERNET_CONF_NET_MASK3                       0

Script

import socket

def receive_udp_data(host, port):
    # Create a UDP socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Bind the socket to the specified host and port
    udp_socket.bind((host, port))
    print(f"Listening for UDP data on {host}:{port}")

    try:
        while True:
            # Receive data from the socket
            data, addr = udp_socket.recvfrom(1024)

            # Print the received data
            print(f"Received data from {addr}: {data.decode('utf-8')}")

    except KeyboardInterrupt:
        print("Stopping UDP listener...")
    finally:
        # Close the socket when done
        udp_socket.close()

if __name__ == "__main__":
    # Set the host and port to listen on
    host = "192.168.0.3"  # Listen on all available interfaces
    port =1234  # Choose a suitable port

    # Start listening for UDP data
    receive_udp_data(host, port)
0

There are 0 best solutions below