Run Multiple Phidget Devices, Attach Listener

Supporting Java SE version 7 and up
PhidgetMidget

Run Multiple Phidget Devices, Attach Listener

Postby PhidgetMidget » Sat Jun 25, 2011 8:49 pm

I am trying to write a Phidgets Application in Java that will orchestrate numerous Phidgets (phidget 8/8/8, servo, etc). Each works correctly through separate and independent applications; and when using simple code WITHOUT listeners all devices can work in one script. However, through using the Phidget example code there seem to be problems I can't seem to figure out once I try event handling with the Attach Event Listener. Below is a part of the first code attempt:

public void attached(AttachEvent ae)
{
try {
InterfaceKitPhidget attached = (InterfaceKitPhidget)ae.getSource();
AdvancedServoPhidget attached2 = (AdvancedServoPhidget)ae.getSource();

But either I get an error saying event ae is already defined, or nothing happens at all. I also tried creating a second attached method called "attached2(AttachEvent ae)", but nothing happens in this method and I can't seem to find where either "attach" method would actually be called.

Any help would again be appreciated; thanks!

PhidgetMidget

RobOrt
Phidgeteer!
Posts: 85
Joined: Wed Jun 17, 2009 11:39 am
Contact:

Re: Run Multiple Phidget Devices, Attach Listener

Postby RobOrt » Mon Jun 27, 2011 10:47 am

In your code example, are you trying to detect what type of Phidget is attached? If so then how about this :

Code: Select all

attached(AttachEvent ae) {

  Phidget p = ae.getSource; //Gets the source of event as Generic Phidget device

  switch (p.getDeviceID()){ // Gets the device ID as listed in the docs
    case Phidget.PHIDID_ANALOG_4OUTPUT:
      // Dos something appropriate to this device, even cast p as another class
    case Phidget.OTHER_IDS_DETAILED_IN_DOCS
}

}


You could get the class instead of the ID, just change p.getDeviceID to p.getDeviceClass().

PhidgetMidget

Re: Run Multiple Phidget Devices, Attach Listener

Postby PhidgetMidget » Sat Jul 09, 2011 3:33 pm

Thanks RobOrt,

That seems like a great idea, and I keep trying to make your suggested approach work, but can't seem to get around the error of "Exception in thread "Thread-2" java.lang.ClassCastException: com.phidgets.InterfaceKitPhidget cannot be cast to com.phidgets.AdvancedServoPhidgets"

This the line of code where the error is flagged; it is in the switch-case code for the case where Advanced Servo is connected:

AdvancedServoPhidget attached2 = (AdvancedServoPhidget)et.getSource();

And this is where et.getSource is first used (prior to the switch-case):

Phidget p = et.getSource();

Another strange thing happens. If the of the cases for each Phidget device are changed, sometimes this error is not caught in the command line; even though it still happens.

After reading through the Phidgets Java documentation for the com.phidget.Phidget class, it seems the Phidget p = et.getSource should be used only for each device, such as "AdvancedServoPhidget attached2 = (AdvancedServoPhidget)et.getSource()"; but we still run into this issue of consolidating code for listeners of more than one phidget.

Thanks!

RobOrt
Phidgeteer!
Posts: 85
Joined: Wed Jun 17, 2009 11:39 am
Contact:

Re: Run Multiple Phidget Devices, Attach Listener

Postby RobOrt » Wed Jul 13, 2011 2:14 pm

PhidgetMidget wrote:Thanks RobOrt,

That seems like a great idea, and I keep trying to make your suggested approach work, but can't seem to get around the error of "Exception in thread "Thread-2" java.lang.ClassCastException: com.phidgets.InterfaceKitPhidget cannot be cast to com.phidgets.AdvancedServoPhidgets"

This the line of code where the error is flagged; it is in the switch-case code for the case where Advanced Servo is connected:

AdvancedServoPhidget attached2 = (AdvancedServoPhidget)et.getSource();

And this is where et.getSource is first used (prior to the switch-case):

Phidget p = et.getSource();

Another strange thing happens. If the of the cases for each Phidget device are changed, sometimes this error is not caught in the command line; even though it still happens.

After reading through the Phidgets Java documentation for the com.phidget.Phidget class, it seems the Phidget p = et.getSource should be used only for each device, such as "AdvancedServoPhidget attached2 = (AdvancedServoPhidget)et.getSource()"; but we still run into this issue of consolidating code for listeners of more than one phidget.

Thanks!


I think your problem is a casting one. Basically you are trying to cast from a specific Phidget to another specific Phidget type which you cannot do. I can roughly see why you're trying to do this (I think as a fast way to get at the result that you want) but basically you cannot do this.

You cannot cast across the class hierachy. Hence you can only cast an AdvancedServoPhidget to a Phidget object and similarly for InterfaceKitPhidget. Therfore you MUST use the Phidget base class method getDeviceID() to identify the type of Phidget and then cast to this type. You cannot specific cast willy-nilly!

Look at my example code again. You'll see I only cast once I have determined the device ID.
Last edited by RobOrt on Wed Jul 13, 2011 2:21 pm, edited 1 time in total.

RobOrt
Phidgeteer!
Posts: 85
Joined: Wed Jun 17, 2009 11:39 am
Contact:

Re: Run Multiple Phidget Devices, Attach Listener

Postby RobOrt » Wed Jul 13, 2011 2:20 pm

Just notice I put some confusing comments in my example code. To clarify:

Code: Select all

attached(AttachEvent ae) {

  Phidget p = ae.getSource; //Gets the source of event as Generic Phidget device

  switch (p.getDeviceID()){ // Gets the device ID as listed in the docs
    case Phidget.PHIDID_INTERFACEKIT_8_8_8:
      /* Cast to InterfaceKit 8/8/8 */

       InterfaceKitPhidget ik = (InterfaceKitPhidget)p;
    case Phidget.OTHER_IDS_DETAILED_IN_DOCS


Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest