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

📄 onewirecontainer10.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    *    * @see    #doTemperatureConvert    */   public double getTemperature (byte[] state)      throws OneWireIOException   {      //on some parts, namely the 18S20, you can get invalid readings.      //basically, the detection is that all the upper 8 bits should      //be the same by sign extension.  the error condition (DS18S20      //returns 185.0+) violated that condition      if (((state [1] & 0x0ff) != 0x00) && ((state [1] & 0x0ff) != 0x0FF))         throw new OneWireIOException(address, "Invalid temperature data!");      short temp = ( short ) ((state [0] & 0x0ff) | (state [1] << 8));      if (state [4] == 1)      {         temp = ( short ) (temp >> 1);   //lop off the last bit         //also takes care of the / 2.0         double tmp = ( double ) temp;         double cr  = (state [6] & 0x0ff);         double cpc = (state [7] & 0x0ff);         //just let the thing throw a divide by zero exception         tmp = tmp - ( double ) 0.25 + (cpc - cr) / cpc;         return tmp;      }      else      {         //do normal resolution         return temp / 2.0;      }   }   /**    * Gets the specified temperature alarm value in Celsius from the    * <code>state</code> data retrieved from the  <code>readDevice()</code>    * method.    *    * @param  alarmType valid value: <code>ALARM_HIGH</code> or    *                   <code>ALARM_LOW</code>    * @param  state     byte array with device state information    *    * @return temperature alarm trip values in Celsius for this    *         <code>OneWireContainer10</code>    *    * @see    #hasTemperatureAlarms    * @see    #setTemperatureAlarm    */   public double getTemperatureAlarm (int alarmType, byte[] state)   {      return ( double ) state [alarmType == ALARM_LOW ? 3                                                      : 2];   }   /**    * Gets the current temperature resolution in Celsius from the    * <code>state</code> data retrieved from the <code>readDevice()</code>    * method.    *    * @param  state byte array with device state information    *    * @return temperature resolution in Celsius for this    *         <code>OneWireContainer10</code>    *    * @see    #hasSelectableTemperatureResolution    * @see    #getTemperatureResolutions    * @see    #setTemperatureResolution    */   public double getTemperatureResolution (byte[] state)   {      if (state [4] == 0)         return RESOLUTION_NORMAL;      return RESOLUTION_MAXIMUM;   }   //--------   //-------- Temperature 'set' Methods   //--------   /**    * Sets the temperature alarm value in Celsius in the provided    * <code>state</code> data.    * Use the method <code>writeDevice()</code> with    * this data to finalize the change to the device.    *    * @param  alarmType  valid value: <code>ALARM_HIGH</code> or    *                    <code>ALARM_LOW</code>    * @param  alarmValue alarm trip value in Celsius    * @param  state      byte array with device state information    *    * @see    #hasTemperatureAlarms    * @see    #getTemperatureAlarm    */   public void setTemperatureAlarm (int alarmType, double alarmValue,                                    byte[] state)   {      if ((alarmType != ALARM_LOW) && (alarmType != ALARM_HIGH))         throw new IllegalArgumentException("Invalid alarm type.");      if (alarmValue > 100.0 || alarmValue < -55.0)         throw new IllegalArgumentException(            "Value for alarm not in accepted range.  Must be -55 C <-> +100 C.");      state [(alarmType == ALARM_LOW) ? 3                                      : 2] = ( byte ) alarmValue;   }   /**    * Sets the current temperature resolution in Celsius in the provided    * <code>state</code> data.   Use the method <code>writeDevice()</code>    * with this data to finalize the change to the device.    *    * @param  resolution temperature resolution in Celsius. Valid values are    *                    <code>RESOLUTION_NORMAL</code> and    *                    <code>RESOLUTION_MAXIMUM</code>.    * @param  state      byte array with device state information    *    * @see    #RESOLUTION_NORMAL    * @see    #RESOLUTION_MAXIMUM    * @see    #hasSelectableTemperatureResolution    * @see    #getTemperatureResolution    * @see    #getTemperatureResolutions    */   public void setTemperatureResolution (double resolution, byte[] state)   {      synchronized (this)      {         if (resolution == RESOLUTION_NORMAL)            normalResolution = true;         else            normalResolution = false;         state [4] = ( byte ) (normalResolution ? 0                                                : 1);      }   }   /**    * Retrieves this <code>OneWireContainer10</code> state information.    * The state information is returned as a byte array.  Pass this byte    * array to the '<code>get</code>' and '<code>set</code>' methods.    * If the device state needs to be changed, then call the    * <code>writeDevice()</code> to finalize the changes.    *    * @return <code>OneWireContainer10</code> state information.    * Device state looks like this:    * <pre>    *   0 : temperature LSB    *   1 : temperature MSB    *   2 : trip high    *   3 : trip low    *   4 : reserved (put the resolution here, 0 for normal, 1 for max)    *   5 : reserved    *   6 : count remain    *   7 : count per degree Celsius    *   8 : an 8 bit CRC over the previous 8 bytes of data    * </pre>    *    * @throws OneWireIOException on a 1-Wire communication error such as    *         reading an incorrect CRC from this <code>OneWireContainer10</code>.    *         This could be caused by a physical interruption in the 1-Wire    *         Network due to shorts or a newly arriving 1-Wire device issuing a    *         'presence pulse'.    * @throws OneWireException on a communication or setup error with the 1-Wire    *         adapter    *    * @see    #writeDevice    */   public byte[] readDevice ()      throws OneWireIOException, OneWireException   {      byte[] data = new byte [8];      doSpeed();      // select the device      if (adapter.select(address))      {         // construct a block to read the scratchpad         byte[] buffer = new byte [10];         // read scratchpad command         buffer [0] = ( byte ) READ_SCRATCHPAD_COMMAND;         // now add the read bytes for data bytes and crc8         for (int i = 1; i < 10; i++)            buffer [i] = ( byte ) 0x0FF;         // send the block         adapter.dataBlock(buffer, 0, buffer.length);         // see if crc is correct         if (CRC8.compute(buffer, 1, 9) == 0)            System.arraycopy(buffer, 1, data, 0, 8);         else            throw new OneWireIOException(address, "OneWireContainer10-Error reading CRC8 from device.");      }      else         throw new OneWireIOException(address, "OneWireContainer10-Device not found on 1-Wire Network");      //we are just reading normalResolution here, no need to synchronize      data [4] = ( byte ) (normalResolution ? 0                                            : 1);      return data;   }   /**    * Writes to this <code>OneWireContainer10</code> <code>state</code>    * information that have been changed by '<code>set</code>' methods.    * Only the state registers that changed are updated.  This is done    * by referencing a field information appended to the state data.    *    * @param  state      byte array with device state information    *    * @throws OneWireIOException on a 1-Wire communication error such as    *         reading an incorrect CRC from this <code>OneWireContainer10</code>.    *         This could be caused by a physical interruption in the 1-Wire    *         Network due to shorts or a newly arriving 1-Wire device issuing a    *         'presence pulse'.    * @throws OneWireException on a communication or setup error with the 1-Wire    *         adapter    *    * @see    #readDevice    */   public void writeDevice (byte[] state)      throws OneWireIOException, OneWireException   {      doSpeed();      byte[] temp = new byte [2];      temp [0] = state [2];      temp [1] = state [3];      // Write it to the Scratchpad.      writeScratchpad(temp);      // Place in memory.      copyScratchpad();   }   /**    * Converts a temperature reading from Celsius to Fahrenheit.    *    * @param   celsiusTemperature temperature value in Celsius    *    * @return  the Fahrenheit conversion of the supplied temperature    *    * @deprecated Replace with call to com.dalsemi.onewire.utils.Convert.toFahrenheit()    *    * @see com.dalsemi.onewire.utils.Convert#toFahrenheit(double)    */   static public double convertToFahrenheit (double celsiusTemperature)   {      return Convert.toFahrenheit(celsiusTemperature);   }   /**    * Converts a temperature reading from Fahrenheit to Celsius.    *    * @param  fahrenheitTemperature temperature value in Fahrenheit    *    * @return  the Celsius conversion of the supplied temperature    *    * @deprecated Replace with call to com.dalsemi.onewire.utils.Convert.toCelsius()    *    * @see com.dalsemi.onewire.utils.Convert#toCelsius(double)    */   static public double convertToCelsius (double fahrenheitTemperature)   {      return Convert.toCelsius(fahrenheitTemperature);   }   //--------   //-------- Private Methods   //--------   /**    * Reads the 8 bytes from the scratchpad and verify CRC8 returned.    *    * @param  data  buffer to store the scratchpad data    *    * @throws OneWireIOException on a 1-Wire communication error such as    *         reading an incorrect CRC from this <code>OneWireContainer10</code>.    *         This could be caused by a physical interruption in the 1-Wire    *         Network due to shorts or a newly arriving 1-Wire device issuing a    *         'presence pulse'.    * @throws OneWireException on a communication or setup error with the 1-Wire    *         adapter    */   private void readScratch (byte[] data)      throws OneWireIOException, OneWireException   {      // select the device      if (adapter.select(address))      {         // construct a block to read the scratchpad         byte[] buffer = new byte [10];         // read scratchpad command         buffer [0] = ( byte ) READ_SCRATCHPAD_COMMAND;         // now add the read bytes for data bytes and crc8         for (int i = 1; i < 10; i++)            buffer [i] = ( byte ) 0x0FF;         // send the block         adapter.dataBlock(buffer, 0, buffer.length);         // see if crc is correct         if (CRC8.compute(buffer, 1, 9) == 0)            System.arraycopy(buffer, 1, data, 0, 8);         else            throw new OneWireIOException(address, "OneWireContainer10-Error reading CRC8 from device.");      }      else         throw new OneWireIOException(address, "OneWireContainer10-Device not found on 1-Wire Network");   }   /**    * Writes to the Scratchpad.    *    * @param data this is the data to be written to the scratchpad.  Cannot    *             be more than two bytes in size. First byte of data must be    *             the temperature High Trip Point and second byte must be    *             temperature Low Trip Point.    *    * @throws OneWireIOException on a 1-Wire communication error such as    *         reading an incorrect CRC from this <code>OneWireContainer10</code>.    *         This could be caused by a physical interruption in the 1-Wire    *         Network due to shorts or a newly arriving 1-Wire device issuing a    *         'presence pulse'.    * @throws OneWireException on a communication or setup error with the 1-Wire    *         adapter    * @throws IllegalArgumentException when data length is not equal to <code>2</code>    */   private void writeScratchpad (byte[] data)      throws OneWireIOException, OneWireException, IllegalArgumentException   {      // Variables.      byte[] write_block = new byte [3];      byte[] buffer      = new byte [8];      // First do some error checking.      if (data.length != 2)         throw new IllegalArgumentException(            "Bad data.  Data must consist of only TWO bytes.");      // Prepare the write_block to be sent.      write_block [0] = WRITE_SCRATCHPAD_COMMAND;      write_block [1] = data [0];      write_block [2] = data [1];      // Send the block of data to the DS1920.      if (adapter.select(address))         adapter.dataBlock(write_block, 0, 3);      else         throw new OneWireIOException(address, "OneWireContainer10 - Device not found");      // Check data to ensure correctly recived.      buffer = new byte [8];      readScratch(buffer);      // verify data      if ((buffer [2] != data [0]) || (buffer [3] != data [1]))         throw new OneWireIOException(address, "OneWireContainer10 - data read back incorrect");      return;   }   /**    * Copies the contents of the User bytes of the ScratchPad to the EEPROM.    *    * @throws OneWireIOException on a 1-Wire communication error such as    *         reading an incorrect CRC from this <code>OneWireContainer10</code>.    *         This could be caused by a physical interruption in the 1-Wire    *         Network due to shorts or a newly arriving 1-Wire device issuing a    *         'presence pulse'.    * @throws OneWireException on a communication or setup error with the 1-Wire    *         adapter    */   private void copyScratchpad ()      throws OneWireIOException, OneWireException   {      // select the device      if (adapter.select(address))      {         // send the copy command         adapter.putByte(COPY_SCRATCHPAD_COMMAND);         // Setup Power Delivery         adapter.setPowerDuration(adapter.DELIVERY_INFINITE);         adapter.startPowerDelivery(adapter.CONDITION_NOW);         // delay for 10 ms         try         {            Thread.sleep(10);         }         catch (InterruptedException e){}         // Turn power back to normal.         adapter.setPowerNormal();      }      else         throw new OneWireIOException(address, "OneWireContainer10 - device not found");   }}

⌨️ 快捷键说明

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