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

📄 jibcomm.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999-2002 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- *//* JibComm.java   revisions   0.01 - March 27, 2001 Captain K          Updated to speed up several operations.  Changed minimum run time from          96 to 63 milliseconds, which is the spec.  Combined multiple datablock          writes wherever possible.  Changed behaviour on finding the button in          a FIRST_BIRTHDAY state, sleeping much less than before.  Tried to take          out as many malloc's as possible, opting to make methods synchronized and          share a byte[] buffer.   0.02   March 30, 2001 Captain K again          1 bug workaround and 1 bug fix.  The fix was that we were sometimes trying          to read the registers when the registers weren't written yet.  Now we check to see          if the registers are done being written to yet, then if they aren't ready,          give the micro the minimum amount of runtime to finish up.  The workaround is a          long standing bug on the part where it won't respond to 1-Wire communication          sometimes.  Its an ugly workaround - if an APDU send fails, we POR and try again.          POR is accomplished by going to normal speed, resetting, kicking to overdrive,          selecting, and making sure the part is there.  See OneWireContainer.doSpeed()   0.03   October 15, 2001          Could it be that the Bad CRC problem is solved?  Thanks to Marc Palmer for working          with me on this one, and Dave S. for helping root through the debug data...          It looks like at some point in a communication, the adapters are dropping down          from overdrive speed (I don't know where, when, or why yet...a project for another day...          or life) but anyhow, doing an Overdrive-Match-ROM makes all the parts on the bus          give presence pulses if you ain't in overdrive.  By switching to Regular-Speed-Match-ROMs          you don't sacrifice anything (whlie at overdrive speed) and you are OK when you rudely          get kicked back to regular speed.   0.04   January, 2002          Lots of updates/fixes.  Put the min runtime back to 96, turns out the JiB likes to run          over and this causes lots of communications failures.  Improved the try schemes.  Fixed a bug when          reading a large amount of data from the button.  Added CRCCorrection table at Tom's request. */package com.dalsemi.onewire.container;import java.io.*;import com.dalsemi.onewire.*;import com.dalsemi.onewire.adapter.*;import com.dalsemi.onewire.utils.*;/** JibComm - an object that implements the necessary communication protocol * to access the Java iButtons.  Note that many methods are now synchronized * because they access global byte arrays.  This should not affect performance, * however, since applications <i>should</i> be using the <code>DSPortAdapter</code> * methods <code>beginExclusive(boolean)</code> and <code>endExclusive()</code> * to synchronize their 1-Wire operations. * * <H3> Usage </H3> * * <DL> * <DD> Used primarily by * {@link com.dalsemi.onewire.container.OneWireContainer16 OneWireContainer16} * </DL> * * * @see com.dalsemi.onewire.container.OneWireContainer16 * * @version    0.04, 23 Jan 2002 * @author     K, JK * * */public class JibComm{   //////////////////////////////////////////////////////////   //  Masks for checking various bits in registers        //   //////////////////////////////////////////////////////////   /** accelerator status mask */   private static final byte COPROCESSOR_ACCELERATOR_RUNNING = ( byte ) 0x01;   /** power-on-reset (POR) mask */   private static final byte POWER_ON_RESET = ( byte ) 0x40;   /** command complete status mask */   private static final byte COMMAND_COMPLETE = ( byte ) 0x0B;   /** command not complete status mask */   private static final byte COMMAND_NOT_COMPLETE = ( byte ) 0x20;   private static final byte CE_RESPONSE_INCOMPLETE = ( byte ) 0x0c;   private static final byte CE_JAVAVM_INCOMPLETE = (byte) 0x0e;   /** first birthday condition mask */   private static final byte FIRST_BIRTHDAY = ( byte ) 0x1D;   /** master Erase problems mask */   private static final byte MASTER_ERASE_PROBLEM = ( byte ) 0x1F;   //////////////////////////////////////////////////////////   //  Device Specific Commands for Java iButton.          //   //////////////////////////////////////////////////////////   /** write IPR commmand */   private static final byte WRITE_IPR_COMMAND = ( byte ) 0x0F;   /** read IPR Command */   private static final byte READ_IPR_COMMAND = ( byte ) 0xAA;   /** write I/O buffer command */   private static final byte WRITE_IO_BUFFER_COMMAND = ( byte ) 0x2D;   /** read I/O buffer command */   private static final byte READ_IO_BUFFER_COMMAND = ( byte ) 0x22;   /** interrupt Micro command */   private static final byte INTERRUPT_MICRO_COMMAND = ( byte ) 0x77;   /** run Micro command */   private static final byte RUN_MICRO_COMMAND = ( byte ) 0x87;   /** reset Micro command */   private static final byte RESET_MICRO_COMMAND = ( byte ) 0xDD;   /** read Status command */   private static final byte READ_STATUS_COMMAND = ( byte ) 0xE1;   /** write Status command */   private static final byte WRITE_STATUS_COMMAND = ( byte ) 0xD2;   /** header size of data block */   private static final int HEADER_SIZE = 8;   /** maximum data block size to send or receive */   // there are 8 bytes of overhead in each block of data send   private final int MAX_BLOCK_SIZE = 128 - 8;   /** minimum run time for this Java iButton in milliseconds.   * This number has been increased from the value specified in the   * data sheet.  This longer time to sleep attempts to keep the host   * from interrupting this Java iButton.   */   private static int MIN_RUNTIME_IN_MILLIS = 96;   private static int RUNTIME_MULTIPLIER = 290;   /** maximum run time for this Java iButton in milliseconds */   private static final int MAX_RUNTIME_IN_MILLIS = 3813;   /** minimum run time value for the status register (OWUS) */   private static final int MIN_RUNTIME = 0;   /** maximum run time value for the status register (OWUS) */   private static final int MAX_RUNTIME = 15;   /** sending bytes to this Java iButton */   private static final int SEND = 0x01;   /** receiving bytes from this Java iButton */   private static final int RECEIVE = 0x02;   /** adapter used to communicate with this Java iButton */   private DSPortAdapter adapter;   /** byte array containing iButtonAddress(ID) */   private byte[] address = new byte [8];   /** enable/disable debug messages */   public static boolean doDebugMessages = false;   /** power-on-reset (POR) correction status.   The POR will always be corrected at the begining of a   transfer.   */   private boolean shouldCorrectPOR = false;   /** 0xFF arrays for quick array initialization */   private byte[] ffBlock;   /* used in lieu of an adapter.select() call */   private byte[] select_buffer = new byte[9];   public int min_read_runtime = 0;   /* byte[ buffers used by specific methods */   private byte[] transfer_jib_status = new byte[4];   private byte[] transfer_jib_header = new byte [HEADER_SIZE];   private byte[] get_header_buffer = new byte[14];   private byte[] command_buffer = new byte[16];   private byte[] check_status = new byte[4];   private byte[] set_status_command_buffer = new byte[6];   private byte[] set_header_buffer = new byte[4 + HEADER_SIZE + 2];   private byte[] run_release_buffer = { RUN_MICRO_COMMAND, ( byte ) 0x73, ( byte ) 0x5D};   private byte[] interrupt_release_buffer = {INTERRUPT_MICRO_COMMAND, ( byte ) 0x43, ( byte ) 0x6D};   /* used only in the sendCommand method */   private byte[] buffer = new byte[200];   /* keeps us from having to create a new object on every apdu sent */   private BlockDataFragmenter fragger = new BlockDataFragmenter();   private OneWireContainer16 container = null;   private static int[] CRCCorrectionTable = new int[140];   static   {        CRCCorrectionTable[2] = CRC16.compute(1, 0);        for (int i = 3; i < CRCCorrectionTable.length; i++)        {            CRCCorrectionTable[i] = CRC16.compute(0, CRCCorrectionTable[i-1]);        }   }    private static int getCRC16Correction(int p_Length)    {        if(p_Length >= CRCCorrectionTable.length)            return 0;        if(p_Length < 1)            return 0;        return CRCCorrectionTable[p_Length-1];    }   /**    * Sets the time given by a host for the Java Powered <u>i</u>Button to    * perform its task. In some cases, increases in this value may help    * avoid communications errors.  63 milliseconds is the absolute lowest    * rated value, but 96 milliseconds makes for fewer errors.    *    * @param runtime minimum runtime the host gives a Java <u>i</u>Button to perform its task (in ms)    *    * @throws IllegalArgumentException on illegal run time values (must be at least 63)    */   public static void setMinRuntime(int runtime)   {       if (runtime > 62)           MIN_RUNTIME_IN_MILLIS = runtime;       else throw new IllegalArgumentException("Minimum runtime must be at least 63 milliseconds");   }   /**    * Sets the incremental increase in runtime a host will give a    * Java Powered <u>i</u>Button to perform its task. Beyond the initial    * minimum runtime, the Java <u>i</u>Button only understands increments    * of 250 milliseconds.  However, due to clock and timing variations,    * this value may need to be altered.  CRC errors are common if this value    * is too low for a given operation. 250 milliseconds is the absolute lowest    * rated value.  290 milliseconds is the default.    *    * @param multiplier new incremental runtime increase value to be used    *                   for this Java <u>i</u>Button to perform its task (in ms)    *    * @throws IllegalArgumentException on illegal multiplier values (must be at least 250)    */   public static void setRuntimeMultiplier(int multiplier)   {       if (multiplier > 249)           RUNTIME_MULTIPLIER = multiplier;       else throw new IllegalArgumentException("Minimum multiplier is 250 milliseconds");   }   //-------------------------------------------------------------------------   /** Constructs a <code>JibComm</code> object to communicate with   * this Java iButton.   *   * @param newAdapter adapter used to communicate with this Java iButton   * @param newAddress address of this Java iButton   *   * @throws IllegalArgumentException Invalid Java iButton address   *   * @see com.dalsemi.onewire.utils.Address   */   public JibComm (OneWireContainer16 owc, DSPortAdapter newAdapter, byte[] newAddress)      throws IllegalArgumentException   {      container = owc;      // Check to see if the length of the address is correct.      if (newAddress.length != 8)         throw new IllegalArgumentException("iButton Address must be of length 8.");      System.arraycopy(newAddress, 0, address, 0, address.length);      System.arraycopy(newAddress,0,select_buffer,1,8);      //select_buffer[0] = (byte)0x69;      select_buffer[0] = (byte)0x55;      adapter = newAdapter;      // create an array of 0xFF for quick array fill      ffBlock = new byte [130];      for (int i = 0; i < 130; i++)         ffBlock [i] = ( byte ) 0xFF;   }   private void datablock(byte[] buf, int start, int off)     throws OneWireException   {      try      {          if ((doDebugMessages) && (buf[0]!=0x55))          {              System.out.println("Found a non-55 beginning..."+Integer.toHexString(buf[0]&0x0ff));          }          buf[0] = (byte)0x55;          adapter.reset();          adapter.dataBlock(buf, start, off);      }      catch(OneWireException owe)      {          if (doDebugMessages) System.out.println("Retrying a datablock...");          /* Note!!! This is a hack for the older, crappy USB fobs that           * shipped with the first Java iButton 2-in-1's.  They don't like           * about 50% of the first datablocks that happen after           * a strong pull-up, which is a bad thing with the JiB,           * since everything it does (just about) needs a strong           * pull up.  So if we get a failure, try to put the part           * at normal speed, then back to overdrive and see if the part is           * present (that's done in the doSpeed method)           */          adapter.setSpeed(DSPortAdapter.SPEED_REGULAR);          container.doSpeed();          adapter.reset();          buf[0] = (byte)0x55;          adapter.dataBlock(buf, start, off);          /* Still, even with these heroic effots, some operations

⌨️ 快捷键说明

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