📄 onewireaccessprovider.java
字号:
} /** * Finds, opens, and verifies the specified adapter on the * indicated port. * * @param adapterName string name of the adapter (match to result * of call to getAdapterName() method in DSPortAdapter) * @param portName string name of the port used in the method * selectPort() in DSPortAdapter * * @return <code>DSPortAdapter</code> if adapter present * * @throws OneWireIOException when communcation with the adapter fails * @throws OneWireException when the port or adapter not present */ public static DSPortAdapter getAdapter (String adapterName, String portName) throws OneWireIOException, OneWireException { DSPortAdapter adapter, found_adapter = null; // check for override if (useOverrideAdapter) return overrideAdapter; // enumerature through available adapters to find the correct one for (Enumeration adapter_enum = enumerateAllAdapters(); adapter_enum.hasMoreElements(); ) { // cast the enum as a DSPortAdapter adapter = ( DSPortAdapter ) adapter_enum.nextElement(); // see if this is the type of adapter we want if ((found_adapter != null) || (!adapter.getAdapterName().equals(adapterName))) { // not this adapter, then just cleanup try { adapter.freePort(); } catch (Exception e) { // DRAIN } continue; } // attempt to open and verify the adapter if (adapter.selectPort(portName)) { adapter.beginExclusive(true); try { // check for the adapter if (adapter.adapterDetected()) found_adapter = adapter; else { // close the port just opened adapter.freePort(); throw new OneWireException("Port found \"" + portName + "\" but Adapter \"" + adapterName + "\" not detected"); } } finally { adapter.endExclusive(); } } else throw new OneWireException( "Specified port \"" + portName + "\" could not be selected for adapter \"" + adapterName + "\""); } // if adapter found then return it if (found_adapter != null) return found_adapter; // adapter by that name not found throw new OneWireException("Specified adapter name \"" + adapterName + "\" is not known"); } /** * Finds, opens, and verifies the default adapter and * port. Looks for the default adapter/port in the following locations: * <p> * <ul> * <li> Use adapter/port in System.properties for onewire.adapter.default, * and onewire.port.default properties tags.</li> * <li> Use adapter/port from onewire.properties file in current directory * or < java.home >/lib/ (Desktop) or /etc/ (TINI)</li> * <li> Use smart default * <ul> * <li> Desktop * <ul> * <li> First, TMEX default (Win32 only) * <li> Second, if TMEX not present, then DS9097U/(first serial port) * </ul> * <li> TINI, TINIExternalAdapter on port serial1 * </ul> * </ul> * * @return <code>DSPortAdapter</code> if default adapter present * * @throws OneWireIOException when communcation with the adapter fails * @throws OneWireException when the port or adapter not present */ public static DSPortAdapter getDefaultAdapter () throws OneWireIOException, OneWireException { if (useOverrideAdapter) { return overrideAdapter; } return getAdapter(getProperty("onewire.adapter.default"), getProperty("onewire.port.default")); } /** * Gets the specfied onewire property. * Looks for the property in the following locations: * <p> * <ul> * <li> In System.properties * <li> In onewire.properties file in current directory * or < java.home >/lib/ (Desktop) or /etc/ (TINI) * <li> 'smart' default if property is 'onewire.adapter.default' * or 'onewire.port.default' * </ul> * * @param propName string name of the property to read * * @return <code>String</code> representing the property value or <code>null</code> if * it could not be found (<code>onewire.adapter.default</code> and * <code>onewire.port.default</code> may * return a 'smart' default even if property not present) */ public static String getProperty (String propName) { try { if (useOverrideAdapter) { if (propName.equals("onewire.adapter.default")) return overrideAdapter.getAdapterName(); if (propName.equals("onewire.port.default")) return overrideAdapter.getPortName(); } } catch(Exception e) { //just drain it and let the normal method run... } Properties onewire_properties = new Properties(); FileInputStream prop_file = null; String ret_str = null; DSPortAdapter adapter_instance; Class adapter_class; // try system properties try { ret_str = System.getProperty(propName, null); } catch (Exception e) { ret_str = null; } // if defaults not found then try onewire.properties file if (ret_str == null) { // loop to attempt to open the onewire.properties file in two locations // .\onewire.properties or <java.home>\lib\onewire.properties String path = new String(""); for (int i = 0; i <= 1; i++) { // attempt to open the onewire.properties file try { prop_file = new FileInputStream(path + "onewire.properties"); } catch (Exception e) { prop_file = null; } // if open, then try to read value if (prop_file != null) { // attempt to read the onewire.properties try { onewire_properties.load(prop_file); ret_str = onewire_properties.getProperty(propName, null); } catch (Exception e) { ret_str = null; } } // check to see if we now have the value if (ret_str != null) break; // try the second path path = System.getProperty("java.home") + File.separator + "lib" + File.separator; } } // if defaults still not found then check TMEX default if (ret_str == null) { try { if (propName.equals("onewire.adapter.default")) ret_str = TMEXAdapter.getDefaultAdapterName(); else if (propName.equals("onewire.port.default")) ret_str = TMEXAdapter.getDefaultPortName(); // if did not get real string then null out if (ret_str != null) { if (ret_str.length() <= 0) ret_str = null; } } catch (Exception e) { // DRAIN } catch (Error e) { // DRAIN } } // if STILL not found then just pick DS9097U on 'smartDefaultPort' if (ret_str == null) { if (propName.equals("onewire.adapter.default")) ret_str = "DS9097U"; else if (propName.equals("onewire.port.default")) { try { adapter_class = Class.forName("com.dalsemi.onewire.adapter.USerialAdapter"); adapter_instance = ( DSPortAdapter ) adapter_class.newInstance(); // check if has any ports (common javax.comm problem) if (adapter_instance.getPortNames().hasMoreElements()) ret_str = ( String ) adapter_instance.getPortNames().nextElement(); } catch (Exception e) { // DRAIN } catch (Error e) { // DRAIN } } } return ret_str; } /** * Sets an overriding adapter. This adapter will be returned from * getAdapter and getDefaultAdapter despite what was requested. * * @param adapter adapter to be the override * * @see #getAdapter * @see #getDefaultAdapter * @see #clearUseOverridingAdapter */ public static void setUseOverridingAdapter(DSPortAdapter adapter) { useOverrideAdapter = true; overrideAdapter = adapter; } /** * Clears the overriding adapter. The operation of * getAdapter and getDefaultAdapter will be returned to normal. * * @see #getAdapter * @see #getDefaultAdapter * @see #setUseOverridingAdapter */ public static void clearUseOverridingAdapter() { useOverrideAdapter = false; overrideAdapter = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -