📄 userialadapter.java
字号:
* as a starting point in the search. If no more devices are found * then <code>false</code> will be returned. * * @return <code>true</code> if an iButton or 1-Wire device is found. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public boolean findNextDevice () throws OneWireIOException, OneWireException { boolean search_result; try { // acquire exclusive use of the port beginLocalExclusive(); // check for previous last device if (owState.searchLastDevice) { owState.searchLastDiscrepancy = 0; owState.searchFamilyLastDiscrepancy = 0; owState.searchLastDevice = false; return false; } // check for 'first' and only 1 target if ((owState.searchLastDiscrepancy == 0) && (owState.searchLastDevice == false) && (owState.searchIncludeFamilies.length == 1)) { // set the search to find the 1 target first owState.searchLastDiscrepancy = 64; // create an id to set byte[] new_id = new byte [8]; // set the family code new_id [0] = owState.searchIncludeFamilies [0]; // clear the rest for (int i = 1; i < 8; i++) new_id [i] = 0; // set this new ID System.arraycopy(new_id, 0, owState.ID, 0, 8); } // loop until the correct type is found or no more devices do { // perform a search and keep the result search_result = search(owState); if (search_result) { // check if not in exclude list boolean is_excluded = false; for (int i = 0; i < owState.searchExcludeFamilies.length; i++) { if (owState.ID [0] == owState.searchExcludeFamilies [i]) { is_excluded = true; break; } } // if not in exclude list then check for include list if (!is_excluded) { // loop through the include list boolean is_included = false; for (int i = 0; i < owState.searchIncludeFamilies.length; i++) { if (owState.ID [0] == owState.searchIncludeFamilies [i]) { is_included = true; break; } } // check if include list or there is no include list if (is_included || (owState.searchIncludeFamilies.length == 0)) return true; } } // skip the current type if not last device if (!owState.searchLastDevice && (owState.searchFamilyLastDiscrepancy != 0)) { owState.searchLastDiscrepancy = owState.searchFamilyLastDiscrepancy; owState.searchFamilyLastDiscrepancy = 0; owState.searchLastDevice = false; } // end of search so reset and return else { owState.searchLastDiscrepancy = 0; owState.searchFamilyLastDiscrepancy = 0; owState.searchLastDevice = false; search_result = false; } } while (search_result); // device not found return false; } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Copies the 'current' iButton address being used by the adapter into * the array. This address is the last iButton or 1-Wire device found * in a search (findNextDevice()...). * * @param address An array to be filled with the current iButton address. * @see Address */ public void getAddress (byte[] address) { System.arraycopy(owState.ID, 0, address, 0, 8); } /** * Copies the provided 1-Wire device address into the 'current' * array. This address will then be used in the getDeviceContainer() * method. Permits the adapter instance to create containers * of devices it did not find in a search. * * @param address An array to be copied into the current iButton * address. */ public void setAddress (byte[] address) { System.arraycopy(address, 0, owState.ID, 0, 8); } /** * Verifies that the iButton or 1-Wire device specified is present on * the 1-Wire Network. This does not affect the 'current' device * state information used in searches (findNextDevice...). * * @param address device address to verify is present * * @return <code>true</code> if device is present else * <code>false</code>. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter * * @see Address */ public boolean isPresent (byte[] address) throws OneWireIOException, OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); // make sure adapter is present if (uAdapterPresent()) { // check for pending power conditions if (owState.oneWireLevel != LEVEL_NORMAL) setPowerNormal(); // if in overdrive, then use the block method in super if (owState.oneWireSpeed == SPEED_OVERDRIVE) return blockIsPresent(address, false); // create a private OneWireState OneWireState onewire_state = new OneWireState(); // set the ID to the ID of the iButton passes to this method System.arraycopy(address, 0, onewire_state.ID, 0, 8); // set the state to find the specified device onewire_state.searchLastDiscrepancy = 64; onewire_state.searchFamilyLastDiscrepancy = 0; onewire_state.searchLastDevice = false; onewire_state.searchOnlyAlarmingButtons = false; // perform a search if (search(onewire_state)) { // compare the found device with the desired device for (int i = 0; i < 8; i++) if (address [i] != onewire_state.ID [i]) return false; // must be the correct device return true; } // failed to find device return false; } else throw new OneWireIOException("Error communicating with adapter"); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Verifies that the iButton or 1-Wire device specified is present * on the 1-Wire Network and in an alarm state. This does not * affect the 'current' device state information used in searches * (findNextDevice...). * * @param address device address to verify is present and alarming * * @return <code>true</code> if device is present and alarming else * <code>false</code>. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter * * @see Address */ public boolean isAlarming (byte[] address) throws OneWireIOException, OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); // make sure adapter is present if (uAdapterPresent()) { // check for pending power conditions if (owState.oneWireLevel != LEVEL_NORMAL) setPowerNormal(); // if in overdrive, then use the block method in super if (owState.oneWireSpeed == SPEED_OVERDRIVE) return blockIsPresent(address, true); // create a private OneWireState OneWireState onewire_state = new OneWireState(); // set the ID to the ID of the iButton passes to this method System.arraycopy(address, 0, onewire_state.ID, 0, 8); // set the state to find the specified device (alarming) onewire_state.searchLastDiscrepancy = 64; onewire_state.searchFamilyLastDiscrepancy = 0; onewire_state.searchLastDevice = false; onewire_state.searchOnlyAlarmingButtons = true; // perform a search if (search(onewire_state)) { // compare the found device with the desired device for (int i = 0; i < 8; i++) if (address [i] != onewire_state.ID [i]) return false; // must be the correct device return true; } // failed to find any alarming device return false; } else throw new OneWireIOException("Error communicating with adapter"); } finally { // release local exclusive use of port endLocalExclusive(); } } //-------- //-------- Finding iButton options //-------- /** * Set the 1-Wire Network search to find only iButtons and 1-Wire * devices that are in an 'Alarm' state that signals a need for * attention. Not all iButton types * have this feature. Some that do: DS1994, DS1920, DS2407. * This selective searching can be canceled with the * 'setSearchAllDevices()' method. * * @see #setNoResetSearch */ public void setSearchOnlyAlarmingDevices () { owState.searchOnlyAlarmingButtons = true; } /** * Set the 1-Wire Network search to not perform a 1-Wire * reset before a search. This feature is chiefly used with * the DS2409 1-Wire coupler. * The normal reset before each search can be restored with the * 'setSearchAllDevices()' method. */ public void setNoResetSearch () { owState.skipResetOnSearch = true; } /** * Set the 1-Wire Network search to find all iButtons and 1-Wire * devices whether they are in an 'Alarm' state or not and * restores the default setting of providing a 1-Wire reset * command before each search. (see setNoResetSearch() method). * * @see #setNoResetSearch */ public void setSearchAllDevices () { owState.searchOnlyAlarmingButtons = false; owState.skipResetOnSearch = false; } /** * Removes any selectivity during a search for iButtons or 1-Wire devices * by family type. The unique address for each iButton and 1-Wire device * contains a family descriptor that indicates the capabilities of the * device. * @see #targetFamily * @see #targetFamily(byte[]) * @see #excludeFamily * @see #excludeFamily(byte[]) */ public void targetAllFamilies () { // clear the include and exclude family search lists owState.searchIncludeFamilies = new byte [0]; owState.searchExcludeFamilies = new byte [0]; } /** * Takes an integer to selectively search for this desired family type. * If this method is used, then no devices of other families will be * found by getFirstButton() & getNextButton(). * * @param family the code of the family type to target for searches * @see Address * @see #targetAllFamilies */ public void targetFamily (int familyID) { // replace include family array with 1 element array owState.searchIncludeFamilies = new byte [1]; owState.searchIncludeFamilies [0] = ( byte ) familyID; } /** * Takes an array of bytes to use for selectively searching for acceptable * family codes. If used, only devices with family codes in this array * will be found by any of the search methods. * * @param family array of the family types to target for searches * @see Address * @see #targetAllFamilies */ public void targetFamily (byte familyID []) { // replace include family array with new array owState.searchIncludeFamilies = new byte [familyID.length]; System.arraycopy(familyID, 0, owState.searchIncludeFamilies, 0, familyID.length); } /** * Takes an integer family code to avoid when searching for iButtons. * or 1-Wire devices. * If this method is used, then no devices of this family will be * found by any of the search methods. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -