while 循环无限运行。无法使用计时器终止 while 循环
The while loop runs infinite. It is not possible to terminate while loop using timer
我正在尝试打开和关闭来自 Python 的电磁阀。可以在实验开始之前使用信息框定义此切换模式。例如,我可以定义可以发生多少次切换(切换次数)或将发生多长时间(以秒为单位的切换时间)。我向 Arduino 发送两个字节,一个用于通道选择(1 到 8),第二个用于状态(0 或 1)。
切换次数完美。在 while 循环中,我给出了指令 myTime > 0 并且在每次切换期间我将它减一。因此 while 循环将 运行 直到 myTime 或 Number of toggles 变为零。
但是,当我试图从信息框中输入时间时,它变成了一个无限循环。我可以从信息框中给出时间并将其添加到 time.time() 并尝试在 while 循环中比较它,并希望在 time.time() 变得大于所需时间时终止循环。
如何确保我可以使用这两个条件终止 while 循环?
Python code:
from __future__ import absolute_import, division, print_function
import serial
from time import sleep
import struct
from psychopy import core, data, event, gui, visual
import time
try:
arduino = serial.Serial('COM8',19200)
sleep(2)
print("Connection to " + 'COM8' + " established succesfully!\n")
except Exception as e:
print(e)
global command
## create a DlgFromDict
info = {'Observer':'jwp', 'Channel':['1','2','3','4','5','6','7','8'],
'BreathingCycle':4, 'Timer': 0, 'ExpVersion': 1.1, 'Debug Mode': True}
infoDlg = gui.DlgFromDict(dictionary=info, title='TestExperiment',
order=['ExpVersion', 'Observer'],
tip={'Observer': 'trained visual observer, initials'},
fixed=['ExpVersion'])
myChannel = info['Channel']
myTime = info ['BreathingCycle']
myTimer = info ['Timer']
win = visual.Window(fullscr=True, size=(1536, 864), monitor='laptop')
#TODO: handle in a different way the screen resolution
instruction1 = visual.TextStim(win, text=u"""Valve on!""")
instruction2 = visual.TextStim(win, text=u"""Valve off!""")
instruction3 = visual.TextStim(win, text=u"""Thank you!""")
if infoDlg.OK:
Mytimer = time.time() + myTimer
while (myTime > 0 or time.time() < Mytimer):
if myChannel == '1':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',513))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',512))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '2':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',257))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',256))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '3':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',2049))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',2048))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '4':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',1025))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',1024))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '5':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',4097))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',4096))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '6':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',8193))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',8192))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '7':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',32769))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',32768))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '8':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',16385))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',16384))
myTime = myTime - 1
instruction2.draw()
win.flip()
win.close ()
arduino.close()
我找到了解决方案,因为有一些函数 event.waitKeys() 基本上是无限期地等待一个键,这就是为什么 time.time() < Mytimer 条件不可用核实。我不得不在 event.waitKeys() 中给出最长等待时间。
我正在尝试打开和关闭来自 Python 的电磁阀。可以在实验开始之前使用信息框定义此切换模式。例如,我可以定义可以发生多少次切换(切换次数)或将发生多长时间(以秒为单位的切换时间)。我向 Arduino 发送两个字节,一个用于通道选择(1 到 8),第二个用于状态(0 或 1)。
切换次数完美。在 while 循环中,我给出了指令 myTime > 0 并且在每次切换期间我将它减一。因此 while 循环将 运行 直到 myTime 或 Number of toggles 变为零。
但是,当我试图从信息框中输入时间时,它变成了一个无限循环。我可以从信息框中给出时间并将其添加到 time.time() 并尝试在 while 循环中比较它,并希望在 time.time() 变得大于所需时间时终止循环。
如何确保我可以使用这两个条件终止 while 循环?
Python code:
from __future__ import absolute_import, division, print_function
import serial
from time import sleep
import struct
from psychopy import core, data, event, gui, visual
import time
try:
arduino = serial.Serial('COM8',19200)
sleep(2)
print("Connection to " + 'COM8' + " established succesfully!\n")
except Exception as e:
print(e)
global command
## create a DlgFromDict
info = {'Observer':'jwp', 'Channel':['1','2','3','4','5','6','7','8'],
'BreathingCycle':4, 'Timer': 0, 'ExpVersion': 1.1, 'Debug Mode': True}
infoDlg = gui.DlgFromDict(dictionary=info, title='TestExperiment',
order=['ExpVersion', 'Observer'],
tip={'Observer': 'trained visual observer, initials'},
fixed=['ExpVersion'])
myChannel = info['Channel']
myTime = info ['BreathingCycle']
myTimer = info ['Timer']
win = visual.Window(fullscr=True, size=(1536, 864), monitor='laptop')
#TODO: handle in a different way the screen resolution
instruction1 = visual.TextStim(win, text=u"""Valve on!""")
instruction2 = visual.TextStim(win, text=u"""Valve off!""")
instruction3 = visual.TextStim(win, text=u"""Thank you!""")
if infoDlg.OK:
Mytimer = time.time() + myTimer
while (myTime > 0 or time.time() < Mytimer):
if myChannel == '1':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',513))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',512))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '2':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',257))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',256))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '3':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',2049))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',2048))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '4':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',1025))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',1024))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '5':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',4097))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',4096))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '6':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',8193))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',8192))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '7':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',32769))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',32768))
myTime = myTime - 1
instruction2.draw()
win.flip()
elif myChannel == '8':
if u'i' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',16385))
myTime = myTime - 1
instruction1.draw()
win.flip()
if u'e' in event.waitKeys():
command = arduino.write(struct.pack(u'>H',16384))
myTime = myTime - 1
instruction2.draw()
win.flip()
win.close ()
arduino.close()
我找到了解决方案,因为有一些函数 event.waitKeys() 基本上是无限期地等待一个键,这就是为什么 time.time() < Mytimer 条件不可用核实。我不得不在 event.waitKeys() 中给出最长等待时间。