Bottom Page

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.
Quote
#2
Anyone ?
Bump
Quote
#3
try: CW = r'\xEA\x10\xC6\x26\x4B\x37\xE4\x87\xC3\x2E\xB2\xB6\x9F\x07\x3D\x2D\xC6\x30'
Quote
#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 ?
Quote
#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'
Quote
#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)
Quote
#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.
My code examples are always for Python >=3.6.0
All humans together. We don't need politicians!
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to store the value from variable into list & run a statement then put in variable searching1 0 17 2 hours ago
Last Post: searching1
  Trying to set an instance variable to current value of a class variable ScottDiesing 3 40 11 hours ago
Last Post: ScottDiesing
  Post serial results BMC 0 43 Feb-11-2019, 04:30 PM
Last Post: BMC
  Serial Communication Error AlphyOuseph 1 62 Feb-07-2019, 07:16 AM
Last Post: DeaD_EyE
  Writing commands to serial python_beginner 7 9,199 Feb-03-2019, 09:03 AM
Last Post: jambuna35
  assign the variable to every value received serial from Arduino barry76 4 85 Feb-01-2019, 10:19 AM
Last Post: barry76
  Python Read/Write serial dlizimmerman 0 126 Jan-31-2019, 03:12 AM
Last Post: dlizimmerman
  Reading data from serial port as byte array vlad93 0 171 Jan-08-2019, 08:52 AM
Last Post: vlad93
  Receive Serial Data and store in different Variables in Python jenkins43 5 279 Dec-28-2018, 01:33 PM
Last Post: snippsat
  I need a serial device expert! Caesars_10th 10 344 Dec-18-2018, 10:33 PM
Last Post: jeanMichelBain

Forum Jump:


Users browsing this thread: 1 Guest(s)