Accessing UDP port data

Hey,
First off, I’m an absolute newbie when it comes to networking.
I’ve been messing with FreePIE’s android IMU recently and been trying to access it’s data from python where it uses a UDP port (5555). I’ve tried various functions in the Socket module, they all ended up by either freezing blender or conflicting with each other. So for example:


host = "192.168.2.6"
port = "5555"
socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket.bind((host, port))
socket.settimeout(0.1) #when removed, blender freezes
print(socket.recvfrom(1024)) #causes a connection timeout error

Is there any working example that can point me correctly? Thanks!

All new sockets start in blocking mode by default. This means that any call to recv/recvfrom will block (i.e. not return when you call them) until there is some data for them to return. Calling settimeout(0.1) causes the recvfrom call to return and raise a time out error if no data is recieved in 0.1 seconds. You can handle this with a try except clause. I would recommend doing some reading on asynchronous socket operations before you continue.

Thanks Mobious!

Well I got it working (it was a stupid mistake in the host), but all I get is hashed numbers. Well they’re correct as they change when freePie’s tracking is active.
Example:
output of print(socket.recvfrom(1024)) :
b’\x00\x02\x00\x00\x94\xbb(\xf3\xb1B:\xa8Q?’

Those aren’t hashed numbers, they’re raw bytes as indicated by the b prefix. You will always get bytes when receiving from sockets. In order to properly parse the data, you need to know what’s actually being sent from the other end. Having no experience using freePIE, I can’t tell you how the data is formatted, but I’m sure you can find out by doing some reading or looking at the code doing the sending.