PhidgetManager not ready after opening?

Supporting Visual Studio on Windows
jorikcaljouw
Phidgetsian
Posts: 9
Joined: Fri Nov 20, 2009 9:25 am
Location: Eindhoven, The Netherlands
Contact:

PhidgetManager not ready after opening?

Postby jorikcaljouw » Fri Nov 20, 2009 9:49 am

I have a problem using the Manager class. I'm trying to generate a list of connected phidgets to select a phidget device and read sensor input from that device. This functionality is similar to what the Phidget Control Panel does.

When I call the open method of my Manager object, the attach handler is fired for all connected devices, but when I read the Devices property of my Manager object in this attach event handler, the program stops responding.

When I debug the event handler and put a breakpoint on the call to the Devices property and try to see the status of the Manager object, I get the error shown in the attached screenshot after about 10 seconds waiting. The source code of my test project is shown below.

I know you have to wait for the interfacekit class before you can use it, but I can't see what I have to wait for with the Manager class. If I wait a little after calling Manager.open(), I can debug the Manager object and use the Devices property properly. Does anybody know what I'm doing wrong or is this a bug of the Phidgets Library?

Thanks for your help

Code: Select all

using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Phidgets;
using Phidgets.Events;

namespace PhidgetManager_test
{
    public partial class Form1 : Form
    {
        Manager phidgetManager = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
       
        void phidgetManager_Attach(object sender, Phidgets.Events.AttachEventArgs e)
        {
            // I can get info from the attaced device
            listBox1.Items.Add(e.Device.Name);

            // I cannot get info from the Devices property of the phidgetManager
            listBox1.Items.Add("Number of attached devices: " + phidgetManager.Devices.Count.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            phidgetManager = new Manager();
            phidgetManager.Attach += new AttachEventHandler(phidgetManager_Attach);
            phidgetManager.open();
        }
    }
}
Attachments
error screenshot.PNG
error screenshot.PNG (14.22 KiB) Viewed 2412 times

becafuel
Phidgeteer!
Posts: 86
Joined: Sat Apr 25, 2009 5:17 am
Location: France
Contact:

Re: PhidgetManager not ready after opening?

Postby becafuel » Fri Nov 20, 2009 11:08 am

I personnaly add a delay like Thread.Sleep(500) just after the manager open call.

Since connected devices could need some (little) time to initialize and then report themselves to the manager, it leaves some time for it.

Btw, a Manager.WaitForAttachment() method would be welcome, here ;)


Regards,
_______
Christophe

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

Re: PhidgetManager not ready after opening?

Postby Patrick » Fri Nov 20, 2009 11:49 am

Are you running the latest library? This should work, it works for me.

-Patrick

jorikcaljouw
Phidgetsian
Posts: 9
Joined: Fri Nov 20, 2009 9:25 am
Location: Eindhoven, The Netherlands
Contact:

Re: PhidgetManager not ready after opening?

Postby jorikcaljouw » Fri Nov 20, 2009 6:30 pm

patrick wrote:Are you running the latest library? This should work, it works for me.


That's strange, just before posting to this forum I updated the drivers from the web and added the reference to the Phidgets again in Studio .NET.

Monday I'm back at the office and then my computer will restart for the first time since the driver update, maybe that'll help.

jorikcaljouw
Phidgetsian
Posts: 9
Joined: Fri Nov 20, 2009 9:25 am
Location: Eindhoven, The Netherlands
Contact:

Re: PhidgetManager not ready after opening?

Postby jorikcaljouw » Mon Nov 23, 2009 7:05 am

I'm using Version 2.1.6.228 of the Phidget.NET reference according to Visual Studio, this must be the latest version. Rebooting didn't help, the application just hangs if I access the Devices list too early.

A Thread.Sleep doesn't help either, apparently that just stalls whatever needs to be done as well. Even a Thread.Sleep(10000) after the open() call blocks my application if I evaluate .Devices immediately after the Sleep or in the Attach event.

The only way I've been able to read the .Devices property is by calling it at least about 20ms after I call open() e.g. with a Timer.

becafuel
Phidgeteer!
Posts: 86
Joined: Sat Apr 25, 2009 5:17 am
Location: France
Contact:

Re: PhidgetManager not ready after opening?

Postby becafuel » Mon Nov 23, 2009 8:19 am

Did you try to check the manager properties outside of the Attach event ?
I just realized that I was doing that way (that is : not in the event handler) when I advised you about the Thread.Sleep delay.


Here's a excerpt from real code :

Code: Select all

private void DetecteInterface()
        {
            Manager Man = new Manager();

            Man.open();
            Thread.Sleep(100);   // The delay I mention in my post
            if (Man.Devices.Count == 0)
            {
                ImageIF.Image = BaFTrackPh.Properties.Resources.no_interface;
                LabelInputIF.Visible = false;
                IFID = Phidget.PhidgetID.NOTHING;
                IFClasse = Phidget.PhidgetClass.NOTHING;
                IFName = "N/A";
                BoxInfo.AppendText(LogNoIF + Environment.NewLine);
            }
            else
            {
                IFID = Man.Devices[0].ID;
                IFName = Man.Devices[0].Name;
                IFClasse = Man.Devices[0].Class;
                LabelInputIF.Text = "Input " + Properties.Settings.Default.Num_Input.ToString();
                BoxInfo.AppendText(Man.Devices[0].Name + Environment.NewLine);
                try
                {
                    switch (IFID)
                    {
                        case Phidget.PhidgetID.MOTORCONTROL_LV_2MOTOR_4INPUT: ImageIF.Image = BaFTrackPh.Properties.Resources.if1060tn;
                            IfMoteur.open("PCASTRO");   // Changement pour compatibilité avec mon programme FocuserContact1060
                            IfMoteur.waitForAttachment(10000);
                            IfMoteur.InputChange +=new InputChangeEventHandler(If_InputChange);
                            IfMoteur.Error += new ErrorEventHandler(If_Error);
                            IsFermeClapet.Enabled = true;
                            break;


As you can see, no event handler for the manager.

If I remove the Thread.Sleep(100) line, then I always get 0 connected devices.

May be this could help you.


Regards,
________
Christophe

jorikcaljouw
Phidgetsian
Posts: 9
Joined: Fri Nov 20, 2009 9:25 am
Location: Eindhoven, The Netherlands
Contact:

Re: PhidgetManager not ready after opening?

Postby jorikcaljouw » Mon Nov 23, 2009 9:17 am

becafuel wrote:Did you try to check the manager properties outside of the Attach event ?
I just realized that I was doing that way (that is : not in the event handler) when I advised you about the Thread.Sleep delay.
[...]
As you can see, no event handler for the manager.

If I remove the Thread.Sleep(100) line, then I always get 0 connected devices.

May be this could help you.


Well that does give a hint to what's wrong. If I do not assign an Attach EventHandler, I have no problems with reading the Devices property. I don't even have to add a Sleep to get the correct number of connected devices!

So only if I do use an Attach eventhandler my program halts when quering the Devices property too soon.

Thanks for this tip! Now the question is why my eventhandler is causing these issues. I'm attaching the handler just before calling open() with:

Code: Select all

phidgetManager.Attach += new AttachEventHandler(phidgetManager_Attach);


and even with an empy handler (all statements are commented), I get the hangs:

Code: Select all

void phidgetManager_Attach(object sender, Phidgets.Events.AttachEventArgs e)
{

// I can get info from the attaced device
//listBox1.Items.Add(e.Device.Name);

// I cannot get info from the Devices property of the phidgetManager
//listBox1.Items.Add("Number of attached devices: " + phidgetManager.Devices.Count.ToString());

}

becafuel
Phidgeteer!
Posts: 86
Joined: Sat Apr 25, 2009 5:17 am
Location: France
Contact:

Re: PhidgetManager not ready after opening?

Postby becafuel » Mon Nov 23, 2009 9:49 am

I just did some tests with an Attach event handler and also got weird (erratic) behaviour :(
Sometimes it works, some other times it doesn't... It even prevents the open call from working, at times.
Problem is that I cannot reproduce each time the same result : if I execute 2 or 3 times the same exe, I can get 2 or 3 different results.

Btw, what is the interest in a manager attach event ? I mean, only physical devices can be attached/detached, not a logical component like the manager.
It would be interesting to know what is really "attach" & "detach" for a Manager and when those event are fired.
Phidgets staff, if you read us ;)


Anyway, the solution is to not use events for the manager but for the devices instead.

About the Sleep delay, right now I don't have much devices on hand to test, but I suspect that some boards like motors controllers may need some time to report themselves. Here I only have a Phidget Encoder 1052 and I indeed dont need any delay to have it available in the count property of the manager.
I will check this this evening or tomorrow with motor boards and I/O boards (1018/1019).


Regards,
_________
Christophe

jorikcaljouw
Phidgetsian
Posts: 9
Joined: Fri Nov 20, 2009 9:25 am
Location: Eindhoven, The Netherlands
Contact:

Re: PhidgetManager not ready after opening?

Postby jorikcaljouw » Mon Nov 23, 2009 9:57 am

becafuel wrote:[...]
Btw, what is the interest in a manager attach event ? I mean, only physical devices can be attached/detached, not a logical component like the manager.
It would be interesting to know what is really "attach" & "detach" for a Manager and when those event are fired.
Phidgets staff, if you read us ;)

According to the Phidgets Programming Manual, the Attach event of the Phidget Manager class should be "Fired when any Phidget is physically attached to the system" (p. 21). I wanted to update a list of attached phidgets when a device is connected or disconnected from the computer. This isn't a crucial requirement for my application of course, but as far as I understand the API, this functionality should be easiest to implement with the Manager.
[...]
About the Sleep delay, right now I don't have much devices on hand to test, but I suspect that some boards like motors controllers may need some time to report themselves. Here I only have a Phidget Encoder 1052 and I indeed dont need any delay to have it available in the count property of the manager.
I will check this this evening or tomorrow with motor boards and I/O boards (1018/1019).

I only have two I/O boards (1018) and they are fast enough to get a correct count without a Sleep delay. I guess some other phidgets could need a delay to get recognized properly.

becafuel
Phidgeteer!
Posts: 86
Joined: Sat Apr 25, 2009 5:17 am
Location: France
Contact:

Re: PhidgetManager not ready after opening?

Postby becafuel » Mon Nov 23, 2009 10:33 am

I finally think that this behaviour is logical !
I think it is about re-entrancy in the manager object.

If you try this code, you will see that it works perfectly. But only because I don't access the manager object from inside the event handler.

Code: Select all

private void Form1_Load(object sender, EventArgs e)
        {
            Man.Attach += new AttachEventHandler(Man_Attach);
            Enc.Attach += new AttachEventHandler(Enc_Attach);
        }

        void Enc_Attach(object sender, AttachEventArgs e)
        {
            textBox1.AppendText("Attach event ENCODER, "+e.Device.SerialNumber.ToString()+Environment.NewLine);
        }

        void Man_Attach(object sender, AttachEventArgs e)
        {
            textBox1.AppendText("Attach event MANAGER : "+ e.Device.Name + Environment.NewLine);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Man.open();
            Enc.open();
            Thread.Sleep (2000);  // Simulates a wait for user input
            textBox1.AppendText("Devices : " + Man.Devices.Count.ToString() + Environment.NewLine);
        }

You will see that the Manager event handler does indeed detect a newly plugged device (for me in this example it is a 1052 encoder) and that the Encoder attach event handler will correctly report the serial number of the connected encoder.

After that, the correct device.count property can be retrieved.


So everything is fine, except the interface between chair and keyboard, as usual ;) (and we're at least two in this case...)


Edit: the chair/keyboard interface may not be the only faulty device... ;) I've just checked the same technique with the encoder and it works : accessing the encoders.count property from inside the attach event handler does report the correct value (1).
So, there should be something else.


Return to “C#.NET”

Who is online

Users browsing this forum: Google [Bot] and 2 guests