Attaching a Channel

From Phidgets Support
Jump to: navigation, search
 Phidget Programming Basics: Attaching a ChannelTOC Icon.png Table of Contents

Back Arrow.png WhiteTab1.png WhiteTab2.png WhiteTab3.png WhiteTab4.png WhiteTab5.png WhiteTab6.png GreenTab7.png WhiteTab8.png WhiteTab9.png WhiteTab10.png WhiteTab11.png WhiteTab12.png WhiteTab13.png WhiteTab14.png WhiteTab15.png WhiteTab16.png Next Arrow.png


7 . Attaching a Channel

When a channel is open (and not attached), the system will continue to try to match it with a Phidget device channel until either a match is found, or the channel is closed. During this time, your program will remain unable to interact with the Phidget channel, as no physical channel has yet been linked to the software one.

When the software channel is matched with a physical channel, the channel is attached and an Attach event is fired.

Attach Event

An event handler can be registered to catch the Attach event. Each channel also has an Attached property that can be read to determine if the channel has been matched to a physical channel.

The following examples show how to set an event handler to detect the Attach event:

#Declare the event handler
def onAttachHandler(self):
    #You can access the Phidget that fired the event using the "self" parameter
    ph = self
    deviceSerialNumber = ph.getDeviceSerialNumber()
...
#Declare your object. Replace "DigitalInput" with the object for your Phidget
ch = DigitalInput()
...
#Assign the handler that will be called when the event occurs
ch.setOnAttachHandler(onAttachHandler)

public static DigitalInputAttachListener onAttach = new DigitalInputAttachListener() {
    @Override
    public void onAttach(AttachEvent e) {
        //You can access the Phidget that fired the event by calling "getSource()"
        //on the event parameter.
        //Replace "DigitalInput" with the object for your Phidget.
        DigitalInput ph = (DigitalInput) e.getSource();
        int deviceSerialNumber = ph.getDeviceSerialNumber();
    }
};
...
//Declare your object. Replace "DigitalInput" with the object for your Phidget.
DigitalInput ch;
...
//Assign the event listener that will be called when the event occurs
ch.addAttachListener(onAttach);

//Declare the event handler
void attach(object sender, Phidget22.Events.AttachEventArgs e) {
    //You can access the Phidget that fired the event by typecasting "sender"
    //to the appropriate Phidget object type.
    //Replace "DigitalInput" with the object for your Phidget.
    DigitalInput ph = ((DigitalInput)sender);
    int deviceSerial = ph.DeviceSerialNumber;
}
...
//Declare your object. Replace "DigitalInput" with the object for your Phidget.
DigitalInput ch;
...
//Assign the handler that will be called when the event occurs
ch.Attach += attach;

//Declare the event handler
static void CCONV onAttachHandler(PhidgetHandle ph, void *ctx) {
    //You can access the Phidget that fired the event by using the first parameter
    //of the event handler
    int deviceSerialNumber;
    Phidget_getDeviceSerialNumber(ph, &deviceSerialNumber);
}
...
//Declare your object. Replace "PhidgetDigitalInputHandle" with the handle for your Phidget object.
PhidgetDigitalInputHandle ch;
...
//Assign the handler that will be called when the event occurs
Phidget_setOnAttachHandler((PhidgetHandle)ch, onAttachHandler, NULL);

The attach event handler should be set before the channel is opened; otherwise, the attach event could occur before the handler is set to deal with it.

Can I Attach the Same Channel Multiple Times?

Any one physical channel can only be attached to one software channel at a time. This remains true across multiple programs. For example, if you connected a single Phidget Accelerometer to a computer, and ran two copies of the same program to open that Accelerometer at the same time, only the first program would attach, while the other would remain open but not attached. If the attached channel were to be closed, the other program would then attach to the Accelerometer.

The one exception is if the Phidget device channel is attached over the network through the Phidget Network Server. Multiple software channels are allowed to match and attach to the hardware channel over the network. Some Phidgets, such as motor controllers will never match more than one channel at a time, for safety reasons.