Python code run while I stop it by using Keyboard

血红的双手。 提交于 2020-02-06 16:33:28

问题


I have a PhidgetBridge which is connected to 2 gauge strain. I got the signal and make the calibration using this code. But when I run it, it displays me only one print, or I'd like to have many prints (with this code, I'd like the data to be printed with a data interval of 50ms (so 20 Hz)).

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():

        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()


main()

回答1:


I've adapted your code to use a While loop:

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()

But your code will halt on every iteration and wait for input so a better implementation would be:

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()

This doesn't block and will break the loop on enter.




回答2:


You probably want to do something like this:

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')



回答3:


You just need an infinite loop (while True) that will run until a KeyboardInterrupt exception is thrown. Furthermore, in order to run print the new results every 50 milisseconds, you just need to add sleep(0.05) in the end of the while loop:

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


来源:https://stackoverflow.com/questions/58978625/python-code-run-while-i-stop-it-by-using-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!