我有一个PhidgetBridge,它连接到2号应变仪。我收到信号并使用此代码进行校准。但是,当我运行它时,它只显示一张打印,或者我想打印很多(使用此代码,我希望以50ms(即20 Hz)的数据间隔打印数据)。
Ctrl+S
答案 0 :(得分:0)
您只需要一个无限循环(while True
)即可运行,直到引发KeyboardInterrupt
异常为止。此外,为了每隔50毫秒运行一次打印新结果,您只需要在sleep(0.05)
循环的末尾添加while
:
from time import sleep
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time
import datetime
TIME_OUT = 5000 #5s beofre it throws a timeout exception
DATA_INTERVAL = 50 #1000ms sample frequency
A0 = -6.128983223994E-06
B0 = -0.000059639277340
A1 = -6.101017778744E-06
B1 = -0.000286467338645
def onVoltageRatioChange0(self, voltageRatio):
Masse = (voltageRatio - (B0) ) / (A0)
self.masse = Masse
def onVoltageRatioChange1(self, voltageRatio):
Masse = (voltageRatio - (B1) ) / (A1)
self.masse = Masse
def results():
voltageRatioInput0 = VoltageRatioInput()
voltageRatioInput0.masse = 0
voltageRatioInput0.setChannel(0)
voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange0)
voltageRatioInput0.openWaitForAttachment(TIME_OUT)
voltageRatioInput0.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
voltageRatioInput0.setDataInterval(DATA_INTERVAL)
voltageRatioInput1 = VoltageRatioInput()
voltageRatioInput1.masse = 0
voltageRatioInput1.setChannel(1)
voltageRatioInput1.setOnVoltageRatioChangeHandler(onVoltageRatioChange1)
voltageRatioInput1.openWaitForAttachment(TIME_OUT)
voltageRatioInput1.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
voltageRatioInput1.setDataInterval(DATA_INTERVAL)
print(str(voltageRatioInput0.masse) + " / " + str(voltageRatioInput1.masse))
voltageRatioInput0.close()
voltageRatioInput1.close()
if __name__ == '__main__':
try:
while True:
results()
# Finally sleep for 50ms
sleep(0.05)
except KeyboardInterrupt:
print("Goodbye")
pass
答案 1 :(得分:0)
我已将您的代码改编为使用While循环:
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time
import datetime
TIME_OUT = 5000 # 5s beofre it throws a timeout exception
DATA_INTERVAL = 50 # 1000ms sample frequency
A0 = -6.128983223994E-06
B0 = -0.000059639277340
A1 = -6.101017778744E-06
B1 = -0.000286467338645
def onVoltageRatioChange0(self, voltageRatio):
Masse = (voltageRatio - (B0)) / (A0)
self.masse = Masse
def onVoltageRatioChange1(self, voltageRatio):
Masse = (voltageRatio - (B1)) / (A1)
self.masse = Masse
def main():
while True:
voltageRatioInput0 = VoltageRatioInput()
voltageRatioInput0.masse = 0
voltageRatioInput0.setChannel(0)
voltageRatioInput0.setOnVoltageRatioChangeHandler(
onVoltageRatioChange0)
voltageRatioInput0.openWaitForAttachment(TIME_OUT)
voltageRatioInput0.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
voltageRatioInput0.setDataInterval(DATA_INTERVAL)
voltageRatioInput1 = VoltageRatioInput()
voltageRatioInput1.masse = 0
voltageRatioInput1.setChannel(1)
voltageRatioInput1.setOnVoltageRatioChangeHandler(
onVoltageRatioChange1)
voltageRatioInput1.openWaitForAttachment(TIME_OUT)
voltageRatioInput1.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
voltageRatioInput1.setDataInterval(DATA_INTERVAL)
print(
str(voltageRatioInput0.masse) + " / " +
str(voltageRatioInput1.masse))
try:
input("Press Enter to Stop\n")
except (Exception, KeyboardInterrupt):
pass
voltageRatioInput0.close()
voltageRatioInput1.close()
sleep(0.05)
main()
但是您的代码将在每次迭代时停止并等待输入,因此更好的实现是:
import os
import sys
import select
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time
import datetime
TIME_OUT = 5000 # 5s beofre it throws a timeout exception
DATA_INTERVAL = 50 # 1000ms sample frequency
A0 = -6.128983223994E-06
B0 = -0.000059639277340
A1 = -6.101017778744E-06
B1 = -0.000286467338645
def onVoltageRatioChange0(self, voltageRatio):
Masse = (voltageRatio - (B0)) / (A0)
self.masse = Masse
def onVoltageRatioChange1(self, voltageRatio):
Masse = (voltageRatio - (B1)) / (A1)
self.masse = Masse
def main():
while True:
voltageRatioInput0 = VoltageRatioInput()
voltageRatioInput0.masse = 0
voltageRatioInput0.setChannel(0)
voltageRatioInput0.setOnVoltageRatioChangeHandler(
onVoltageRatioChange0)
voltageRatioInput0.openWaitForAttachment(TIME_OUT)
voltageRatioInput0.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
voltageRatioInput0.setDataInterval(DATA_INTERVAL)
voltageRatioInput1 = VoltageRatioInput()
voltageRatioInput1.masse = 0
voltageRatioInput1.setChannel(1)
voltageRatioInput1.setOnVoltageRatioChangeHandler(
onVoltageRatioChange1)
voltageRatioInput1.openWaitForAttachment(TIME_OUT)
voltageRatioInput1.setBridgeGain(BridgeGain.BRIDGE_GAIN_128)
voltageRatioInput1.setDataInterval(DATA_INTERVAL)
print(
str(voltageRatioInput0.masse) + " / " +
str(voltageRatioInput1.masse))
voltageRatioInput0.close()
voltageRatioInput1.close()
os.system('cls' if os.name == 'nt' else 'clear')
print("I'm doing stuff. Press Enter to stop me!")
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
break
time.sleep(0.05) # wait 50ms
main()
这不会阻塞,并且会破坏Enter的循环。
答案 2 :(得分:0)
您可能想要执行以下操作:
import time
while True:
try:
print('my code output') #substitute this line with your code
time.sleep(0.05) #50 ms interval
except KeyboardInterrupt:
answer = input("Quit? Y/N")
if answer.upper() == 'Y':
break
print('Program terminated by the user')