⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jibcardterminal.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      if (driver == null)         return false;      OneWireContainer16 owc16 = javabuttons[slot_id];      if (owc16 == null)      {         return false;      }      try      {          driver.beginExclusive(true);          boolean present = owc16.isPresent();          driver.endExclusive();          return present;      }      catch(OneWireException owe)      {          return false;      }   }   /**    * Gets the CardID from the Java Powered <u>i</u>Button on the specified slot.    *    * @param int the id of the slot    * @return the CardID object containing the ATR of the Java Powered <u>i</u>Button.    * @throws CardTerminalException    */   public CardID getCardID(int slot_id) throws CardTerminalException   {      com.dalsemi.onewire.debug.Debug.debug("getCardID called with slot = " + slot_id);      byte[]    ATR = getCachedATR(slot_id);      if (ATR != null)      {         System.out.println("Returning cached ATR");         return new CardID((opencard.core.terminal.Slot) slots.elementAt(slot_id),                           ATR);      }      else      {         return this.internalReset(slot_id, 500);      }   }   /**    * Get the CardID from the Java Powered <u>i</u>Button on the specified slot.  This    * method will timeout after the specified timeout period.    *    * @param slot_id - the id of the slot    * @param ex_timeout - the period of time to wait before timing out.    *    * @return the CardID object containing the ATR of the Java Powered <u>i</u>Button    * @throws CardTerminalException    */   public CardID getCardID(int slot_id,                           int ex_timeout) throws CardTerminalException   {      byte[]    ATR = getCachedATR(slot_id);      if (ATR != null)      {         com.dalsemi.onewire.debug.Debug.debug("Returning cached ATR");         return new CardID((opencard.core.terminal.Slot) slots.elementAt(slot_id),                           ATR);      }      else      {         return this.internalReset(slot_id, ex_timeout);      }   }   /*    * Kluge to allow getting the ATR from the button when the button    * is not physically connected to the reader.    */   /*    * Get the stored ATR array of bytes and return a copy to the caller.    */   private byte[] getCachedATR(int slot_id)   {      com.dalsemi.onewire.debug.Debug.debug("Getting ATR for: " + slot_id);      byte[]    ATR = (byte[]) ATRArray[slot_id];      if (ATR != null)      {         ATR = (byte[]) ATR.clone();      }      return ATR;   }   /*    * Set the stored ATR array of bytes.    */   private void setCachedATR(int slot_id, byte[] atr)   {      com.dalsemi.onewire.debug.Debug.debug("Setting ATR for slot: " + slot_id);      ATRArray[slot_id] = atr.clone();   }   /**    *    */   protected CardID internalReset(int slot_id,                                  int ms) throws CardTerminalException   {      // driver must be installed      if (driver == null)         throw new CardTerminalException("Driver is not installed");      try      {         OneWireContainer16 owc16 = javabuttons[slot_id];         driver.beginExclusive(true);         byte[] atr = owc16.getATR().getData();         driver.endExclusive();/*         setCachedATR(slot_id, ATR);*/         CardID CardAtr = new CardID((opencard.core.terminal.Slot) slots.elementAt(slot_id), atr);         return CardAtr;      }      catch (Exception e)      {         throw new CardTerminalException("Can not reset the card: " + e);      }   }   /**    * Method called by the OpenCard internals to send an APDU to the    * Java Powered <u>i</u>Button.    *    * @param int the slot number of the Java Powered <u>i</u>Button    * @param opencard.core.terminal.CommandAPDU message to send to the <u>i</u>Button    * @param int time to give the <u>i</u>Button to run    *    * @return message returned by the <u>i</u>Button    * @throws CardTerminalException on communications error with the <u>i</u>Button    */   protected synchronized ResponseAPDU internalSendAPDU(int slot_id, CommandAPDU capdu, int ms) throws CardTerminalException   {      // driver must be installed      if (driver == null)          throw new CardTerminalException("Driver is not installed");      synchronized (driver)      {            try            {                OneWireContainer16 owc16 = javabuttons[slot_id];                com.dalsemi.onewire.container.CommandAPDU ds_capdu = new com.dalsemi.onewire.container.CommandAPDU(capdu.getBytes());                /* this strange little code contraption is due to something in the new jibcomm                 * layer...it expects a value between 0-15, which translates into the runTime                 * like so...                 * Input I, I>=0 and I<=15                 *                 *   runTime = (62.5 + 250 * I) ms                 * so we want I                 *   runTime - 62.5 = 250 * I                 *   (runTime - 62.5) / 250 = I                 *   (runTime*2 - 125) / 500 = I                 *                 * Rather than use flaoting point, I just used a shift so I could do all int's                 *                 * enjoy                 */                int time = ((ms << 1) - 125) / 500;                if (time < 0)                    time = 0;                else if (time > 15)                    time = 15;                driver.beginExclusive(true);                //hack!!! this makes it slow...                driver.setSpeed(DSPortAdapter.SPEED_REGULAR);                com.dalsemi.onewire.container.ResponseAPDU ds_rapdu = owc16.sendAPDU(ds_capdu, time);                driver.endExclusive();                ResponseAPDU rapdu = new ResponseAPDU(ds_rapdu.getBytes());                return rapdu;            }            catch (Exception e)            {               throw new CardTerminalException("Can not communicate with the card " + e.getMessage());            }      }   }   /**    * Method called to poll the One-Wire bus for the presence of a Java Powered <u>i</u>Button.    *    * @throws CardTerminalException on communications error with the <u>i</u>Button    */   public synchronized void poll() throws CardTerminalException   {      if (firstPoll)      {          firstPoll = false;          return;      }//System.out.print("<po");      //first put all the button addresses in buttonaddresses      for (int i=0;i<buttonaddresses.length;i++)      {          added[i] = removed[i] = false;          if (javabuttons[i]!=null)          {              buttonaddresses[i] = javabuttons[i].getAddressAsLong();              //System.out.println("Setting button address "+i+" to "+buttonaddresses[i]);          }          else              buttonaddresses[i] = 0;      }      try      {//System.out.print("<");        driver.beginExclusive(true);//System.out.print(">");        driver.targetFamily(JIB_FAMILY_CODES);        driver.setSpeed(DSPortAdapter.SPEED_REGULAR);        driver.reset();        driver.setSearchAllDevices();        boolean next = driver.findFirstDevice();        while (next)        {            OneWireContainer16 owc16 = (OneWireContainer16)driver.getDeviceContainer();            add(owc16);            next = driver.findNextDevice();        }        driver.endExclusive();      }      catch(Exception e)      {        driver.endExclusive();        System.out.println("Exception: "+e.toString());        e.printStackTrace();      }      //now remove the old parts      for (int i=0;i<buttonaddresses.length;i++)      {          if (buttonaddresses[i] != 0)          {              com.dalsemi.onewire.debug.Debug.debug("Removing card at slot "+i+", val = "+buttonaddresses[i]);              hash.remove(new Long(javabuttons[i].getAddressAsLong()));              javabuttons[i] = null;              //cardRemoved(i);              removed[i] = true;          }      }      //do all the notification here outside of the polling loop      for (int i=0;i<added.length;i++)      {        if (removed[i])            cardRemoved(i);        if (added[i])            cardInserted(i);      }//System.out.print("ll>");   }   private void add(OneWireContainer16 jibButton)   {      if (!hash.containsKey(new Long(jibButton.getAddressAsLong())))      {        com.dalsemi.onewire.debug.Debug.debug("ADDING "+jibButton.getAddressAsString());        for (int i=0;i<javabuttons.length;i++)        {            if (javabuttons[i]==null)            {                //add it here                javabuttons[i] = jibButton;                hash.put(new Long(jibButton.getAddressAsLong()),jibButton);                buttonaddresses[i] = 0;                //cardInserted(i);                added[i] = true;                return;            }        }      }      else //its in the hashtable, so fix up the buttonaddresses[] thing      {          for (int i=0;i<javabuttons.length;i++)          {              if (buttonaddresses[i]==jibButton.getAddressAsLong())              {                  buttonaddresses[i] = 0;                  return;              }          }      }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -