📄 userialadapter.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. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.adapter;// importsimport java.util.*;import java.io.*;import javax.comm.*;//import javax.comm.*;import java.lang.Integer;import com.dalsemi.onewire.OneWireAccessProvider;import com.dalsemi.onewire.container.OneWireContainer;import com.dalsemi.onewire.adapter.OneWireIOException;import com.dalsemi.onewire.OneWireException;import com.dalsemi.onewire.utils.CRC8;import com.dalsemi.onewire.utils.Bit;/** * The USerialAdapter class implememts the DSPortAdapter interface * for a DS2480 based serial adapter such as the DS9097U-009 or * DS9097U-S09. <p> * * Instances of valid USerialAdapter's are retrieved from methods in * {@link com.dalsemi.onewire.OneWireAccessProvider OneWireAccessProvider}. * * <P>The DSPortAdapter methods can be organized into the following categories: </P> * <UL> * <LI> <B> Information </B> * <UL> * <LI> {@link #getAdapterName() getAdapterName} * <LI> {@link #getPortTypeDescription() getPortTypeDescription} * <LI> {@link #getClassVersion() getClassVersion} * <LI> {@link #adapterDetected() adapterDetected} * <LI> {@link #getAdapterVersion() getAdapterVersion} * <LI> {@link #getAdapterAddress() getAdapterAddress} * </UL> * <LI> <B> Port Selection </B> * <UL> * <LI> {@link #getPortNames() getPortNames} * <LI> {@link #selectPort(String) selectPort} * <LI> {@link #getPortName() getPortName} * <LI> {@link #freePort() freePort} * </UL> * <LI> <B> Adapter Capabilities </B> * <UL> * <LI> {@link #canOverdrive() canOverdrive} * <LI> {@link #canHyperdrive() canHyperdrive} * <LI> {@link #canFlex() canFlex} * <LI> {@link #canProgram() canProgram} * <LI> {@link #canDeliverPower() canDeliverPower} * <LI> {@link #canDeliverSmartPower() canDeliverSmartPower} * <LI> {@link #canBreak() canBreak} * </UL> * <LI> <B> 1-Wire Network Semaphore </B> * <UL> * <LI> {@link #beginExclusive(boolean) beginExclusive} * <LI> {@link #endExclusive() endExclusive} * </UL> * <LI> <B> 1-Wire Device Discovery </B> * <UL> * <LI> Selective Search Options * <UL> * <LI> {@link #targetAllFamilies() targetAllFamilies} * <LI> {@link #targetFamily(int) targetFamily(int)} * <LI> {@link #targetFamily(byte[]) targetFamily(byte[])} * <LI> {@link #excludeFamily(int) excludeFamily(int)} * <LI> {@link #excludeFamily(byte[]) excludeFamily(byte[])} * <LI> {@link #setSearchOnlyAlarmingDevices() setSearchOnlyAlarmingDevices} * <LI> {@link #setNoResetSearch() setNoResetSearch} * <LI> {@link #setSearchAllDevices() setSearchAllDevices} * </UL> * <LI> Search With Automatic 1-Wire Container creation * <UL> * <LI> {@link #getAllDeviceContainers() getAllDeviceContainers} * <LI> {@link #getFirstDeviceContainer() getFirstDeviceContainer} * <LI> {@link #getNextDeviceContainer() getNextDeviceContainer} * </UL> * <LI> Search With NO 1-Wire Container creation * <UL> * <LI> {@link #findFirstDevice() findFirstDevice} * <LI> {@link #findNextDevice() findNextDevice} * <LI> {@link #getAddress(byte[]) getAddress(byte[])} * <LI> {@link #getAddressAsLong() getAddressAsLong} * <LI> {@link #getAddressAsString() getAddressAsString} * </UL> * <LI> Manual 1-Wire Container creation * <UL> * <LI> {@link #getDeviceContainer(byte[]) getDeviceContainer(byte[])} * <LI> {@link #getDeviceContainer(long) getDeviceContainer(long)} * <LI> {@link #getDeviceContainer(String) getDeviceContainer(String)} * <LI> {@link #getDeviceContainer() getDeviceContainer()} * </UL> * </UL> * <LI> <B> 1-Wire Network low level access (usually not called directly) </B> * <UL> * <LI> Device Selection and Presence Detect * <UL> * <LI> {@link #isPresent(byte[]) isPresent(byte[])} * <LI> {@link #isPresent(long) isPresent(long)} * <LI> {@link #isPresent(String) isPresent(String)} * <LI> {@link #isAlarming(byte[]) isAlarming(byte[])} * <LI> {@link #isAlarming(long) isAlarming(long)} * <LI> {@link #isAlarming(String) isAlarming(String)} * <LI> {@link #select(byte[]) select(byte[])} * <LI> {@link #select(long) select(long)} * <LI> {@link #select(String) select(String)} * </UL> * <LI> Raw 1-Wire IO * <UL> * <LI> {@link #reset() reset} * <LI> {@link #putBit(boolean) putBit} * <LI> {@link #getBit() getBit} * <LI> {@link #putByte(int) putByte} * <LI> {@link #getByte() getByte} * <LI> {@link #getBlock(int) getBlock(int)} * <LI> {@link #getBlock(byte[], int) getBlock(byte[], int)} * <LI> {@link #getBlock(byte[], int, int) getBlock(byte[], int, int)} * <LI> {@link #dataBlock(byte[], int, int) dataBlock(byte[], int, int)} * </UL> * <LI> 1-Wire Speed and Power Selection * <UL> * <LI> {@link #setPowerDuration(int) setPowerDuration} * <LI> {@link #startPowerDelivery(int) startPowerDelivery} * <LI> {@link #setProgramPulseDuration(int) setProgramPulseDuration} * <LI> {@link #startProgramPulse(int) startProgramPulse} * <LI> {@link #startBreak() startBreak} * <LI> {@link #setPowerNormal() setPowerNormal} * <LI> {@link #setSpeed(int) setSpeed} * <LI> {@link #getSpeed() getSpeed} * </UL> * </UL> * <LI> <B> Advanced </B> * <UL> * <LI> {@link #registerOneWireContainerClass(int, Class) registerOneWireContainerClass} * </UL> * </UL> * * @see com.dalsemi.onewire.OneWireAccessProvider * @see com.dalsemi.onewire.container.OneWireContainer * * @version 0.10, 24 Aug 2001 * @author DS * */public class USerialAdapter extends DSPortAdapter{ //-------- //-------- Finals //-------- /** Family code for the EPROM iButton DS1982 */ private static final int ADAPTER_ID_FAMILY = 0x09; /** Extended read page command for DS1982 */ private static final int EXTENDED_READ_PAGE = 0xC3; /** Normal Search, all devices participate */ private static final char NORMAL_SEARCH_CMD = 0xF0; /** Conditional Search, only 'alarming' devices participate */ private static final char ALARM_SEARCH_CMD = 0xEC; //-------- //-------- Static Variables //-------- /** Version string for this adapter class */ private static String classVersion = "0.10"; /** Hashtable to contain SerialService instances */ //private static Hashtable serailServiceHash = new Hashtable(4); /** Max baud rate supported by DS9097U */ private static int maxBaud; //-------- //-------- Variables //-------- /** Reference to the current SerialService */ private SerialService serial; /** String name of the current opened port */ private boolean adapterPresent; /** Flag to indicate more than expected byte received in a transaction */ private boolean extraBytesReceived; /** U Adapter packet builder */ UPacketBuilder uBuild; /** State of the OneWire */ private OneWireState owState; /** U Adapter state */ private UAdapterState uState; /** Input buffer to hold received data */ private StringBuffer inBuffer; /** Flag to indicate have a local begin/end Exclusive use of serial */ private boolean haveLocalUse; private Object syncObject; /** Enable/disable debug messages */ private static boolean doDebugMessages = false; //-------- //-------- Constructor //-------- /** * Constructs a DS9097U serial adapter class * */ public USerialAdapter () { serial = null; owState = new OneWireState(); uState = new UAdapterState(owState); uBuild = new UPacketBuilder(uState); inBuffer = new StringBuffer(); adapterPresent = false; haveLocalUse = false; syncObject = new Object(); } //-------- //-------- Information Methods //-------- protected void finalize() { try { freePort(); } catch(Exception e) {;} } /** * Cleans up the resources used by the thread argument. If another * thread starts communicating with this port, and then goes away, * there is no way to relinquish the port without stopping the * process. This method allows other threads to clean up. * * @param thread that may have used a <code>USerialAdapter</code> */ public static void CleanUpByThread(Thread t) { if(doDebugMessages) System.out.println("CleanUpByThread called: Thread=" + t); SerialService.CleanUpByThread(t); } /** * Retrieve the name of the port adapter as a string. The 'Adapter' * is a device that connects to a 'port' that allows one to * communicate with an iButton or other 1-Wire device. As example * of this is 'DS9097U'. * * @return <code>String</code> representation of the port adapter. */ public String getAdapterName () { return "DS9097U"; } /** * Retrieve a description of the port required by this port adapter. * An example of a 'Port' would 'serial communication port'. * * @return <code>String</code> description of the port type required. */ public String getPortTypeDescription () { return "serial communication port"; } /** * Retrieve a version string for this class. * * @return version string */ public String getClassVersion () { return classVersion; } //-------- //-------- Port Selection //-------- /** * Retrieve a list of the platform appropriate port names for this * adapter. A port must be selected with the method 'selectPort' * before any other communication methods can be used. Using * a communcation method before 'selectPort' will result in * a <code>OneWireException</code> exception. * * @return enumeration of type <code>String</code> that contains the port * names */ public Enumeration getPortNames () { return serial.getSerialPortIdentifiers(); } /** * Specify a platform appropriate port name for this adapter. Note that * even though the port has been selected, it's ownership may be relinquished * if it is not currently held in a 'exclusive' block. This class will then * try to re-aquire the port when needed. If the port cannot be re-aquired * ehen the exception <code>PortInUseException</code> will be thrown. * * @param newPortName name of the target port, retrieved from * getPortNames() * * @return <code>true</code> if the port was aquired, <code>false</code> * if the port is not available. * * @throws OneWireIOException If port does not exist, or unable to communicate with port. * @throws OneWireException If port does not exist */ public boolean selectPort (String newPortName) throws OneWireIOException, OneWireException { // find the port reference serial = serial.getSerialService(newPortName); //( SerialService ) serailServiceHash.get(newPortName); // check if there is no such port if (serial == null) throw new OneWireException( "USerialAdapter: selectPort(), Not such serial port: " + newPortName); try { // acquire exclusive use of the port beginLocalExclusive(); // attempt to open the port serial.openPort(); return true; } catch(IOException ioe) { throw new OneWireIOException(ioe.toString()); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Retrieve the name of the selected port as a <code>String</code>. * * @return <code>String</code> of selected port * * @throws OneWireException if valid port not yet selected */ public String getPortName () throws OneWireException { if (serial != null) return serial.getPortName(); else throw new OneWireException( "USerialAdapter-getPortName, port not selected"); } /** * Free ownership of the selected port if it is currently owned back * to the system. This should only be called if the recently * selected port does not have an adapter or at the end of * your application's use of the port. * * @throws OneWireException If port does not exist
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -