Attaching a Channel

From Phidgets Support
 Phidget Programming Basics: Attaching a ChannelTOC Icon.png Table of Contents

Nav Back Arrow.png Nav Back Hover.png WhiteTab1.png HoverTab1.jpg WhiteTab2.png HoverTab2.jpg WhiteTab3.png HoverTab3.jpg WhiteTab4.png HoverTab4.jpg WhiteTab5.png HoverTab5.jpg WhiteTab6.png HoverTab6.jpg GreenTab7.png WhiteTab8.png HoverTab8.jpg WhiteTab9.png HoverTab9.jpg WhiteTab10.png HoverTab10.jpg WhiteTab11.png HoverTab11.jpg WhiteTab12.png HoverTab12.jpg WhiteTab13.png HoverTab13.jpg WhiteTab14.png HoverTab14.jpg WhiteTab15.png HoverTab15.jpg WhiteTab16.png HoverTab16.jpg Nav Next Arrow.png Nav Next Hover.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);
// Declare your object. Replace "DigitalInput" with the object for your Phidget
const ch = new phidget22.DigitalInput()

// Assign the handler that will be called when the event occurs
ch.onAttach = function() {
    // You can access the Phidget that fired the event using this function's parameter
    let deviceSerialNumber = this.deviceSerialNumber
}

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 in Multiple Programs?

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. In fact, if the device has other devices connected to it (e.g. a VINT Hub with many Phidgets connected), attaching a channel from any device in one program will prevent any of the channels on the entire Hub from attaching in the other program.

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.