📄 userialadapter.java
字号:
*/ public void freePort () throws OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); // attempt to close the port serial.closePort(); } finally { // release local exclusive use of port endLocalExclusive(); } } //-------- //-------- Adapter detection //-------- /** * Detect adapter presence on the selected port. * * @return <code>true</code> if the adapter is confirmed to be connected to * the selected port, <code>false</code> if the adapter is not connected. * * @throws OneWireIOException * @throws OneWireException */ public boolean adapterDetected () throws OneWireIOException, OneWireException { boolean rt; try { // acquire exclusive use of the port beginLocalExclusive(); uAdapterPresent(); rt = uVerify(); } catch (OneWireException e) { rt = false; } finally { // release local exclusive use of port endLocalExclusive(); } return rt; } /** * Retrieve the version of the adapter. * * @return <code>String</code> of the adapter version. It will return * "<na>" if the adapter version is not or cannot be known. * * @throws OneWireIOException on a 1-Wire communication error such as * no device present. 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 String getAdapterVersion () throws OneWireIOException, OneWireException { String version_string = "DS2480 based adapter"; boolean rt; try { // acquire exclusive use of the port beginLocalExclusive(); // only check if the port is aquired if (uAdapterPresent()) { // perform a reset to read the version if (uState.revision == 0) reset(); version_string = version_string.concat(", version " + (uState.revision >> 2)); return version_string; } else throw new OneWireIOException( "USerialAdapter-getAdapterVersion, adapter not present"); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Retrieve the address of the adapter if it has one. * * @return <code>String</code> of the adapter address. It will return "<na>" if * the adapter does not have an address. The address is a string representation of an * 1-Wire address. * * @throws OneWireIOException on a 1-Wire communication error such as * no device present. 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 Address */ public String getAdapterAddress () throws OneWireIOException, OneWireException { // get a reference to the current oneWire State OneWireState preserved_mstate = owState; owState = new OneWireState(); try { // acquire exclusive use of the port beginLocalExclusive(); // only check if the port is aquired if (uAdapterPresent()) { // set the search to find all of the available DS1982's this.setSearchAllDevices(); this.targetAllFamilies(); this.targetFamily(ADAPTER_ID_FAMILY); Enumeration adapter_id_enum = this.getAllDeviceContainers(); byte[] address = new byte [8]; // loop through each of the DS1982's to find an adapter ID for (; adapter_id_enum.hasMoreElements(); ) { OneWireContainer ibutton = ( OneWireContainer ) adapter_id_enum.nextElement(); System.arraycopy(ibutton.getAddress(), 0, address, 0, 8); // select this device if (select(address)) { // create a buffer to read the first page byte[] read_buffer = new byte [37]; int cnt = 0; int i; // extended read memory command read_buffer [cnt++] = ( byte ) EXTENDED_READ_PAGE; // address of first page read_buffer [cnt++] = 0; read_buffer [cnt++] = 0; // CRC, data of page and CRC from device for (i = 0; i < 34; i++) read_buffer [cnt++] = ( byte ) 0xFF; // perform CRC8 of the first chunk of known data int crc8 = CRC8.compute(read_buffer, 0, 3, 0); // send/receive data to 1-Wire dataBlock(read_buffer, 0, cnt); // check the first CRC if (CRC8.compute(read_buffer, 3, 1, crc8) == 0) { // compute the next CRC8 with data from device if (CRC8.compute(read_buffer, 4, 33, 0) == 0) { // now loop to see if all data is 0xFF for (i = 4; i < 36; i++) if (( byte ) read_buffer [i] != ( byte ) 0xFF) continue; // must be the one! if (i == 36) return ibutton.getAddressAsString(); } } } } } else throw new OneWireIOException( "USerialAdapter-getAdapterAddress, adapter not present"); } catch (OneWireException e) { // Drain. } finally { // restore the old state owState = preserved_mstate; // release local exclusive use of port endLocalExclusive(); } // don't know the ID return "<not available>"; } //-------- //-------- Adapter features //-------- /* The following interogative methods are provided so that client code * can react selectively to underlying states without generating an * exception. */ /** * Returns whether adapter can physically support overdrive mode. * * @return <code>true</code> if this port adapter can do OverDrive, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canOverdrive () throws OneWireIOException, OneWireException { return true; } /** * Returns whether the adapter can physically support hyperdrive mode. * * @return <code>true</code> if this port adapter can do HyperDrive, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canHyperdrive () throws OneWireIOException, OneWireException { return false; } /** * Returns whether the adapter can physically support flex speed mode. * * @return <code>true</code> if this port adapter can do flex speed, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canFlex () throws OneWireIOException, OneWireException { return true; } /** * Returns whether adapter can physically support 12 volt power mode. * * @return <code>true</code> if this port adapter can do Program voltage, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canProgram () throws OneWireIOException, OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); // only check if the port is aquired if (uAdapterPresent()) { // perform a reset to read the program available flag if (uState.revision == 0) reset(); // return the flag return uState.programVoltageAvailable; } else throw new OneWireIOException( "USerialAdapter-canProgram, adapter not present"); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Returns whether the adapter can physically support strong 5 volt power * mode. * * @return <code>true</code> if this port adapter can do strong 5 volt * mode, <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canDeliverPower () throws OneWireIOException, OneWireException { return true; } /** * Returns whether the adapter can physically support "smart" strong 5 * volt power mode. "smart" power delivery is the ability to deliver * power until it is no longer needed. The current drop it detected * and power delivery is stopped. * * @return <code>true</code> if this port adapter can do "smart" strong * 5 volt mode, <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canDeliverSmartPower () throws OneWireIOException, OneWireException { // regardless of adapter, the class does not support it return false; } /** * Returns whether adapter can physically support 0 volt 'break' mode. * * @return <code>true</code> if this port adapter can do break, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canBreak () throws OneWireIOException, OneWireException { return true; } //-------- //-------- Finding iButtons //-------- /** * Returns <code>true</code> if the first iButton or 1-Wire device * is found on the 1-Wire Network. * If no 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 findFirstDevice () throws OneWireIOException, OneWireException { // reset the current search owState.searchLastDiscrepancy = 0; owState.searchFamilyLastDiscrepancy = 0; owState.searchLastDevice = false; // search for the first device using next return findNextDevice(); } /** * Returns <code>true</code> if the next iButton or 1-Wire device * is found. The previous 1-Wire device found is used
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -