Hi All,
I have managed to get my program working now using UDP to send a message to a sensor and get a reply.The last major issue i am having is the data i recieve back.

data, addr = sock.recvfrom(782) # buffer size is 782 bytes

This is my line receiving the info. "data" is the Data and "addr" is the ip address where the data came from.

if i do this :-

print ("received message:- ", data,addr)

I get this:-
('received message:- ', '\xff\xff\xff\xff\x00\x00ZZ\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x00\x00\x00\x00\x93n\x10\x00x\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00, ('192.168.60.3', 9008))

I can print the data perfect and thats fine except if i want to pass that "data" into another variable and then print the other variable it prints out complete nonsense.I have tried to encode the data and decode it with no luck always the same rubbish coming out for example:-

    ÿÿÿÿ

These are examples I have tried some give the above values out others dont work at all

            mystring = str(data)

            print(str(data,'Latin_1',errors='strict'))

            print ("My new string is "),mystring

            ascii_text = str(data).encode('string_escape')

            print ("My Ascii String",ascii_text)

(Not all at the same time of course)

I am presuming the buffer is sending bytes but i cannot seem to convert to a string that i can manipulate later

Any suggestions would be gratefully received.

Thanks again

Why should you want to print binary data as string, it is list of bytes, not characters

print ("received message:- ", data,addr)

    print("""received message:- 
    %s
    from %s""" % (data,addr)) # produce same output in Python 2 as in Python 3

Hi PyTony,
Its not that i want to print it out as much as convert the data" into a format that i can manipulate. Inside the "received message \xff\xff........." etc is the values that the sensor is measuring i will need to remove all the "\x" and convert the hex to decimal values to calculate distances that the sensor is reading.

That is only the way it is printed, but they are in binary format allready.

'\xff\xff\xff\xff\x00\x00ZZ\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x00\x00\x00\x00\x93n\x10\x00x\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

>>> d = '\xff\xff\xff\xff\x00\x00ZZ\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x00\x00\x00\x00\x93n\x10\x00x\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> len(d)
61
>>> d[0]
'\xff'
>>> ord(d[0])
255
>>> hex(ord(d[0]))
'0xff'
>>> d = memoryview(d)
>>> d
<memory at 0x00EE49E0>
>>> list(d)
['\xff', '\xff', '\xff', '\xff', '\x00', '\x00', 'Z', 'Z', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x11', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x93', 'n', '\x10', '\x00', 'x', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']
>>> d.tolist()
[255, 255, 255, 255, 0, 0, 90, 90, 0, 0, 0, 0, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 0, 0, 147, 110, 16, 0, 120, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> 

Edited 4 Years Ago by pyTony: memoryview code

Hi,
I think I understand so my last question is what would be the best way to convert that data into something readable and be able to pass the values back to the main script is it a binary to dec convertion or something else?

Thanks again for your help its been invaluble to me as i am relatively new to python and would love to solve this and continue using python

Regards
Deepthought

Hi,
In the message are sensor measurments they are in there with other values that i dont care about i just want to be able to read the particular values i want so basically how do i extract the values i want or convert the message into something else i can use. these values will be then used to plot a graph. At the moment all i can do is read the message I dont understand how to extract the data out of the message i want.
Regards

You must define the types of the fields and use the struct module to access the data in correct format.

Hi,
I have found a conversion within a module call binascii if you import this then within that module you have a command binascii.b2a_qp so by adding this into my code i.e

   import binascii

   mystring =  binascii.b2a_qp(data)

this will convert the bytes (all 782) into a string now I can manipulate the string into a list etc and extract the bits i need and view the results because "mystring" can be printed on output.

Beware of some of the conversions as they will only allow 45 bytes to be converted at once however the one i used converts all 782 that i have.

Regards
DeepThought

d = '\xff\xff\xff\xff\x00\x00ZZ\x00\x00\x00\x00\x00\x00\x00\x00\x11\x01\x00\x00\x00\x00\x00\x00\x93n\x10\x00x\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

from the command

=FF=FF=FF=FF=00=00ZZ=00=00=00=00=00=00=00=00=11=01=00=00=00=00=00=00=93n=10=
=00x=01=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=
=00=00=00=00=00=00=00=00=00

That command completely messes up your string, which does not need nothing done.

This question has already been answered. Start a new discussion instead.