
Originally Posted by
elcray
Alex:
All depends on if I ever quit tinkering with it! [...] I would love to be able to program around an unknown amount of phidget devices, but I just can't seem to figure it out.
I hear ya about tinkering! I'm always trying to perfect the applications that I build
About the "unknown amount of phidget devices"; I see that you are using VB.NET. Is this perhaps VB.NET 2005, or even VB.NET 2005 Express Edition? The reason why I ask is that I was working on what I called "Dynamic Phidgets" in C# 2005 a while back which used what are called Generics which are unique to Visual Studio 2005 and 2005 Express Edition.
The purpose of "Dynamic Phidgets" was to ignore how many Phidgets were connected to a single machine, and just keep storing them in a Generic Collection as they are attached and removing them when they were detached. Then, I can do what I want with them while they are active in the application I built.
Generics are incredibly powerful, and if you are not familar with them, I highly recommend on reading up on them here, here and here. There are a ton of other examples, but this will help you get started in understanding them if you don't already. Sorry, but these are all C#, but if you understand VB.NET, you understand C#. All of the concepts are the same, the only difference is syntax.
Basically what I did to create Dynamic Phidgets is create a Generic Dictionary Phidget object with the key of the Dictionary being a long variable which will be the serial number of each Phidget:
Code:
private Dictionary<long, Phidgets.Phidget> phidgetDictionary = new Dictionary<long, Phidgets.Phidget>();
Then, I created a new Phidget Manager object, and intialized it:
Code:
private void InitializePhidgetManager()
{
PM = new Phidgets.Manager();
PM.Attach += new Phidgets.Events.AttachEventHandler(PM_Attach);
PM.Detach += new Phidgets.Events.DetachEventHandler(PM_Detach);
PM.Error += new Phidgets.Events.ErrorEventHandler(PM_Error);
PM.open();
}
The Phidget Manager object was necessary because you never know what type of Phidget a user connects to the computer and you need to account for this
Finally, I hooked the Attach and Detach events of the Phidget manager and ran checks to see if they existed in the Generic Dictionary Class. If they did/didn't I added/removed them respectively:
Code:
void PM_Detach(object sender, Phidgets.Events.DetachEventArgs e)
{
if (phidgetDictionary.ContainsKey(e.Device.SerialNumber))
{
phidgetDictionary.Remove(e.Device.SerialNumber);
EventLogger.LogEvent("Object Deleted from Dictionary:" + e.Device.GetType().ToString() +
" Serial Number:" + e.Device.SerialNumber);
}
}
void PM_Attach(object sender, Phidgets.Events.AttachEventArgs e)
{
if (!phidgetDictionary.ContainsKey(e.Device.SerialNumber))
{
phidgetDictionary.Add(e.Device.SerialNumber, e.Device);
EventLogger.LogEvent("Object created and added to Dictionary for:" + e.Device.GetType().ToString() +
" Serial Number:" + e.Device.SerialNumber);
e.Device.open();
e.Device.waitForAttachment(500);
}
}
NOTE: I worked with Phidgets to get the e.Device returned as the actual object itself that it found. This way, you can run a check on the Type of Phidget object that was found and do what you need to with it
This will allow you to ignore the fact of how many Phidgets get connected to the computer. Then, you can simply roll through the Dictionary to find the Phidget objects you have added and do what you need to with them!
I hope all this made sense. Let me know if you have any questions
Bookmarks