Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hex and serial variable
#1
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.
Reply
#2
Anyone ?
Bump
Reply
#3
try: CW = r'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'
Reply
#4
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 ?
Reply
#5
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'
Reply
#6
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)
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
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
Reply
#9
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,292 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  assign the variable to every value received serial from Arduino barry76 4 3,283 Feb-01-2019, 10:19 AM
Last Post: barry76

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020