📄 onewirecontainer41.java
字号:
* the address of the iButton or 1-Wire device. * * @param sourceAdapter adapter object required to communicate with * this iButton * @param newAddress address of this 1-Wire device * @see com.dalsemi.onewire.utils.Address */ public void setupContainer(DSPortAdapter sourceAdapter, long newAddress) { super.setupContainer(sourceAdapter, newAddress); // initialize the memory banks initMem(); setContainerVariables(null); } /** * Provides this container with the adapter object used to access this device and * the address of the iButton or 1-Wire device. * * @param sourceAdapter adapter object required to communicate with * this iButton * @param newAddress address of this 1-Wire device * @see com.dalsemi.onewire.utils.Address */ public void setupContainer(DSPortAdapter sourceAdapter, String newAddress) { super.setupContainer(sourceAdapter, newAddress); // initialize the memory banks initMem(); setContainerVariables(null); }// *****************************************************************************// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// Sensor read/write// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// ***************************************************************************** /** * Retrieves the 1-Wire device sensor state. This state is * returned as a byte array. Pass this byte array to the 'get' * and 'set' methods. If the device state needs to be changed then call * the 'writeDevice' to finalize the changes. * * @return 1-Wire device sensor state * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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[] readDevice () throws OneWireIOException, OneWireException { byte[] buffer = new byte [96]; int retryCnt = MAX_READ_RETRY_CNT; do { try { //going to return the register pages, 96 bytes register.readPageCRC(0, false, buffer, 0); register.readPageCRC(1, true, buffer, 32); register.readPageCRC(2, true, buffer, 64); retryCnt = MAX_READ_RETRY_CNT; } catch(OneWireIOException owioe) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) Debug.debug( "readDevice exc, retryCnt=" + retryCnt, owioe); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(--retryCnt==0) throw owioe; } catch(OneWireException owe) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) Debug.debug( "readDevice exc, retryCnt=" + retryCnt, owe); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(--retryCnt==0) throw owe; } } while(retryCnt<MAX_READ_RETRY_CNT); if(!isContainerVariablesSet) setContainerVariables(buffer); return buffer; } /** * Writes the 1-Wire device sensor state that * have been changed by 'set' methods. Only the state registers that * changed are updated. This is done by referencing a field information * appended to the state data. * * @param state 1-Wire device sensor state * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 writeDevice (byte[] state) throws OneWireIOException, OneWireException { int start = updatertc ? 0 : 6; register.write(start, state, start, 32-start); synchronized (this) { updatertc = false; } } /** * Reads a single byte from the DS1922. Note that the preferred manner * of reading from the DS1922 Thermocron is through the <code>readDevice()</code> * method or through the <code>MemoryBank</code> objects returned in the * <code>getMemoryBanks()</code> method. * * @param memAddr the address to read from (in the range of 0x200-0x21F) * * @return the data byte read * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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() * @see #getMemoryBanks() */ public byte readByte (int memAddr) throws OneWireIOException, OneWireException { // break the address up into bytes byte msbAddress = (byte)((memAddr >> 8) & 0x0ff); byte lsbAddress = (byte)(memAddr & 0x0ff); /* check the validity of the address */ if ((msbAddress > 0x2F) || (msbAddress < 0)) throw new IllegalArgumentException( "OneWireContainer41-Address for read out of range."); int numBytesToEndOfPage = 32 - (lsbAddress&0x1F); byte[] buffer = new byte [11 + numBytesToEndOfPage + 2]; if (doSpeedEnable) doSpeed(); if (adapter.select(address)) { buffer [0] = READ_MEMORY_CRC_PW_COMMAND; buffer [1] = lsbAddress; buffer [2] = msbAddress; if(isContainerReadWritePasswordSet()) getContainerReadWritePassword(buffer, 3); else getContainerReadOnlyPassword(buffer, 3); for(int i=11; i<buffer.length; i++) buffer [i] = (byte)0x0ff; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) Debug.debug("Send-> ", buffer); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ adapter.dataBlock(buffer, 0, buffer.length); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) Debug.debug("Recv<- ", buffer); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ // exclude password from CRC 16 if(CRC16.compute(buffer, 11, buffer.length-11, CRC16.compute(buffer, 0, 3, 0)) != 0x0000B001) throw new OneWireIOException( "Invalid CRC16 read from device. Password may be incorrect or a sample may be in progress."); return buffer [11]; } else throw new OneWireException("OneWireContainer41-Device not present."); } /** * <p>Gets the status of the specified flag from the specified register. * This method actually communicates with the DS1922. To improve * performance if you intend to make multiple calls to this method, * first call <code>readDevice()</code> and use the * <code>getFlag(int, byte, byte[])</code> method instead.</p> * * <p>The DS1922 has several sets of flags.</p> * <ul> * <LI>Register: <CODE> TEMPERATURE_CONTROL_REGISTER </CODE><BR> * Flags: * <UL> * <li><code> TCR_BIT_ENABLE_TEMPERATURE_LOW_ALARM </code></li> * <li><code> TCR_BIT_ENABLE_TEMPERATURE_HIGH_ALARM </code></li> * </UL> * </LI> * <LI>Register: <CODE> DATA_CONTROL_REGISTER </CODE><BR> * Flags: * <UL> * <li><code> DCR_BIT_ENABLE_DATA_LOW_ALARM </code></li> * <li><code> DCR_BIT_ENABLE_DATA_HIGH_ALARM </code></li> * </UL> * </LI> * <LI>Register: <CODE> RTC_CONTROL_REGISTER </CODE><BR> * Flags: * <UL> * <li><code> RCR_BIT_ENABLE_OSCILLATOR </code></li> * <li><code> RCR_BIT_ENABLE_HIGH_SPEED_SAMPLE </code></li> * </UL> * </LI> * <LI>Register: <CODE> MISSION_CONTROL_REGISTER </CODE><BR> * Flags: * <UL> * <li><code> MCR_BIT_ENABLE_TEMPERATURE_LOGGING </code></li> * <li><code> MCR_BIT_ENABLE_DATA_LOGGING </code></li> * <li><code> MCR_BIT_TEMPERATURE_RESOLUTION </code></li> * <li><code> MCR_BIT_DATA_RESOLUTION </code></li> * <li><code> MCR_BIT_ENABLE_ROLLOVER </code></li> * <li><code> MCR_BIT_START_MISSION_UPON_TEMPERATURE_ALARM </code></li> * </UL> * </LI> * <LI>Register: <CODE> ALARM_STATUS_REGISTER </CODE><BR> * Flags: * <UL> * <li><code> ASR_BIT_TEMPERATURE_LOW_ALARM </code></li> * <li><code> ASR_BIT_TEMPERATURE_HIGH_ALARM </code></li> * <li><code> ASR_BIT_DATA_LOW_ALARM </code></li> * <li><code> ASR_BIT_DATA_HIGH_ALARM </code></li> * <li><code> ASR_BIT_BATTERY_ON_RESET </code></li> * </UL> * </LI> * <LI>Register: <CODE> GENERAL_STATUS_REGISTER </CODE><BR> * Flags: * <UL> * <li><code> GSR_BIT_SAMPLE_IN_PROGRESS </code></li> * <li><code> GSR_BIT_MISSION_IN_PROGRESS </code></li> * <li><code> GSR_BIT_MEMORY_CLEARED </code></li> * <li><code> GSR_BIT_WAITING_FOR_TEMPERATURE_ALARM </code></li> * </UL> * </LI> * </ul> * * @param register address of register containing the flag (see above for available options) * @param bitMask the flag to read (see above for available options) * * @return the status of the flag, where <code>true</code> * signifies a "1" and <code>false</code> signifies a "0" * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #getFlag(int,byte,byte[]) * @see #readDevice() * @see #setFlag(int,byte,boolean) */ public boolean getFlag (int register, byte bitMask) throws OneWireIOException, OneWireException { int retryCnt = MAX_READ_RETRY_CNT; while(true) { try { return ((readByte(register) & bitMask) != 0); } catch(OneWireException owe) { if(--retryCnt==0) throw owe; } } } /** * <p>Gets the status of the specified flag from the specified register. * This method is the preferred manner of reading the control and * status flags.</p> * * <p>For more information on valid values for the <code>bitMask</code> * parameter, see the {@link #getFlag(int,byte) getFlag(int,byte)} method.</p> * * @param register address of register containing the flag (see * {@link #getFlag(int,byte) getFlag(int,byte)} for available options) * @param bitMask the flag to read (see {@link #getFlag(int,byte) getFlag(int,byte)} * for available options) * @param state current state of the device returned from <code>readDevice()</code> * * @return the status of the flag, where <code>true</code> * signifies a "1" and <code>false</code> signifies a "0" * * @see #getFlag(int,byte) * @see #readDevice() * @see #setFlag(int,byte,boolean,byte[]) */ public boolean getFlag (int register, byte bitMask, byte[] state) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -