Python:以/x06格式写入串行端口

Python:以/x06格式写入串行端口,python,pyserial,Python,Pyserial,我需要将06 as/x06写入串行端口以确认机器,我的代码如下所示,需要进行哪些更正 import time import serial ser = serial.Serial(port='COM1',baudrate=9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1) counter=0 while 1: ser.write('06')

我需要将06 as/x06写入串行端口以确认机器,我的代码如下所示,需要进行哪些更正

import time 
import serial

ser = serial.Serial(port='COM1',baudrate=9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)
counter=0

while 1:
    ser.write('06')
    time.sleep(1)
    counter += 1
    x=ser.readline()
    print x

您需要将
'06'
更改为
'\x06'
您需要将
'06'
更改为
'\x06'
尝试使用:

ser.write("06".encode())
以下是解决方法。

尝试使用:

ser.write("06".encode())

这是我执行“06”时的。

。encode()显示为输出2未生成\x06,这意味着它向串行端口发送了2个字节。尝试发送更长的字符串。我以ser.write(“\x06.encode())的形式发送了它,并且工作了。。。谢谢当我执行“06”时。encode()显示为输出2未生成\x06,这意味着它向串行端口发送了2个字节。尝试发送更长的字符串。我以ser.write(“\x06.encode())的形式发送了它,并且工作了。。。谢谢