Page 1 of 1

Is it possible to autodetect / autoconfigure Phidgets?

Posted: Sat Sep 28, 2013 11:16 pm
by pdxjjb
I have a working data acquisition program, but it requires static configuration of the attached Phidgets (thermocouple sensor for now, but more to follow). So I thought I would write a nicer program that used the Manager to dynamically detect attaching Phidgets and configure itself.

I quickly figured out that the Phidget passed to the AttachListener is not a real (typed) Phidget. This is sort of documented ("The Phidget manager deals in base Phidget objects. These objects are not actually connected to opened Phidgets but can be used to get serial number, name, version, etc.") on the Manager page.

OK, I thought, when I get the Manager AttachEvent I will get the serial number out of the "fake" Phidget, create TemperatureSensorPhidget() and call open(int serial) on it. But this doesn't work ("PhidgetException 5 (Phidget not physically attached)").

Of course the Phidget really is physically attached, since I'm in the AttachEvent handler. Which leads me to believe I should have created the TemperatureSensorPhidget before attaching the device. But this means I couldn't have configured it dynamically.

Am I missing something?


Re: Is it possible to autodetect / autoconfigure Phidgets?

Posted: Sun Oct 20, 2013 4:27 pm
by pdxjjb
For those who might care, here's what I discovered in getting this to work. First, as noted, the Phidget passed to the AttachListener on the Manager object is a "base Phidget", not a "typed Phidget". It can be used for getting the (String) "type" of the actual phidget, e.g. "PhidgetTemperatureSensor", but it's not an instance of TemperatureSensorPhidget.

So when you get the event, you must create a TemperatureSensorPhidget and open() it. And then you must wait for it to be attached(), either by adding an event listener before you open it or by polling isAttached(). In other words the attached event is overloaded to mean "attached" (plugged into the computer) but also "open".

Third, you cannot poll isAttached() within the AttachedEvent handler for the Manager. The Phidget library isn't reentrant (or at least isn't reentrant enough for this).

So the way I handled this is to create a newSingleThreadedExecutor(). When a Manager AttachedEvent occurs, I queue up a Runnable that creates the correct "typed" Phidget, opens it, polls it with isAttached(), and then calls the rest of the initialization code I need for my own purposes. This seems to work for me.

I'm not sure the Executor needs to be single-threaded, but since I'm not complete clear on the reentrancy limitations of the Phidgets API this seemed safest - ensure that everything except the Manager AttachEvents occurs serially on a single thread.