Skip to content Skip to sidebar Skip to footer

Python Code Run While I Stop It By Using Keyboard

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 lik

Solution 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.000286467338645defonVoltageRatioChange0(self, voltageRatio):
    Masse = (voltageRatio - (B0)) / (A0)
    self.masse = Masse


defonVoltageRatioChange1(self, voltageRatio):
    Masse = (voltageRatio - (B1)) / (A1)
    self.masse = Masse


defmain():
    whileTrue:

        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.000286467338645defonVoltageRatioChange0(self, voltageRatio):
    Masse = (voltageRatio - (B0)) / (A0)
    self.masse = Masse


defonVoltageRatioChange1(self, voltageRatio):
    Masse = (voltageRatio - (B1)) / (A1)
    self.masse = Masse


defmain():
    whileTrue:

        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.

Solution 2:

You probably want to do something like this:

import time

whileTrue:
    try:

        print('my code output') #substitute this line with your code

        time.sleep(0.05) #50 ms intervalexcept KeyboardInterrupt:
        answer = input("Quit? Y/N")
        if answer.upper() == 'Y':
            breakprint('Program terminated by the user')

Solution 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.000286467338645defonVoltageRatioChange0(self, voltageRatio):
    Masse = (voltageRatio - (B0) ) / (A0)
    self.masse = Masse

defonVoltageRatioChange1(self, voltageRatio):
    Masse = (voltageRatio - (B1) ) / (A1)
    self.masse = Masse

defresults():
        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:
        whileTrue:
            results()

            # Finally sleep for 50ms
            sleep(0.05)

    except KeyboardInterrupt:
        print("Goodbye")
        pass

Post a Comment for "Python Code Run While I Stop It By Using Keyboard"