Urgent - Help needed with driver timing issue

Supporting Java SE version 7 and up
RickBullotta
Phidgetsian
Posts: 5
Joined: Tue Nov 29, 2011 9:19 am
Contact:

Urgent - Help needed with driver timing issue

Postby RickBullotta » Mon Feb 10, 2014 8:41 am

Absolutely stumped on what should be a simple use case for the Phidgets Java library:

We are trying to use the Manager class attach listener to know when interface kit Phidgets are added. I get the event fine, but in the event handler I attempt to create an InterfaceKitPhidget object, open it using the phidget serial number, and wait for attachment. Oddly enough it times out on attachment (yet it is ALREADY ATTACHED!).

Need help ASAP. Code below.

public void attached(AttachEvent event) {
try {
ifk = new InterfaceKitPhidget();

Integer serialNumber = event.getSource().getSerialNumber();

ifk.open(serialNumber);

if(!ifk.isAttached())
ifk.waitForAttachment(5000);
}
catch(Exception ex) {
}

RickBullotta
Phidgetsian
Posts: 5
Joined: Tue Nov 29, 2011 9:19 am
Contact:

Re: Urgent - Help needed with driver timing issue

Postby RickBullotta » Mon Feb 10, 2014 9:37 am

It definitely seems like a concurrency issue in the Phidgets Java SDK. Is the source code available anywhere? If so, I'll take a look and fix the SDK. If I attempt the open() and waitForAttachment() outside of the attached(AttachEvent event) listener on Manager, it works fine.

User avatar
Patrick
Lead Developer
Posts: 3078
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Urgent - Help needed with driver timing issue

Postby Patrick » Tue Feb 11, 2014 10:11 am

You can't call waitForAttachment from within the attach event, because you are then blocking the thread which would set the attached status to true. You should never call a blocking call in an event handler.

-Patrick

RickBullotta
Phidgetsian
Posts: 5
Joined: Tue Nov 29, 2011 9:19 am
Contact:

Re: Urgent - Help needed with driver timing issue

Postby RickBullotta » Tue Feb 11, 2014 10:21 am

Thanks, Patrick. So what is the best practice for opening and attaching to a Phidget from within an Attach event?

User avatar
Patrick
Lead Developer
Posts: 3078
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Urgent - Help needed with driver timing issue

Postby Patrick » Tue Feb 11, 2014 10:47 am

It depends on your situation, but most of the time it's not necessary to use the PhidgetManager - you just use the OpenAny() call. The Manager is only really needed when you want to enumerate an unknown number of Phidgets.

At any rate, the correct practice, it to not mix event driven code and blocking code. If you are going to use the Manager attach events, then:

1. create your new object
2. register a new attach event for it
3. open it
4. return from the manager attach event.
5. when the new device attach event fires, you can initialize the device from within that event.

If you want to use waitForAttachment, then you would poll the attachedDevices list of the Phidget manager in your main() loop, calling open and waitForAttachment() as you find devices you wish to open - this method is clunky and not really recommended - mostly it is only for languages which don't support events.

-Patrick


Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest