Page 1 of 1

Re: Time interval data from Vint hub

Posted: Tue Dec 12, 2017 11:18 am
by mparadis
Hi Max,

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()
I set the dataInterval for both channels to the minimum for the VINT Hub (1ms) so that 'a' and 'b' will always be as up-to-date as possible regardless of how long the loop sleeps for.

Re: Time interval data from Vint hub

Posted: Wed Dec 13, 2017 9:26 am
by mparadis
I had assumed that you were trying to access normal sensors on VINT ports 0 and 1. Now that I know you have a DAQ1500 connected to port 0, we don't need to setIsHubPortDevice(1) since we're accessing a Phidget that's connected to the hub.

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.setChannel(0)
ch2.openWaitForAttachment(5000)
ch2.setDataInterval(20)

while 1:
    try:
        print("Bridge: {0}   Sensor: {1}".format(b,a))
        time.sleep(0.4)
    except (KeyboardInterrupt):
        print ("gracefully aborted")
        ch.close()       
        ch2.close()
        sys.exit()
- I changed the DataInterval for port 0, since the minimum data interval is 20ms for the DAQ1500. You can find this information on the API page and selecting DAQ1500 from the second drop-down box.

- I also added setChannel(0) because I have the load cell plugged into port 0 of the DAQ1500.

- You don't actually have to call setBridgeEnabled(1) because it is enabled by default (you only need this function if you choose to disable the bridge input for some reason).

- The reason that python is giving you such cryptic error messages is because you're not using try/except to catch PhidgetException errors. If you catch PhidgetExceptions, they'll be able to tell you what's wrong (e.g. invalid argument, Phidget not attached, etc). You can see how catching PhidgetException works if you look at the python sample code for VoltageRatio.