代码之家  ›  专栏  ›  技术社区  ›  Sunil Jose

Python:写入串行端口as/x06

  •  0
  • Sunil Jose  · 技术社区  · 8 年前

    我需要将06作为/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
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Ignacio Vazquez-Abrams    8 年前

    你需要改变 '06' '\x06' .

        2
  •  0
  •   Fejs Do It    8 年前

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

    这是 docs