代码之家  ›  专栏  ›  技术社区  ›  Multisync

为什么我的“for”循环没有循环?

  •  0
  • Multisync  · 技术社区  · 10 年前

    以下python脚本应执行以下操作:

    • 邮寄 X1650 Y0 Z0
    • 填充变量 line 逐字节响应

    阿尔托 print (ser.in_waiting) 声明输入缓冲区已正确填充 for 没有对其进行迭代。

    代码:

    import serial
    import time
    
    # configure the serial connections
    ser = serial.Serial(
        port='COM3',
        baudrate=9600,
    )
    
    while 1 :
        # Wait until user presses a key
        eingabe = input("PROMPT >> ")
    
        # Send text string to embedded device
        destination_position = 'X1650 Y0 Z0\r\n'
        ser.write(destination_position.encode('ascii'))
    
        # Wait until embedded device responds
        while ser.in_waiting == 0:
            time.sleep(0.1)
    
        # How long is the response?
        print ('The response is: ')
        print (ser.in_waiting)
        print (' bytes long')
    
        # Traverse through the queue
        line = []
        for c in ser.read():
            line.append(chr(c))
            print(line)
    

    输出:

    D:\7-Thema\Programmieren\projects\robot\remote-control-scripts>python test.py
    PROMPT >> GO!
    The response is:
    39
     bytes long
    ['X']
    PROMPT >>
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   Marko Mackic    10 年前

    必须指定要读取的字节数。但我不知道返回类型,所以当您打印一行时,您会看到:),然后您可以相应地进行转换

    line = ser.read(ser.in_waiting)
    print("%r"%line)
    

    这是一份文件 link