Interface Kit hangs on connect

Supporting 4.1 and up
blainablin
Phidgetsian
Posts: 14
Joined: Tue Oct 09, 2012 7:27 pm
Location: Caledonia, Ontario
Contact:

Interface Kit hangs on connect

Postby blainablin » Tue Dec 11, 2012 10:21 am

I'm running into an issue when connecting an Interface Kit to my tablet.

Here's the sequence of events that I'm expecting to happen:

1. Attach and Detach handlers are setup for Manager
2. SensorChange handler is setup for InterfaceKitPhidget
3. Manger is opened
4. I physically attach InterfaceKitPhidget
5. Attach event fires in Manager
6. Manager sends Serial Number to Attach handler where InterfaceKitPhidget is opened
7. SensorChange events fire for all 8 sensors. I use these initial events to trigger an Initialize method that sets DataRates and Sensitivity on the analog inputs.
8. Away we go with normal operation

Occasionally (maybe 40% of the time) this works fine. I would say the majority of time things get stuck at step 6. Step 7 doesn't happen UNTIL I physically disconnect the Interface Kit. Then the SensorChange events come through followed by the Detach event.

Below are my event handlers and listeners. I've tried puting a Thread.sleep() in before and after the ik.open() command thinking that it needed time to complete the Attach or open operations but it had no affect.

Any advice?


Listeners:

Code: Select all


         phManager.addAttachListener(new AttachListener() {
            public void attached(final AttachEvent attachEvent) {
               IkAttachEventHandler handler = null;
               int deviceId, deviceSerial;
               try {
                  deviceId = attachEvent.getSource().getDeviceClass();
                  deviceSerial = attachEvent.getSource().getSerialNumber();
               
                  if (deviceId == 7 )   {  // 7 is Interface Kit
                     handler = new IkAttachEventHandler(deviceSerial);
                  }   
               } catch (PhidgetException pe) {
                  Log.e(TAG, pe.toString());
               }

               if (handler!=null) {
                  // This is synchronised in case more than one device is attached before one completes attaching
                  synchronized(handler) {
                     runOnUiThread(handler);
                     try {
                        handler.wait();
                     } catch (InterruptedException e) {
                        Log.e(TAG, e.toString());
                     }
                  }

               }
            }
         });

         phManager.addDetachListener(new DetachListener() {
            public void detached(final DetachEvent detachEvent) {
               DetachEventHandler handler = new DetachEventHandler(detachEvent.getSource());
               // This is synchronised in case more than one device is attached before one completes attaching
               synchronized(handler) {
                  runOnUiThread(handler);
                  try {
                     handler.wait();
                     
                  } catch (InterruptedException e) {
                     Log.e(TAG,e.toString());
                  }
               }
            }
         });

          ik.addSensorChangeListener(new SensorChangeListener() {
            public void sensorChanged(SensorChangeEvent se) {
               runOnUiThread(new SensorChangeRunnable(se.getIndex(), se.getValue()));
            }
         });






Handlers:

Code: Select all


   class IkAttachEventHandler implements Runnable {
       int deviceSerial;
       TextView deviceStatus;

       public IkAttachEventHandler(int deviceSerial) {
          this.deviceSerial = deviceSerial;
       }

       public void run() {
          LogThis(LOGINFO, "Interface Kit attached...");
         
         
          try {
             //Thread.sleep(2000);  // Gives time for connection to finish before we open it
             ik.open(deviceSerial);
             LogThis(LOGINFO, "Interface Kit " + Integer.toString(deviceSerial) + " opened.");
         
             scaleData = new ScaleData();


            
          } catch (PhidgetException pe) {
             LogThis(LOGERROR, pe.toString());
          } catch (Exception e ) {
             LogThis(LOGERROR, e.toString());
          }
   
          // Notify that we're done
          synchronized(this) {
             this.notify();
          }
       }
    }

   class DetachEventHandler implements Runnable {
       Phidget device;
   
       public DetachEventHandler(Phidget device) {
          this.device = device;
       }
   
       public void run() {

          try {
      
             if (device.getDeviceClass()==7) {    // 7 is Interface Kit
                Log.i(TAG, "Interface Kit detached...");
                scaleData = null;
                //ik.close();
         
                Log.i(TAG, "Interface Kit closed");
         
                deviceStatus.setText("Scale not attached");
                btnSave.setEnabled(false);
                btnZero.setEnabled(false);
             }
          } catch (PhidgetException e) {
             LogThis(LOGINFO, e.toString());
          } catch (Exception e) {
             LogThis(LOGERROR, e.toString());
          }
      
          // Notify that we're done
          synchronized(this) {
             this.notify();
          }
       }
    }

   class SensorChangeRunnable implements Runnable {
      int sensorIndex, sensorVal, totalWeight;
      
      public SensorChangeRunnable(int index, int val)
      {
         this.sensorIndex = index;
         this.sensorVal = val;
      }
      public void run() {

         
         try {
            LogThis(LOGINFO, "Input change event. Index: " + Integer.toString(sensorIndex) + " Value: " + Integer.toString(sensorVal));
            if (scaleData.getInitialized()) {
               UpdateWeightDisplay(scaleData.SaveSensorData(sensorIndex, sensorVal));
            } else {
               if (!scaleData.isSensorsReady()) {
                  totalWeight = scaleData.SaveSensorData(sensorIndex, sensorVal);
               }
               if (scaleData.isSensorsReady()) {
                  InitializeScale();
               }
            }
         
         } catch (Exception e) {
            LogThis(LOGERROR, e.toString());
         }

      }
   }   


Windows XP, Nextbook Premium 8 SE, Android 4.0.3, InterfaceKit 8/8/8, Load cells

User avatar
burley
Human-Cyborg Relations
Posts: 441
Joined: Tue Sep 27, 2011 2:37 pm
Location: Calgary
Contact:

Re: Interface Kit hangs on connect

Postby burley » Tue Dec 11, 2012 12:19 pm

You should really be using .waitforattachment() after you open the interface kit. This will pause the code until the device is properly opened. You can use an argument for a timeout in ms if you do not want the code to hang on the event of a failure during opening. No argument means it will wait indefinitely if it has to.

e.g.
ik.waitforattachment(2000);
Brian Burley
403-282-7335 ext. 6003
support@phidgets.com

blainablin
Phidgetsian
Posts: 14
Joined: Tue Oct 09, 2012 7:27 pm
Location: Caledonia, Ontario
Contact:

Re: Interface Kit hangs on connect

Postby blainablin » Tue Dec 11, 2012 1:48 pm

Unfortunately that didn't help.

It seems to get through the handler and open process but then hangs. I stuck a log command in right after the runOnUiThread(handler); in the addAttachListener section and the log entry happens but then everything stops until I detach the Interface Kit. Like I said before, after the detach the sensorChange events come through.

Does it have something to do with the synchronization? I haven't got my head around that stuff yet so I don't know where to start on diagnosing that.

Thanks for your attention.
Windows XP, Nextbook Premium 8 SE, Android 4.0.3, InterfaceKit 8/8/8, Load cells


Return to “Android”

Who is online

Users browsing this forum: No registered users and 1 guest