public static final void main(String args[]) throws Exception { RFIDPhidget rfid; System.out.println(Phidget.getLibraryVersion()); rfid = new RFIDPhidget(); rfid.addAttachListener( new AttachListener() { public void attached(AttachEvent ae) { try { ((RFIDPhidget) ae.getSource()).setAntennaOn(true); ((RFIDPhidget) ae.getSource()).setLEDOn(true); } catch (PhidgetException ex) { } System.out.println("attachment of " + ae); } }); rfid.addDetachListener( new DetachListener() { public void detached(DetachEvent ae) { System.out.println("detachment of " + ae); } }); rfid.addErrorListener( new ErrorListener() { public void error(ErrorEvent ee) { System.out.println("error event for " + ee); } }); rfid.addTagGainListener( new TagGainListener() { public void tagGained(TagGainEvent oe) { System.out.println(oe); s = oe.getValue(); } }); rfid.addTagLossListener( new TagLossListener() { public void tagLost(TagLossEvent oe) { System.out.println(oe); } }); rfid.addOutputChangeListener( new OutputChangeListener() { public void outputChanged(OutputChangeEvent oe) { System.out.println(oe); } }); rfid.openAny(); System.out.println("waiting for RFID attachment..."); rfid.waitForAttachment(1000); System.out.println("Serial: " + rfid.getSerialNumber()); System.out.println("Outputs: " + rfid.getOutputCount()); System.out.println("Outputting events. Input to stop."); System.in.read(); System.out.print("closing..."); rfid.close(); rfid = null; System.out.println(" ok"); if (false) { System.out.println("wait for finalization..."); System.gc(); } }
public static void main(String[] args) { // boolean ready = false; // final Vector<Phidget> attachedPhidgets = new Vector<Phidget>(); Manager mgr; try { mgr = new Manager(); mgr.open(); /* * With listeners... */ mgr.addAttachListener( new AttachListener() { @Override public void attached(AttachEvent ae) { try { System.out.println( "Phidget attached: " + ae.getSource().getDeviceName() + " sn=" + ae.getSource().getSerialNumber()); } catch (PhidgetException e) { e.printStackTrace(); } } }); mgr.addDetachListener( new DetachListener() { @Override public void detached(DetachEvent de) { try { System.out.print(de.getSource().getSerialNumber() + " detached."); } catch (PhidgetException e) { e.printStackTrace(); } } }); while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { mgr.close(); } } /* * Without listeners... */ // while(!ready){ // if(!mgr.getPhidgets().isEmpty()){ // // get and create the phidgets // Vector currPhidgets = mgr.getPhidgets(); // for(int i = 0; i < currPhidgets.size(); i++){ // Phidget p = (Phidget) currPhidgets.elementAt(i); // int sn = p.getSerialNumber(); // p.open(sn); // p.waitForAttachment(); // attachedPhidgets.addElement(p); // System.out.println(p.getDeviceName() + ": " + sn + " attached."); // } // ready = true; // } // // try { // Thread.sleep(500); // } catch (InterruptedException e) { // e.printStackTrace(); // } // // } // System.out.println("All done!"); // for(Phidget ap : attachedPhidgets){ // ap.close(); // } // mgr.close(); } catch (PhidgetException e1) { e1.printStackTrace(); } System.exit(0); }