Some notes about your program:
- It doesn't make sense to put the definition for your VoltageRatioChangeHandler inside your while loop, you only need to define it once, like you would a variable.
- It doesn't make sense to access the value for the variable "a" outside of your change handler. You can think of change handlers as separate programs that run alongside your main program. By the time your main program attempts to access "a", you have no way of knowing whether it's been set by your event handler. You're better off printing "a" from the handler. Rather than continuously printing in an infinite loop, you'd normally do your prints in the event handler and wait for input on your main program. If you want both numbers output simultaneously, the infinite loop may be the easiest way. I needed to add the "global" keyword to the variables to make it work this way.
- The second time you call setIsHubPortDevice, the parameter should still be "1", as 1 means "true" in this case.
- You should use different handles for your different channels. That way, you can keep them both open at the same time.
- If you're just using ordinary sensors plugged into your VINT hub, it doesn't make sense to call setBridgeEnabled. That function only belongs to boards like our 1046 or DAQ1500. If this is the case, you would not use setIsHubPortDevice(1), since you're getting data from the attached device, not the hub port itself.
Here's a modified version that should work:
Code: Select all
#There is about 0.8 seconds difference between the readings
import sys
import time
import datetime
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *
try:
ch = VoltageRatioInput()
ch2 = VoltageRatioInput()
except RuntimeError as e:
print("Runtime Exception %s" % e.details)
print("Press Enter to Exit...\n")
readin = sys.stdin.read(1)
exit(1)
a=0
b=0
def VoltageRatioChangeHandlerA(e, voltageRatio):
global a
a = voltageRatio
def VoltageRatioChangeHandlerB(e, voltageRatio):
global b
b = voltageRatio
start = time.time()
ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerA)
ch.setHubPort(1)
ch.setIsHubPortDevice(1)
ch.openWaitForAttachment(5000)
ch.setDataInterval(1)
ch2.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerB)
ch2.setHubPort(0)
ch2.setIsHubPortDevice(1)
ch2.openWaitForAttachment(5000)
ch2.setDataInterval(1)
while 1:
try:
print("Channel 0: {0} Channel 1: {1}".format(b,a))
time.sleep(0.4)
except (KeyboardInterrupt):
print ("gracefully aborted")
ch.close()
ch2.close()
sys.exit()