I have a LIDAR device connected to my home PC. The LIDAR has an IP of 192.168.1.201. I have configured it to give its data out on port 2368. My PC has an IP of 192.168.1.202. I have confirmed on wireshark that there are indeed UDP packets on the network with a source of 192.168.1.201 that have a destination of 192.168.1.202. In this situation i believe my "server" is the LIDAR (broadcasting packets) and my client is my PC (which i am trying to write python code to receive these packets on). I am using Spyder launched from Anaconda to develop this code on a windows PC. My python version is 3.9.
I understand i must use the socket library in python to do this. hence my program is as follows:
import socket
UDP_IP = "192.168.1.201" # This is my LIDAR
UDP_PORT = 2368 #Port i expect to see data on.
# create a socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# bind the socket to a specific IP address and port
sock.bind((UDP_IP, UDP_PORT))
print(f"Listening for UDP packets from {UDP_IP} on port {UDP_PORT}...")
# receive data until the program is interrupted
while True:
data, addr = sock.recvfrom(1024)
print(f"Received packet from {addr}: {data.decode('utf-8')}")
This code gives OSError: [WinError 10049] The requested address is not valid in its context Now my network understanding is shaky, so i change the bind command IP to my PC IP (192.168.1.202). This now gets rid of that error, however now the program will hang in the while loop. It does not print data. I have done some reading and understand that this might be something to do with port blocking.
I now add the sock.setblocking(0) command underneath the bind command. This generates the error: BlockingIOError: [WinError 10035] A non-blocking socket operation could not be completed immediately. Adding in time.sleep(2.0) does not get rid of this error. My program now looks like this:
import socket
import time
UDP_IP = "192.168.1.202" # This is my LIDAR
UDP_PORT = 2368 #Port i expect to see data on.
# create a socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# bind the socket to a specific IP address and port
sock.bind((UDP_IP, UDP_PORT))
sock.setblocking(0)
time.sleep(2.0)
print(f"Listening for UDP packets from {UDP_IP} on port {UDP_PORT}...")
# receive data until the program is interrupted
while True:
data, addr = sock.recvfrom(1024)
print(f"Received packet from {addr}: {data.decode('utf-8')}")
Can anyone help me to get this working, given the info provided? I can provide more detail if required. It does not need to be complicated, i would be happy to simply print packets in the console window.
I have tried as per the above.