为什么我的 'for' 循环没有循环?

Why is my 'for' loop not looping?

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

尽管 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:-Thema\Programmieren\projects\robot\remote-control-scripts>python test.py
PROMPT >> GO!
The response is:
39
 bytes long
['X']
PROMPT >>

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

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

这是一个文档link