📄 onewireaccessprovider.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */// OneWireAccessProvider.javapackage com.dalsemi.onewire;// importsimport java.util.Vector;import java.util.Enumeration;import com.dalsemi.onewire.adapter.*;import java.io.*;import java.util.Properties;/** * The OneWireAccessProvider class manages the Dallas Semiconductor * adapter class derivatives of <code>DSPortAdapter</code>. An enumeration of all * available adapters can be accessed through the * member function <code>EnumerateAllAdapters</code>. This enables an * application to be adapter independent. There are also facilities to get a system * appropriate default adapter/port combination.<p> * * <H3> Usage </H3> * * <DL> * <DD> <H4> Example 1</H4> * Get an instance of the default 1-Wire adapter. The adapter will be ready * to use if no exceptions are thrown. * <PRE> <CODE> * try * { * DSPortAdapter adapter = OneWireAccessProvider.getDefaultAdapter(); * * System.out.println("Adapter: " + adapter.getAdapterName() + " Port: " + adapter.getPortName()); * * // use the adapter ... * * } * catch(Exception e) * { * System.out.println("Default adapter not present: " + e); * } * </CODE> </PRE> * </DL> * * <DL> * <DD> <H4> Example 2</H4> * Enumerate through the available adapters and ports. * <PRE> <CODE> * DSPortAdapter adapter; * String port; * * // get the adapters * for (Enumeration adapter_enum = OneWireAccessProvider.enumerateAllAdapters(); * adapter_enum.hasMoreElements(); ) * { * // cast the enum as a DSPortAdapter * adapter = ( DSPortAdapter ) adapter_enum.nextElement(); * * System.out.print("Adapter: " + adapter.getAdapterName() + " with ports: "); * * // get the ports * for (Enumeration port_enum = adapter.getPortNames(); * port_enum.hasMoreElements(); ) * { * // cast the enum as a String * port = ( String ) port_enum.nextElement(); * * System.out.print(port + " "); * } * * System.out.println(); * } * </CODE> </PRE> * </DL> * * <DL> * <DD> <H4> Example 3</H4> * Display the default adapter name and port without getting an instance of the adapter. * <PRE> <CODE> * System.out.println("Default Adapter: " + * OneWireAccessProvider.getProperty("onewire.adapter.default")); * System.out.println("Default Port: " + * OneWireAccessProvider.getProperty("onewire.port.default")); * </CODE> </PRE> * </DL> * * @see com.dalsemi.onewire.adapter.DSPortAdapter * * @version 0.00, 30 August 2000 * @author DS */public class OneWireAccessProvider{ /** * Smart default port */ private static String smartDefaultPort = "COM1"; /** * Override adapter variables */ private static boolean useOverrideAdapter = false; private static DSPortAdapter overrideAdapter = null; /** * Don't allow anyone to instantiate. */ private OneWireAccessProvider () { } /** * Gets an <code>Enumeration</code> of all 1-Wire * adapter types supported. Using this enumeration with the port enumeration for * each adapter, a search can be done to find all available hardware adapters. * * @return <code>Enumeration</code> of <code>DSPortAdapters</code> in the system */ public static Enumeration enumerateAllAdapters () { Vector adapter_vector = new Vector(3, 1); DSPortAdapter adapter_instance; Class adapter_class; String class_name = null; boolean TMEX_loaded = false; boolean serial_loaded = false; // check for override if (useOverrideAdapter) { adapter_vector.addElement(overrideAdapter); return (adapter_vector.elements()); } // only try native TMEX if on x86 Windows platform if ((System.getProperty("os.arch").indexOf("86") != -1) && (System.getProperty("os.name").indexOf("Windows") != -1)) { // loop through the TMEX adapters for (int port_type = 0; port_type <= 15; port_type++) { // try to load the adapter classes try { adapter_instance = ( DSPortAdapter ) (new com.dalsemi.onewire.adapter.TMEXAdapter( port_type)); // only add it if it has some ports if (adapter_instance.getPortNames().hasMoreElements()) { adapter_vector.addElement(adapter_instance); TMEX_loaded = true; } } catch (Exception e) { // DRAIN } catch (Error e) { // DRAIN } } } // get the pure java adapter 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()) { if(!TMEX_loaded) { System.err.println( "Warning: serial communications API not setup properly, no ports in enumeration "); System.err.println( "Pure-Java DS9097U adapter will not work, not added to adapter enum"); } } else { adapter_vector.addElement(adapter_instance); serial_loaded = true; } } catch (java.lang.UnsatisfiedLinkError e) { if(!TMEX_loaded) { System.err.println( "WARNING: Could not load serial comm API for pure-Java DS9097U adapter."); System.err.println( "This message can be safely ignored if you are using TMEX Drivers or"); System.err.println( "the NetAdapter to connect to the 1-Wire Network."); System.err.println(); } } catch (java.lang.NoClassDefFoundError e) { if(!TMEX_loaded) { System.err.println(); System.err.println( "WARNING: Could not load serial comm API for pure-Java DS9097U adapter: " + e); System.err.println( "This message can be safely ignored if you are using TMEX Drivers or"); System.err.println( "the NetAdapter to connect to the 1-Wire Network."); System.err.println(); } } catch (Exception e) { // DRAIN } if(!TMEX_loaded && !serial_loaded) { System.err.println(); System.err.println("Standard drivers for 1-Wire are not found."); System.err.println("Please download the latest drivers from http://www.ibutton.com "); System.err.println("Or install RXTX Serial Communications API from http://www.rxtx.org "); System.err.println(); } // get the network adapter try { adapter_class = Class.forName("com.dalsemi.onewire.adapter.NetAdapter"); adapter_instance = ( DSPortAdapter ) adapter_class.newInstance(); adapter_vector.addElement(adapter_instance); } catch (java.lang.NoClassDefFoundError e) { System.err.println( "Warning: Could not load NetAdapter: " + e); } catch (Exception e) { // DRAIN } // get adapters from property file with keys 'onewire.register.adapter0-15' try { // loop through the possible registered adapters for (int reg_num = 0; reg_num <= 15; reg_num++) { class_name = getProperty("onewire.register.adapter" + reg_num); // done if no property by that name if (class_name == null) break; // add it to the enum adapter_class = Class.forName(class_name); adapter_instance = ( DSPortAdapter ) adapter_class.newInstance(); adapter_vector.addElement(adapter_instance); } } catch (java.lang.UnsatisfiedLinkError e) { System.err.println( "Warning: Adapter \"" + class_name + "\" was registered in " + "properties file, but the class could not be loaded"); } catch (java.lang.ClassNotFoundException e) { System.err.println( "Adapter \"" + class_name + "\" was registered in properties file, " + " but the class was not found"); } catch (Exception e) { // DRAIN } // check for no adapters if (adapter_vector.isEmpty()) System.err.println("No 1-Wire adapter classes found"); return (adapter_vector.elements());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -