Search code examples
javarfid

RFID Phidget Manager attached event


I am trying a program with many phidget rfid readers. This test code works fine and I can load up all the readers and have it worked.

Vector phidgetList = manager.getPhidgets();
          Enumeration phidgetListEnum = phidgetList.elements();
          int count=phidgets.size();
          while(phidgetListEnum.hasMoreElements()) { 
                Phidget phidgetElement = (Phidget) phidgetListEnum
                        .nextElement();
                if (!phidgets.containsKey(phidgetElement.getSerialNumber())) {

                    RFIDTracking rfi = (RFIDTracking) ct.getTracking("rfid")
                            .clone();
                    rfi.setName("rfid clone " + count++);
                    rfi.detect();
                    rfi.setCode(phidgetElement.getSerialNumber());
                    phidgets.put(phidgetElement.getSerialNumber(), rfi);
                    Thread t = new Thread(rfi);
                    t.start();
              }
          }

The problem is when I tried to detect the new readers attached or detached from the system. I used this code

Manager manager;
      manager = new Manager();
      try {
          manager.addAttachListener(new AttachListener() {
                 public void attached(AttachEvent ae)
                 {
                    try
                    {
                       System.out.println("attached" + ((RFIDPhidget)ae.getSource()).getSerialNumber());
                    }
                    catch (PhidgetException ex) { }
                 }
              });
          manager.open();
      } catch (PhidgetException exception) {
          System.err.println(exception.getErrorNumber()+ exception.getDescription());
      }
      // Allow the Phidgets time to attach
      Thread.sleep(1000);

This code could not detect any reader attachment. I found there is no waitForAttachment(time) in the manager. May I know how to solve this. Thank you in advanced


Solution

  • It's Phidget, but not RFIDPhidget. There is no WaitForAttachment in the manager class because it is not necessary. The previous code works fine, but the wait time must be a little bit longer and the program won't terminate before something is attached.

    Manager manager;
    manager = new Manager();
    try {
      manager.addAttachListener(new AttachListener() {
        public void attached(AttachEvent ae)
        {
          try
          {
            System.out.println("attached" + ((Phidget)ae.getSource()).getSerialNumber());
          } catch (PhidgetException ex) { }
        }
      });
      manager.open();
    } catch (PhidgetException exception) {
      System.err.println(exception.getErrorNumber()+ exception.getDescription());
    }
    // Allow the Phidgets time to attach
    Thread.sleep(1000);