Using Multiple Phidgets

From Phidgets Support
Jump to: navigation, search

When only one Phidget is connected to a computer, it is pretty easy for a simple program to open and attach to the channels on that one Phidget; however, when the program becomes more complicated or more than one Phidget is connected, attaching to the correct channel can be more difficult.

Matching Channels

Before opening a channel, it is important to set enough of the matching properties to ensure the desired device channel is matched. By default, the matching code in the Phidget library will match the first available device channel that is of the correct class. For example, if two temperature sensor devices are connected to a computer, it is undefined which will attach when the device serial number is not specified before the channel is opened.

The best practice is to always specify at least the device serial number and the channel ID of the device channel that should be attached to the user channel. Even when not strictly necessary, setting as many matching properties as possible can ensure that code will not break in the future.

Any matching properties being set must be set before the channel is opened, as the behaviour of setting matching properties while the channel is open is undefined.

Using the Channel Class

The Phidget's channel classes highlighted in yellow.

Each channel exported by a Phidget device has channel class. The channel class describes the type of Phidget being used, and must be known to start interacting with the Phidget. A Phidget can have multiple channels, and they may even have different channel classes, like the example shown in this image. You can find which channel classes are used with your Phidget by finding them listed on the enclosure, or on the API tab of the product's page.

The channelClass property is implied by the handle and API you use to communicate with the Phidget. For instance a DigitalOutput channel has a channel class of DigitalOutput, and a HumiditySensor channel has a channel class of HumiditySensor.

Get the channel class with the ChannelClass property. It's often useful for distinguishing which Phidget channel called a shared Attach, Detach, or Error Event Handler.

Using the Channel ID

Channel numbers highlighted in yellow.

Each channel exported by a Phidget device has an id, normally starting at 0. Channel ID will allow you to distinguish between multiple channels on a single device. If the Phidget has more than one channel class, the numbering starts back at zero for each class. For example, the Thumbstick Phidget has two VoltageRatioInput channels (IDs 0 and 1), and a single DigitalInput channel (ID 0).

The channel property must be set to ensure the device channel the Phidget software library matches is right one. If the channel property is not specified, the library will attach to channel 0 if available, and the next available channel if not.

Set the channel id with the Channel property.

Using the Hub Port

Numbered ports on a VINT Hub.

VINT hubs have a number of ports that VINT devices can be connected to. To ensure the correct VINT device is attached, the hub port must be specified. If two temperature sensors are attached to the same hub, and the hub port is not specified prior to opening a TemperatureSensor channel, there is no way to know which temperature sensor will be attached.

Set the hub port with the HubPort property.

Using "Is Hub Port Device"

Examples of devices that would require IsHubPortDevice to be true.

VINT ports have the ability to be used with analog sensors as well as with intelligent VINT Phidgets. If you are trying to use a simple device such as a slider, a 0-5V sensor, a switch, or an LED with the VINT port directly, you will want to set this property to TRUE. Otherwise this should be left as default or set to FALSE.

Set is hub port device with the IsHubPortDevice property.

Using the Serial Number

The serial number on the underside of a Phidget.

Each Phidget has a unique serial number (VINT devices inherit the serial number of the hub they're connected to). When you use more than one device with the same channel class, the device serial number must be specified to ensure the channel on the desired device is matched. The device serial number can be found on a label on the underside of the Phidget, or determined by reading the DeviceSerialNumber property.

Set the device serial number with the DeviceSerialNumber property.

Using the Label

If you want to have a human-readable way to reference your Phidget instead of an arbitrary serial number, you can use the Label feature of Phidgets. You can call the WriteLabel method to permanently store the desired label in the Phidget's onboard flash. From then on, you can set the DeviceLabel property of a channel before opening it. The disadvantage of Labels is that they are not available on all operating systems and languages:

  • You can only write labels to Phidgets that have serial numbers.
  • In Windows, any label can be read, but label can only be written for newer Phidgets that support firmware upgrading.
  • Some programming languages do not support writing to labels. See the Phidget22 API to see if it's supported in your language.

If you have a VINT Hub that has a label written to it, you can use the same label to address any device connected to that hub. See the User Guide for more information on using labels with the VINT Hub.

Note: You should be careful when writing labels to your Phidgets in your code, because the label is stored in flash which can only be re-written around 10,000 times before it will no longer write. If your program is complex, be sure to test it thoroughly before using WriteLabel to avoid accidentally burning out the flash.

Code Examples

For example, in Java, this would be:

 ch.setDeviceSerialNumber(12345);  // match device 12345
 ch.setHubPort(4);                 // match hub port 4
 ch.setChannel(1);                 // match channel 1 port 4 dev 12345;                        // start matching

 //for a second device simply repeat this process
//and so on

Or in C:

 Phidget_setDeviceSerialNumber((PhidgetHandle)ch, 12345);  // match device 12345
 Phidget_setHubPort((PhidgetHandle)ch, 4);                 // match hub port 4
 Phidget_setChannel((PhidgetHandle)ch, 1);                 // match channel 1 port 4 dev 12345
 Phidget_open((PhidgetHandle)ch);                         // start matching

 //for a second device simply repeat this process
 Phidget_setDeviceSerialNumber((PhidgetHandle)ch1, 12345);  
 Phidget_setHubPort((PhidgetHandle)ch1, 2);                 
 Phidget_setChannel((PhidgetHandle)ch1, 1);                 
//and so on

Distinguishing Events

When using events, you can either create separate events for each device, or handle multiple devices with the same event (or some combination of both). In the case where multiple devices are handled by the same event handler, the matching properties of the channel can be read to determine what device channel the event is from.

The channel that fired the event will always be available in the event handler, regardless of language, though its exact form changes from language to language. In most languages, it is the first parameter in the parameter list, whether it's called source or sender or ch. In Java, you will need to call getSource() on the event parameter to get the Phidget that caused the event. In JavaScript, you will use the this keyword in your event handler.

Referencing Phidgets from Events

When using multiple Phidgets in the same program, you may want to access one Phidget from the within an event caused by another. To do so, there are three basic methods of making the Phidget available, depending of the programming language you are using:

C# and Java

In C# and Java, chances are your event handlers are defined in the same namespace (or class) as the Phidget handles. In this case, you can simply reference Phidgets in the event handlers the same way as you would in the rest of your code.

Python and JavaScript

Python and JavaScript are both dynamically interpreted, and objects follow a less rigid structure than in other languages. In these languages, to access another Phidget from an event handler, you can add the second Phidget's handle to the Phidget triggering the event. Then, you may access the second Phidget using the self parameter of the event.

For example, in Python:

def onStateChangeHandler(self, state):
    #Be sure the other Phidget you are trying to access is attached before using it
    #In JavaScript, you would use "this" instead of "self", but the idea is the same 
    print("State %f" % state)
    return 0

input = DigitalInput()
output = DigitalOutput()

#Addressing info here

#Here we create an attribute of input called "output", and assign it the handle for output
#You can use this exact line in JavaScript
input.output = output



In C, all event handler declarations have a context pointer that can be pointed at any object you choose. This can be a set of relevant data, or even a Phidget handle. If you pass a Phidget handle as the context pointer for an event, you can access the passed Phidget from the event as follows:

static void CCONV onStateChangeHandler(PhidgetDigitalInputHandle pdih, void *ctx, int state) {
    int attached;
    //Extract our output handle from the context pointer
    PhidgetDigitalOutputHandle out = (PhidgetDigitalOutputHandle)ctx;

    //Be sure the other Phidget you are trying to access is attached before using it
    Phidget_getAttached((PhidgetHandle)out, &attached);

        PhidgetDigitalOutput_setState(out, state);

    printf("[State Event] -> State: %d\n", state);

int main() {
    PhidgetDigitalInputHandle ch = NULL;
    PhidgetDigitalOutputHandle out = NULL;

    //Addressing info here

    //Here we pass the handle for "out" as the context pointer so we can access it from the event
    PhidgetDigitalInput_setOnStateChangeHandler(ch, onStateChangeHandler, out);

    if (SetAttachDetachError_Handlers((PhidgetHandle)ch))
        goto error;
    Phidget_openWaitForAttachment((PhidgetHandle)out, 5000);
    Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);

    //The rest of your code here


Further Reading

Phidget Programming Basics - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.

Data Interval/Change Trigger - Learn about these two properties that control how much data comes in from your sensors.

Polling vs. Events - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.

Logging, Exceptions, and Errors - Learn about all the tools you can use to debug your program.

Phidget Network Server - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.

Best Phidgets Practices - Good programming habits that will save you from common problems when writing code for your Phidgets.