📄 onewirecontainer28.java
字号:
* 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>OneWireContainer28</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 { byte[] temp = new byte [3]; temp [0] = state [2]; temp [1] = state [3]; temp [2] = state [4]; // Write it to the Scratchpad. writeScratchpad(temp); // Place in memory. copyScratchpad(); } //-------- //-------- Custom Methods for this iButton Type //-------- //------------------------------------------------------------------------- /** * Reads the Scratchpad of the DS18B20. * * @return 9-byte buffer representing the scratchpad * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</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 */ public byte[] readScratchpad () throws OneWireIOException, OneWireException { byte[] result_block; // select the device if (adapter.select(address)) { // create a block to send that reads the scratchpad byte[] send_block = new byte [10]; // read scratchpad command send_block [0] = ( byte ) READ_SCRATCHPAD_COMMAND; // now add the read bytes for data bytes and crc8 for (int i = 1; i < 10; i++) send_block [i] = ( byte ) 0xFF; // send the block adapter.dataBlock(send_block, 0, send_block.length); // now, send_block contains the 9-byte Scratchpad plus READ_SCRATCHPAD_COMMAND byte // convert the block to a 9-byte array representing Scratchpad (get rid of first byte) result_block = new byte [9]; for (int i = 0; i < 9; i++) { result_block [i] = send_block [i + 1]; } // see if CRC8 is correct if (CRC8.compute(send_block, 1, 9) == 0) return (result_block); else throw new OneWireIOException( "OneWireContainer28-Error reading CRC8 from device."); } // device must not have been present throw new OneWireIOException( "OneWireContainer28-Device not found on 1-Wire Network"); } //------------------------------------------------------------------------- /** * Writes to the Scratchpad of the DS18B20. * * @param data data to be written to the scratchpad. First * byte of data must be the temperature High Trip Point, the * second byte must be the temperature Low Trip Point, and * the third must be the Resolution (configuration register). * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</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 is of invalid length */ public void writeScratchpad (byte[] data) throws OneWireIOException, OneWireException { // setup buffer to write to scratchpad byte[] writeBuffer = new byte [4]; writeBuffer [0] = WRITE_SCRATCHPAD_COMMAND; writeBuffer [1] = data [0]; writeBuffer [2] = data [1]; writeBuffer [3] = data [2]; // send command block to device if (adapter.select(address)) { adapter.dataBlock(writeBuffer, 0, writeBuffer.length); } else { // device must not have been present throw new OneWireIOException( "OneWireContainer28-Device not found on 1-Wire Network"); } // double check by reading scratchpad byte[] readBuffer; readBuffer = readScratchpad(); if ((readBuffer [2] != data [0]) || (readBuffer [3] != data [1]) || (readBuffer [4] != data [2])) { // writing to scratchpad failed throw new OneWireIOException( "OneWireContainer28-Error writing to scratchpad"); } return; } //------------------------------------------------------------------------- /** * Copies the Scratchpad to the E-squared memory of the DS18B20. * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</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 */ public void copyScratchpad () throws OneWireIOException, OneWireException { // first, let's read the scratchpad to compare later. byte[] readfirstbuffer; readfirstbuffer = readScratchpad(); // second, let's copy the scratchpad. if (adapter.select(address)) { // apply the power delivery adapter.setPowerDuration(adapter.DELIVERY_INFINITE); adapter.startPowerDelivery(adapter.CONDITION_AFTER_BYTE); // send the convert temperature command adapter.putByte(COPY_SCRATCHPAD_COMMAND); // sleep for 10 milliseconds to allow copy to take place. try { Thread.sleep(10); } catch (InterruptedException e){} ; // Turn power back to normal. adapter.setPowerNormal(); } else { // device must not have been present throw new OneWireIOException( "OneWireContainer28-Device not found on 1-Wire Network"); } // third, let's read the scratchpad again with the recallE2 command and compare. byte[] readlastbuffer; readlastbuffer = recallE2(); if ((readfirstbuffer [2] != readlastbuffer [2]) || (readfirstbuffer [3] != readlastbuffer [3]) || (readfirstbuffer [4] != readlastbuffer [4])) { // copying to scratchpad failed throw new OneWireIOException( "OneWireContainer28-Error copying scratchpad to E2 memory."); } } //------------------------------------------------------------------------- /** * Recalls the DS18B20 temperature trigger values (<code>ALARM_HIGH</code> * and <code>ALARM_LOW</code>) and the configuration register to the * scratchpad and reads the scratchpad. * * @return byte array representing data in the device's scratchpad. * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</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 */ public byte[] recallE2 () throws OneWireIOException, OneWireException { byte[] ScratchBuff; // select the device if (adapter.select(address)) { // send the Recall E-squared memory command adapter.putByte(RECALL_E2MEMORY_COMMAND); // read scratchpad ScratchBuff = readScratchpad(); return (ScratchBuff); } // device must not have been present throw new OneWireIOException( "OneWireContainer28-Device not found on 1-Wire Network"); } //------------------------------------------------------------------------- /** * Reads the way power is supplied to the DS18B20. * * @return <code>true</code> for external power, <BR> * <code>false</code> for parasite power * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from this <code>OneWireContainer28</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 */ public boolean isExternalPowerSupplied () throws OneWireIOException, OneWireException { int intresult = 0; boolean result = false; // select the device if (adapter.select(address)) { // send the "Read Power Supply" memory command adapter.putByte(READ_POWER_SUPPLY_COMMAND); // read results intresult = adapter.getByte(); } else { // device must not have been present throw new OneWireIOException( "OneWireContainer28-Device not found on 1-Wire Network"); } if (intresult != 0x00) result = true; // reads 0xFF for true and 0x00 for false return result; } //------------------------------------------------------------------------- /** * 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) */ public float convertToFahrenheit (float celsiusTemperature) { return (float)Convert.toFahrenheit(celsiusTemperature); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -