PhidgetException 12 (Duplicated request)

Comments & issues
njuk-njuk

PhidgetException 12 (Duplicated request)

Postby njuk-njuk » Wed Jun 20, 2007 8:21 pm

i am running across this in Java but assume this is a general API question, hence the post in this forum.

i am getting a PhidgetException when i make a call to open(). the code is 12, which is for a "duplicate request". it took me a while to figure this out, but i am effectively making a call to open() twice. this is a result of some event-driven code i have which has handles to two phidget servos on one particular phidget servo controller. my servo handles are an abstraction that each separately contain a reference to the actual servo controller.

is a duplicate request to open a bad thing?...enough so that an exception must be thrown?

is there a programmatic way to avoid this in the phidget world? for example, some check to see if the call to open() has been previously made? i would guess a call to isAttached() is not correct since it implies much more than having open called.

i don't like it, but i am thinking that simply ignoring the exception when it's code is 12 is the easiest approach.

thanks.

Admin
Site Admin
Posts: 8
Joined: Mon Jul 28, 2003 3:43 am
Contact:

Postby Admin » Thu Jun 21, 2007 8:07 am

Do you have two PhidgetServo Controllers? If you do then you need to use the serial number in the open(#####). When you leave it blank the library tries to open the "first" Phidget it finds, if that phidget is already open then you will get the exception.

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

Postby Patrick » Thu Jun 21, 2007 8:22 am

He said it was only one servo controller. And you raise some good points. The reason for this exception is that the library isn't built to handle more then one device handle per device - this might be a good idea at some point, but for now, we're going for exclusive access via one device handle. Your best bet is to open the device once and then just pass references to this handle around wherever you like in your code - the phidget library is thread safe. The duplicated exception could be ignored if you call open twice using the same phidget device handle, and in that case I guess it's really there just to let you know that open has already been called (and a second call is not neccessary). We can add an isOpen property.

-Patrick

Admin
Site Admin
Posts: 8
Joined: Mon Jul 28, 2003 3:43 am
Contact:

Postby Admin » Thu Jun 21, 2007 8:32 am

Sorry coffee hasn't hit the brain yet.

mlham

Re: PhidgetException 12 (Duplicated request)

Postby mlham » Sun Oct 11, 2009 7:43 pm

Hey Patrick:
So, I am trying to get some code working where I have a given phidget getting opened twice in the same thread of execution. Here is my question.

Here is the code I am working with. I have removed the attach methods from the listing for ease of reading, but they are there in the running code:

Code: Select all

package dcmotor;

import com.phidgets.MotorControlPhidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.AttachEvent;
import com.phidgets.event.AttachListener;
import com.phidgets.event.CurrentChangeEvent;
import com.phidgets.event.CurrentChangeListener;
import com.phidgets.event.DetachEvent;
import com.phidgets.event.DetachListener;
import com.phidgets.event.ErrorEvent;
import com.phidgets.event.ErrorListener;
import com.phidgets.event.InputChangeEvent;
import com.phidgets.event.InputChangeListener;
import com.phidgets.event.MotorVelocityChangeEvent;
import com.phidgets.event.MotorVelocityChangeListener;
import com.phidgets.event.ServerConnectEvent;
import com.phidgets.event.ServerConnectListener;
import com.phidgets.event.ServerDisconnectEvent;
import com.phidgets.event.ServerDisconnectListener;

/**
 *
 * @author mlham
 */
public class ControllerTester {

    public static void main(String[] args) throws PhidgetException {
        try{
        MotorControlPhidget mcp1 = new MotorControlPhidget();
        ... events attached
        MotorControlPhidget mcp2 = new MotorControlPhidget();
        ... events attached
        String ipAddr = args[0];
        int port = Integer.parseInt(args[1]);
        mcp1.open(41718, ipAddr,port);
//        mcp2.open(41718, ipAddr, port);
        Thread.sleep(10000);
        System.out.println("Here is the answer to the attached mcp1?" + mcp1.isAttached());
//        System.out.println("Here is the answer to the attached mcp2?" + mcp2.isAttached());
//        mcp2.waitForAttachment(2000);
        mcp1.setAcceleration(0, 50);

        System.out.println("Here is the acceleration: " + mcp1.getAcceleration(0));
        } catch (InterruptedException ie){
            ie.printStackTrace();
        }
    }

}


Here is what I get when I run this with the current commented out code blocks.

Code: Select all

MotorControlPhidget v-1 #-1
PhidgetMotorControl v110 #41718 (attached)
Here is the answer to the attached mcp1?true
Here is the acceleration: 50.0


Now, if I run this with say this command:

Code: Select all

java -cp dist/DCMotor.jar:dist/lib/phidget21.jar dcmotor.ControllerTester 192.168.123.134 5001 > log2

I am able to run it as many times as I can and not get duplicate message errors.

However, if I run the code with comments not in:

Code: Select all

String ipAddr = args[0];
        int port = Integer.parseInt(args[1]);
        mcp1.open(41718, ipAddr,port);
        mcp2.open(41718, ipAddr, port);
        Thread.sleep(1000); //Changed to reduce listing :)
        System.out.println("Here is the answer to the attached mcp1?" + mcp1.isAttached());
        System.out.println("Here is the answer to the attached mcp2?" + mcp2.isAttached());
        mcp1.setAcceleration(0, 50);


I get this for output:

Code: Select all

MotorControlPhidget v-1 #-1
PhidgetMotorControl v110 #41718 (attached)
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1
Here is the answer to the attached mcp1?true
Here is the answer to the attached mcp2?false
Here is the acceleration: 50.0
Error Event (12): Duplicated request.
MotorControlPhidget v-1 #-1


So, this doesn't make sense to me. Why can more than one thread on the same box have access to the phidgets, but if I run them from the same thread, I get a duplicate error? Why does the code/system care?
Just wondering, cause it makes writing code much harder if we have to first come up with a singleton for each of the phidgets we want to use, then create rapper objects that hold the actions we want to use, then pass the singletons in to the objects so they can use it to make the calls.

Is this a bug in the java library? Meaning that it is using/holding state and handing it back even though it being called from the remote open that is re-entrant?

Thanks in advance.

Mason

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

Re: PhidgetException 12 (Duplicated request)

Postby Patrick » Tue Oct 13, 2009 10:16 am

The C library (Java JNI Library) doesn't support more then one handle (Phidget object in Java) being associated with a single Phidget. This is the same for both local and remote Phidgets, and it's the same across all of our libraries.

Since remote Phidgets can be opened multiple times (from multiple applications), You can run your app multiple times without trouble. It only when you call open on the same Phidget twice within the context of the same instance on the JNI library that you have troubles.

It may make sense to allow multiple handles per Phidget in the library - but this would require some major rework to make things thread safe, though less so for remote Phidgets. I'll put it on the list.

-Patrick


Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest