Using pyserial to send binary data

I know that there has been a lot of discussion on this, but I still have a question. I am trying to transfer hex values ​​through pyserial to my device using pyserial

command="\x89\x45\x56" ser.write(command) 

However, I keep getting the string argument without encoding. error message string argument without encoding. Does anyone know how to solve this?

+6
source share
4 answers

If it's Python 3, it probably treats your string as unicode and doesn't know how to convert it. I think you are probably going to use bytes here:

 command=b"\x89\x45\x56" 
+1
source

If you are using Python 3, you can use the bytes object.

 command=b"\x89\x45\x56" 

Due to the error, it looks like pyserial is trying to convert (your) string to a byte object without specifying an encoding.

+1
source

I had a successful dispatch of hexadecimal values ​​from a string as follows:

 input = '736e7000ae01FF' ser.write(input.decode("hex")) print "sending",input.decode("hex") >> sending snp «☺ 
+1
source
 packet = bytearray() packet.append(0x41) packet.append(0x42) packet.append(0x43) ser.write(packet) 
0
source

Source: https://habr.com/ru/post/949206/


All Articles