Using Multiple Phidgets: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
[[Category:Programming]] | [[Category:Programming]] | ||
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. | |||
===Using the Channel ID=== | |||
Each channel exported by a Phidget device has an id, normally starting at 0. The {{Code|channel}} property must be set to ensure the device channel the Phidget software library matches is right one. If a 4-channel temperature sensor is connected, and the {{Code|channel}} property is not specified, the matching code will attach to channel 0 if available, and the next available channel if not. | |||
Set the '''channel id''' with the {{Code|Channel}} property. | |||
===Using the | ===Using the Hub Port=== | ||
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 channel, it is undefined which temperature sensor will be attached. | |||
Set the '''hub port''' with the {{Code|HubPort}} property. | |||
===Using the Serial Number=== | |||
Each Phidget has a unique serial number (VINT devices inherit the serial number of the hub). When there is more than one device that exports 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 bottom of the Phidget, or determined by reading the {{Code|DeviceSerialNumber}} property. | |||
Set the '''device serial number''' with the {{Code|DeviceSerialNumber}} property. | |||
===Using the Label=== | |||
Most Phidgets can be assigned a label that may be used as a human-readable way to reference them (VINT devices inherit the serial number of the hub). In software that will be distributed, or cannot easily be modified it is useful to attach to channels based on the label of the device. This way, the device can be labelled prior to running the software, and the new device will be matched | |||
Set the '''device label''' with the {{Code|DeviceLabel}} property. | |||
Some limitations are: | |||
* In [[OS - Windows|Windows]], label can be read on any Phidget that has a serial number, but label can only be written for Phidgets that support firmware upgrading. | |||
* Some programming languages do not support writing to labels. See the {{Phidget22API}}. | |||
===Code Examples=== | |||
For example, in Java, this would be: | |||
<div class="source"> | |||
<syntaxhighlight lang=java> | |||
ch.setDeviceSerialNumber(12345); // match device 12345 | |||
ch.setHubPort(4); // match hub port 4 | |||
ch.setChannel(1); // match channel 1 port 4 dev 12345 | |||
ch.open(); // start matching | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | </div> | ||
Or in C: | |||
Or | |||
<div class="source"> | <div class="source"> | ||
<syntaxhighlight lang= | <syntaxhighlight lang=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((CPhidgetHandle)ch); // start matching | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | </div> | ||
===Distinguishing Events=== | ===Distinguishing Events=== |
Revision as of 18:32, 11 July 2017
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.
Using the Channel ID
Each channel exported by a Phidget device has an id, normally starting at 0. The channel
property must be set to ensure the device channel the Phidget software library matches is right one. If a 4-channel temperature sensor is connected, and the channel
property is not specified, the matching code 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
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 channel, it is undefined which temperature sensor will be attached.
Set the hub port with the HubPort
property.
Using the Serial Number
Each Phidget has a unique serial number (VINT devices inherit the serial number of the hub). When there is more than one device that exports 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 bottom of the Phidget, or determined by reading the DeviceSerialNumber
property.
Set the device serial number with the DeviceSerialNumber
property.
Using the Label
Most Phidgets can be assigned a label that may be used as a human-readable way to reference them (VINT devices inherit the serial number of the hub). In software that will be distributed, or cannot easily be modified it is useful to attach to channels based on the label of the device. This way, the device can be labelled prior to running the software, and the new device will be matched
Set the device label with the DeviceLabel
property.
Some limitations are:
- In Windows, label can be read on any Phidget that has a serial number, but label can only be written for Phidgets that support firmware upgrading.
- Some programming languages do not support writing to labels. See the Phidget22 API.
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
ch.open(); // start matching
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((CPhidgetHandle)ch); // start matching
Distinguishing Events
If you are using event-driven code, once you have correctly opened multiple Phidgets of different types, they will have different event handlers and hence you will know what Phidget triggered which event. If you are using multiple Phidgets of the same type, or you are trying to determine within general events (such as Attach Events) which Phidget triggered the event, you can then check the serial number (or device type) of the triggering device and act accordingly.
For example, in Java, your attach event handler might look like this:
detachHandler = new DetachListener() {
public void detached(DetachEvent event) {
int serialNumber = ((Phidget)event.getSource()).getSerialNumber();
// Do something according to serialNumber
} }
Or in C:
int AttachHandler(CPhidgetHandle device, void *userptr) {
int serialNo;
CPhidget_getSerialNumber(device, &serialNo);
// Do something according to serialNumber
}
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.