Multiple Interfacekits mixed up

Supporting Visual Studio on Windows
brinkand
Fresh meat
Posts: 2
Joined: Tue Mar 27, 2012 5:20 am
Contact:

Multiple Interfacekits mixed up

Postby brinkand » Tue Mar 27, 2012 5:57 am

I am writing test system software, where I measure analog values using a 1018 and have a number of 1017 relay boards for switching between test devices.

My problem is that the boards get mixed up during initialization. If I only initialize one ( the 1018) it works fine. I can read analog values just fine. When I initialize the second board (a 1017), the first board suddenly appears to be a 1017.

I am not sure where to look for the problem. Please point me in the right direction. Below is some code showing what I am doing.

I have created an object for each phidget board, and placed them all in an array for easier access.:

Code: Select all

static PhidgetBoard[] Phidgets;


I initialize using:

Code: Select all

for (int cnt = 0; cnt <= 7; cnt++)
  {
      Phidgets[cnt] = new PhidgetBoard();
      Phidgets[cnt].somethingHappened += WriteStatus;  // Write to status box
      int serial = applSettings.getSerial(cnt);
      Phidgets[cnt].Open( serial );
  }


My phidget class constructor (first part - based on Phidget examples and manuals)

Code: Select all

class PhidgetBoard
    {
        //Declare an InterfaceKit object
        static InterfaceKit ifKit;
        static bool BA = false;
        public bool BoardAvailable
        {
            get { return BA; }
            set { BA = value; }
        }
       
        public delegate void MessageCallback(string message);
        public event MessageCallback somethingHappened;
       
       
        public PhidgetBoard()  //Constructor
        {
            try
            {
                //Initialize the InterfaceKit object
                ifKit = new InterfaceKit();
                //Hook the basic event handlers
                ifKit.Attach += new AttachEventHandler(ifKit_Attach);
                ifKit.Detach += new DetachEventHandler(ifKit_Detach);
                ifKit.Error += new ErrorEventHandler(ifKit_Error);

                //Hook the phidget spcific event handlers
                ifKit.InputChange += new InputChangeEventHandler(ifKit_InputChange);
                ifKit.OutputChange += new OutputChangeEventHandler(ifKit_OutputChange);
                ifKit.SensorChange += new SensorChangeEventHandler(ifKit_SensorChange);

               
               
            }
            catch (PhidgetException ex)
            {
                somethingHappened(ex.Description.ToString());
            }
        }

        public void Open(int serial)
        {
            try
            {
                ifKit.open(serial);
                ifKit.waitForAttachment(100);
            }
            catch
            {
                somethingHappened("Error opening Phidget. Probably timeout. " + serial.ToString());       

            }
           
        }

        public void Close()
        {
            if (ifKit != null)
            {
                if (ifKit.Attached)
                {
                    //UnHook the basic event handlers
                    ifKit.Attach -= new AttachEventHandler(ifKit_Attach);
                    ifKit.Detach -= new DetachEventHandler(ifKit_Detach);
                    ifKit.Error -= new ErrorEventHandler(ifKit_Error);

                    //UnHook the phidget spcific event handlers
                    ifKit.InputChange -= new InputChangeEventHandler(ifKit_InputChange);
                    ifKit.OutputChange -= new OutputChangeEventHandler(ifKit_OutputChange);
                    ifKit.SensorChange -= new SensorChangeEventHandler(ifKit_SensorChange);
                    ifKit.close();
                    ifKit = null;
                }
            }
           
        }

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

Re: Multiple Interfacekits mixed up

Postby Patrick » Tue Mar 27, 2012 8:59 am

Your interfacekit object is static - so it will be overwritten every time the constructor gets called.

-Patrick

brinkand
Fresh meat
Posts: 2
Joined: Tue Mar 27, 2012 5:20 am
Contact:

Re: Multiple Interfacekits mixed up

Postby brinkand » Wed Mar 28, 2012 7:02 am

Thanks, this solved the problem. :D


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 5 guests