Python 尝试使用下面的PySerial代码读取串行端口数据,并获取不需要的符号/字符

Python 尝试使用下面的PySerial代码读取串行端口数据,并获取不需要的符号/字符,python,serial-port,pyserial,Python,Serial Port,Pyserial,使用下面的代码,我得到了不需要的符号/字符。例如,在本例中,我应该只接收“boot.last”,而得到“[0;0mboot.last[0m” ) 这些看起来像ANSI转义序列,可能是用来定位光标的。发送端的代码是什么?您在这端输入了什么来获得此响应?例如ESC[0 m是SGR序列,谢谢。我正在研究发送端的情况。 ser = serial.Serial( port='COM1', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.S

使用下面的代码,我得到了不需要的符号/字符。例如,在本例中,我应该只接收“boot.last”,而得到“[0;0mboot.last[0m”

)


这些看起来像ANSI转义序列,可能是用来定位光标的。发送端的代码是什么?您在这端输入了什么来获得此响应?例如ESC[0 m是SGR序列,谢谢。我正在研究发送端的情况。
ser = serial.Serial(
port='COM1',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
ser.isOpen()

print ('Enter your commands below.\r\nInsert "exit" to leave the application.')

userin=1
while 1 :
    # get keyboard input
    userin = input(">> ")
        # Python 3 users
        # input = input(">> ")
    if userin == 'exit':
        ser.close()
        exit()
    else:
        # send the character to the device
        # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
        ser.write(userin.encode('utf-8') + '\r\n'.encode('utf-8'))
        out = ''
        # let's wait one second before reading output (let's give device time to answer)
        time.sleep(1)
        while ser.inWaiting() > 0:
            out += ser.read(1).decode('ascii')

        if out != '':
            
            print(">>" + out)