0

我想听串口并每15秒保存一次。但我不能在循环中使用时间。

它给出了如下错误。

文件“serial-reader.py”,第 13 行 timer.start() ^ IndentationError: expected an indented block

我怎么解决这个问题?

    import threading
from contextlib import closing
import serial
counter = 0
continue_looping = True
def stopper():
    global continue_looping
    continue_looping = False

timer = threading.Timer(15, stopper)

while (counter < 9 ):
timer.start()
with open("/Users/macproretina/Desktop/data.txt", 'w') as out_file:
    with closing(serial.Serial('/dev/tty.usbmodem1411', 9600, timeout=1)) as ser:
        while continue_looping:
            line = ser.readline()   # read a '\n' terminated line
            out_file.write(line.decode('utf-8'))
            out_file.flush()
            counter = counter +1
4

2 回答 2

2
while True:       # True must be upper-case!
    timer.start() # This is inside a loop so must be indented!
    ...

您会注意到收到的错误消息确切地告诉您它需要缩进。

于 2013-05-24T02:40:17.150 回答
0
import threading
from contextlib import closing
import serial
counter = 0
continue_looping = True
def stopper():
    global continue_looping
    continue_looping = False

timer = threading.Timer(15, stopper)

while (counter < 9 ):
timer.start()
with open("/Users/macproretina/Desktop/data.txt", 'w') as out_file:
with closing(serial.Serial('/dev/tty.usbmodem1411', 9600, timeout=1)) as ser:
    while continue_looping:
        line = ser.readline()   # read a '\n' terminated line
        out_file.write(line.decode('utf-8'))
        out_file.flush()
        counter = counter +1

“而(计数器< 9):timer.start()”

只需在“timer.start()”的fornd中放置一个标签,我认为这应该会有所帮助

于 2015-06-24T07:38:54.630 回答