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
Almost dead, but too lazy to die: https://sourceserver.info
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
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Serial to telnet using Python Charles_Linquist 2 352 Jun-17-2019, 04:37 PM
Last Post: DeaD_EyE
  Reading mixed ASCII/binary serial input N8UR 1 303 Jun-11-2019, 02:49 PM
Last Post: N8UR
  How to store the value from variable into list & run a statement then put in variable searching1 1 291 May-29-2019, 06:36 AM
Last Post: heiner55
  Reading data from serial port as byte array vlad93 1 765 May-18-2019, 05:26 AM
Last Post: heiner55
  Naming a variable with the str value and another variable SheeppOSU 2 273 Apr-21-2019, 07:18 PM
Last Post: ichabod801
  How can I only receive data from selected serial numbers MasterCATZ 7 339 Apr-20-2019, 08:35 AM
Last Post: MasterCATZ
  Multiple process access to a serial port mkonnov 0 197 Apr-14-2019, 12:42 PM
Last Post: mkonnov
  Trouble writing over serial EngineerNeil 1 275 Apr-07-2019, 08:17 PM
Last Post: j.crater
  Serial communication with raspberry pi 3B and Xbee kj7 0 269 Mar-25-2019, 03:39 AM
Last Post: kj7
  Serial USB problem DPaul 3 396 Feb-21-2019, 02:18 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)