Python Forum
Hex and serial variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hex and serial variable (/thread-16029.html)



Hex and serial variable - deal0703 - Feb-11-2019

Hi all,

I am new to pyhton and pyserial and got a question please.

I got a file with hex code

final_CW
b'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'

I'd like to send this array to the serial port.


f=open("final_CW","r")
CW = f.read()
print("Send CW")
f.close()
ser.write(CW)
I'd like to send

CW = b'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'
ser.write(CW)
I am getting the following error if I read from file.

File "x.last", line 8049, in <module>
ser.write(CW)
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 518, in write
d = to_bytes(data)
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: "b' \\xEA\\x10\\xC6\\x26\\x4B\\x37\\xE4\\x87\\xC3\\x2E\\xB2\\xB6\\x9F\\x07\\x 3D\\x2D\\xC6\\x30'\n"

Thanks for any help.


RE: Hex and serial variable - deal0703 - Feb-13-2019

Anyone ?
Bump


RE: Hex and serial variable - Larz60+ - Feb-13-2019

try: CW = r'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'


RE: Hex and serial variable - deal0703 - Feb-13-2019

Thanks for the reply.

Manually sending
CW = b'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'
ser.write(CW)


works fine.

But I need this stored in a variable.
I don't understand how to read this string from a text file and pass it on like above ?


RE: Hex and serial variable - Larz60+ - Feb-13-2019

Re-examine my post.
i suggested:
CW = r'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'
'r' not 'b'


RE: Hex and serial variable - deal0703 - Feb-14-2019

Thanks but same error

final_CW
r'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'

Traceback (most recent call last):
File "CW", line 81, in <module>
ser.write(CW)
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 518, in write
d = to_bytes(data)
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: "r'\\xEA\\x10\\xBF\\x85\\xB6\\xFA\\x4D\\x9D\\x3F\\x29\\x2B\\x2B\\x64\\xBA\\x75\\x8E\\xC3\\xC6'\n"

root@raspberrypi:~# cat final_CW
r'\xEA\x10\x1E\x87\xB2\x57\xEA\xED\x27\xFE\xF4\x38\x1B\x47\xD2\x41\x00\x13'
In bash it would be like this

cat final_CW
b'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'
CW=cat final_CW
echo $CW
b'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'
echo "A = $CW"
A = b'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'

and than ser.write(A)


RE: Hex and serial variable - DeaD_EyE - Feb-14-2019

f=open("final_CW","r")  # <== You open the file in text mode interpreted as utf8
CW = f.read()
print("Send CW")
f.close()
ser.write(CW)
Just open the file with open(file, 'rb') in binary mode, then you get bytestrings back, if you read.


RE: Hex and serial variable - deal0703 - Feb-14-2019

Thanks for the reply

That doesn't show the error anymore, but the device now receives
72 27 5C 78 45 41 5C 78 31 30 5C 78 43 44 5C 78 31 35
instead of
EA 10 C6 26 4B 37 E4 87 C3 2E B2 B6 9F 07 3D 2D C6 30


RE: Hex and serial variable - deal0703 - Feb-17-2019

I am trying to understand why this doesn't work ?

If I use a shell script with sed to read
final_CW
b'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'

and replace
CW=cat final_CW
cat Mypythonscript | sed "s/REPLACE/${CW}/g" > final_CW1

like this
CW = REPLACE
ser.write(CW)

python3.5 final_CW1
it works fine.