-1

I am receiving UDP packets over wifi running a simple python script on a PC. The server and the PC are in the same subnet.

The server is sending 15 uint_8 (4 bytes each) every 20 ms or so. The data received seems to be corrupted (non Hex values). Any feedback why this could be happening greatly appreciated. For example I get something like this,

'\xb3}fC\xb7v\t>\xc8X\xd2=g\x8e1\xbf\xe6D3\xbf\x00\x00\x13\xc3\xc8g\x1b@\xc2\x12\xb2B\x01\x000=\x02\xc0~?\x01\x00\x94<\x00\x00\x00\x00\x00\x00\x00\x00\x00
@\x9c\xbe\xac\xc9V@', ('192.168.4.1', 4097))

The script is attached here.

from socket import *
import time

HOST = '192.168.4.10'
PORT = 9048

address = (HOST, PORT) 
client_socket = socket(AF_INET, SOCK_DGRAM) #Set Up the Socket
client_socket.bind((HOST, PORT)) 
client_socket.settimeout(5) #only wait 5 second for a response, otherwise timeout

while(1): #Main Loop
    single_var = client_socket.recvfrom(1024)
    print single_var #Print the response from Arduino
    time.sleep(10/1000000) # sleep 10 microseconds
Grisha Levit
  • 8,194
  • 2
  • 38
  • 53
Venkat
  • 1
  • 1
  • If you using python 3.X you should decode data -> `single_var.decode('utf-8')` , be cause message are in binary represntation – ᴀʀᴍᴀɴ Jan 21 '17 at 23:43
  • Just a note: `uint8` (`uint8_t` in C language specification) is one byte, not 4. A `uint32_t` is four bytes. The byte order ("big-endian" [traditional "network order"] versus "little-endian") will become important as you try to interpret those bytes representing multi-byte values. – Gil Hamilton Jan 21 '17 at 23:58

1 Answers1

1

The print statement doesn't know that you want hex output, so it interprets hex values that have valid character representations as characters. If you want to print it as hex bytes, see the solution in Print a string as hex bytes.

i.e. do:

print ":".join("{:02x}".format(ord(c)) for c in single_var)
Community
  • 1
  • 1
Grisha Levit
  • 8,194
  • 2
  • 38
  • 53