I'm communicating over a serial port, and currently using python2 code which I want to convert to python3. I want to make sure the bytes I send over the wire are the same, but I'm having trouble verifying that that's the case.
我正在通过一个串行端口进行通信,目前使用的是要转换为python3的python2代码。我想确保我通过电线发送的字节是相同的,但是我在验证这一点上有困难。
In the original code the commands are sent like this:
在原始代码中,命令是这样发送的:
serial.Serial().write("\xaa\xb4" + chr(2))
If I print "\xaa\xb4"
in python2 I get this: ��.
If I print("\xaa\xb4")
in python3 I get this: ª´
如果我打印“\ xaa \ xb4”python2我得到这个:��。如果我打印(“\ xaa \ xb4”)python3我得到这个:ª´
Encoding and decoding seem opposite too:
编码和解码似乎也相反:
Python2: print "\xaa".decode('latin-1')
-> ª
Python3: print("\xaa".encode('latin-1'))
-> b'\xaa'
Python2:打印" \ xaa " .decode(latin - 1)- >ªPython3:打印(" \ xaa " .encode(latin - 1))- > b \ xaa”
To be crude, what do I need to send in serial.write()
in python3 to make sure exactly the same sequence of 1
s and 0
s are sent down the wire?
为了更简单,我需要用python编写的write()来确保相同序列的1和0被发送到电线上?
1 个解决方案
#1
1
Use a bytes
sequence.
使用一个字节序列。
ser.write(b'\xaa\xb4')
#1
1
Use a bytes
sequence.
使用一个字节序列。
ser.write(b'\xaa\xb4')