netadapter.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 2,064 行 · 第 1/5 页

JAVA
2,064
字号
   /**    * Gets a block of data from the 1-Wire Network and write it into    * the provided array.    *    * @param  arr     array in which to write the received bytes    * @param  len     length of data bytes to receive    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    */   public void getBlock (byte[] arr, int len)      throws OneWireIOException, OneWireException   {      getBlock(arr, 0, len);   }   /**    * Gets a block of data from the 1-Wire Network and write it into    * the provided array.    *    * @param  arr     array in which to write the received bytes    * @param  off     offset into the array to start    * @param  len     length of data bytes to receive    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    */   public void getBlock (byte[] arr, int off, int len)      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send getBlock command            conn.output.writeByte(CMD_GETBLOCK);            // followed by the number of bytes to get            conn.output.writeInt(len);            conn.output.flush();            // check return value for success            checkReturnValue(conn);            // next should be the bytes            conn.input.readFully(arr, off, len);         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }   }   /**    * Sends a block of data and returns the data received in the same array.    * This method is used when sending a block that contains reads and writes.    * The 'read' portions of the data block need to be pre-loaded with 0xFF's.    * It starts sending data from the index at offset 'off' for length 'len'.    *    * @param  dataBlock array of data to transfer to and from the 1-Wire Network.    * @param  off       offset into the array of data to start    * @param  len       length of data to send / receive starting at 'off'    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    */   public void dataBlock (byte[] dataBlock, int off, int len)      throws OneWireIOException, OneWireException   {      if(DEBUG)      {         System.out.println("DataBlock called for " + len + " bytes");      }      try      {         synchronized(conn)         {            // send dataBlock command            conn.output.writeByte(CMD_DATABLOCK);            // followed by the number of bytes to block            conn.output.writeInt(len);            // followed by the bytes            conn.output.write(dataBlock, off, len);            conn.output.flush();            // check return value for success            checkReturnValue(conn);            // next should be the bytes returned            conn.input.readFully(dataBlock, off, len);         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }      if(DEBUG)      {         System.out.println("   Done DataBlocking");      }   }   //--------   //-------- 1-Wire Network power methods   //--------   /**    * Sets the duration to supply power to the 1-Wire Network.    * This method takes a time parameter that indicates the program    * pulse length when the method startPowerDelivery().<p>    *    * Note: to avoid getting an exception,    * use the canDeliverPower() and canDeliverSmartPower()    * method to check it's availability. <p>    *    * @param timeFactor    * <ul>    * <li>   0 (DELIVERY_HALF_SECOND) provide power for 1/2 second.    * <li>   1 (DELIVERY_ONE_SECOND) provide power for 1 second.    * <li>   2 (DELIVERY_TWO_SECONDS) provide power for 2 seconds.    * <li>   3 (DELIVERY_FOUR_SECONDS) provide power for 4 seconds.    * <li>   4 (DELIVERY_SMART_DONE) provide power until the    *          the device is no longer drawing significant power.    * <li>   5 (DELIVERY_INFINITE) provide power until the    *          setPowerNormal() method is called.    * </ul>    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    */   public void setPowerDuration (int timeFactor)      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send setPowerDuration command            conn.output.writeByte(CMD_SETPOWERDURATION);            // followed by the timeFactor            conn.output.writeInt(timeFactor);            conn.output.flush();            // check return value for success            checkReturnValue(conn);         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }   }   /**    * Sets the 1-Wire Network voltage to supply power to a 1-Wire device.    * This method takes a time parameter that indicates whether the    * power delivery should be done immediately, or after certain    * conditions have been met. <p>    *    * Note: to avoid getting an exception,    * use the canDeliverPower() and canDeliverSmartPower()    * method to check it's availability. <p>    *    * @param changeCondition    * <ul>    * <li>   0 (CONDITION_NOW) operation should occur immediately.    * <li>   1 (CONDITION_AFTER_BIT) operation should be pending    *           execution immediately after the next bit is sent.    * <li>   2 (CONDITION_AFTER_BYTE) operation should be pending    *           execution immediately after next byte is sent.    * </ul>    *    * @return <code>true</code> if the voltage change was successful,    * <code>false</code> otherwise.    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    */   public boolean startPowerDelivery (int changeCondition)      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send startPowerDelivery command            conn.output.writeByte(CMD_STARTPOWERDELIVERY);            // followed by the changeCondition            conn.output.writeInt(changeCondition);            conn.output.flush();            // check return value for success            checkReturnValue(conn);            // and get the return value from startPowerDelivery            return conn.input.readBoolean();         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }   }   /**    * Sets the duration for providing a program pulse on the    * 1-Wire Network.    * This method takes a time parameter that indicates the program    * pulse length when the method startProgramPulse().<p>    *    * Note: to avoid getting an exception,    * use the canDeliverPower() method to check it's    * availability. <p>    *    * @param timeFactor    * <ul>    * <li>   7 (DELIVERY_EPROM) provide program pulse for 480 microseconds    * <li>   5 (DELIVERY_INFINITE) provide power until the    *          setPowerNormal() method is called.    * </ul>    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    */   public void setProgramPulseDuration (int timeFactor)      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send setProgramPulseDuration command            conn.output.writeByte(CMD_SETPROGRAMPULSEDURATION);            // followed by the timeFactor            conn.output.writeInt(timeFactor);            conn.output.flush();            // check return value for success            checkReturnValue(conn);         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }   }   /**    * Sets the 1-Wire Network voltage to eprom programming level.    * This method takes a time parameter that indicates whether the    * power delivery should be done immediately, or after certain    * conditions have been met. <p>    *    * Note: to avoid getting an exception,    * use the canProgram() method to check it's    * availability. <p>    *    * @param changeCondition    * <ul>    * <li>   0 (CONDITION_NOW) operation should occur immediately.    * <li>   1 (CONDITION_AFTER_BIT) operation should be pending    *           execution immediately after the next bit is sent.    * <li>   2 (CONDITION_AFTER_BYTE) operation should be pending    *           execution immediately after next byte is sent.    * </ul>    *    * @return <code>true</code> if the voltage change was successful,    * <code>false</code> otherwise.    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    *         or the adapter does not support this operation    */   public boolean startProgramPulse (int changeCondition)      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send startProgramPulse command            conn.output.writeByte(CMD_STARTPROGRAMPULSE);            // followed by the changeCondition            conn.output.writeInt(changeCondition);            conn.output.flush();            // check return value for success            checkReturnValue(conn);            // and get the return value from startPowerDelivery            return conn.input.readBoolean();         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }   }   /**    * Sets the 1-Wire Network voltage to 0 volts.  This method is used    * rob all 1-Wire Network devices of parasite power delivery to force    * them into a hard reset.    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    *         or the adapter does not support this operation    */   public void startBreak ()      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send startBreak command            conn.output.writeByte(CMD_STARTBREAK);            conn.output.flush();            // check return value for success            checkReturnValue(conn);         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED);      }   }   /**    * Sets the 1-Wire Network voltage to normal level.  This method is used    * to disable 1-Wire conditions created by startPowerDelivery and    * startProgramPulse.  This method will automatically be called if    * a communication method is called while an outstanding power    * command is taking place.    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    *         or the adapter does not support this operation    */   public void setPowerNormal ()      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send startBreak command            conn.output.writeByte(CMD_SETPOWERNORMAL);            conn.output.flush();            // check return value for success            checkReturnValue(conn);         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }   }   //--------   //-------- 1-Wire Network speed methods   //--------   /**    * Sets the new speed of data    * transfer on the 1-Wire Network. <p>    *    * @param speed    * <ul>    * <li>     0 (SPEED_REGULAR) set to normal communciation speed    * <li>     1 (SPEED_FLEX) set to flexible communciation speed used    *            for long lines    * <li>     2 (SPEED_OVERDRIVE) set to normal communciation speed to    *            overdrive    * <li>     3 (SPEED_HYPERDRIVE) set to normal communciation speed to    *            hyperdrive    * <li>    >3 future speeds    * </ul>    *    * @throws OneWireIOException on a 1-Wire communication error    * @throws OneWireException on a setup error with the 1-Wire adapter    *         or the adapter does not support this operation    */   public void setSpeed (int speed)      throws OneWireIOException, OneWireException   {      try      {         synchronized(conn)         {            // send startBreak command            conn.output.writeByte(CMD_SETSPEED);            // followed by the speed            conn.output.writeInt(speed);            conn.output.flush();            // check return value for success            checkReturnValue(conn);         }      }      catch(IOException ioe)      {         throw new OneWireException(COMM_FAILED + ioe.getMessage());      }   }   /**    * Returns the current data transfer speed on the 1-Wire Network. <p>    *    * @return <code>int</code> representing the curr

⌨️ 快捷键说明

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