【问题标题】:The while loop runs infinite. It is not possible to terminate while loop using timerwhile 循环无限运行。无法使用计时器终止 while 循环
【发布时间】:2019-03-26 10:19:55
【问题描述】:

我正在尝试通过 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()

【问题讨论】:

  • 您发布的代码缩进不一致 - 有时看起来像 4 个空格,有时是 8 个。确保您尝试运行的代码始终使用一种约定。建议使用 4 个空格,而不是制表符。
  • 另外,您的八个不同 myChannel 案例之间的唯一区别似乎是 struct.pack 参数中的数值。您是否有充分的理由不能只查找这些值,例如从以myChannel 为键的字典中,然后将它们传递给此代码的单个副本?这样做可能会减少你犯错误的机会。
  • @nekomatic 是的,你是对的。我必须改变,但这不是问题。
  • @nekomatic mapped.pack 将这些数字打包成两个字节并将其发送到 Arduino,在那里我将第一个字节作为通道号读取,例如 1、2、4、8 等等,第二个byte 用于 1 或 0 来切换状态。不过,这也不是问题。

标签: python-2.7 arduino-uno


【解决方案1】:

我找到了解决方案,因为有函数 event.waitKeys() 基本上是无限期地等待一个键,这就是为什么 time.time()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2021-12-23
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    相关资源
    最近更新 更多