📄 lserialadapter.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 2001 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 LSerialAdapter class implememts the DSPortAdapter interface * for a legacy 1-Wire serial interface adapters such as the DS9097.<p> * * Instances of valid LSerialdapter'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> * * 8/11/2003 - shughes - modified to support RXTX instead of javax.comm * * @see com.dalsemi.onewire.OneWireAccessProvider * @see com.dalsemi.onewire.container.OneWireContainer * * @version 0.00, 4 Dec 2001 * @author DS * * */public class LSerialAdapter extends DSPortAdapter{ //-------- //-------- Finals //-------- /** Normal Search, all devices participate */ private static final int NORMAL_SEARCH_CMD = 0xF0; /** Conditional Search, only 'alarming' devices participate */ private static final int ALARM_SEARCH_CMD = 0xEC; //-------- //-------- Static Variables //-------- /** Version string for this adapter class */ private static String classVersion = "0.00"; /** Hashtable to contain SerialService instances */ //private static Hashtable serialServiceHash = new Hashtable(4); //-------- //-------- Variables //-------- /** Reference to the current SerialService */ private SerialService serial; /** String name of the current opened port */ private boolean adapterPresent; /** flag to indicate the last discrepancy */ private int LastDiscrepancy; /** flag to indicate the last descrepancy with the family code byte */ private int LastFamilyDiscrepancy; /** true if device found is the last device */ private boolean LastDevice; /** current device */ private byte[] CurrentDevice = new byte[8]; /** * Whether we are searching only alarming iButtons. This is currently * ignored. */ boolean searchOnlyAlarmingButtons; /** Flag to indicate next search will not be preceeded by a 1-Wire reset */ private boolean skipResetOnSearch = false; /** Flag to indicate next search will be a 'first' */ private boolean resetSearch = true; /** Flag to indicate have a local begin/end Exclusive use of serial */ private boolean haveLocalUse; /** Field syncObject */ private Object syncObject; /** Enable/disable debug messages */ private static boolean doDebugMessages = false; //-------- //-------- Constructor //-------- /** * Constructs a legacy serial adapter class * */ public LSerialAdapter() { serial = null; adapterPresent = false; haveLocalUse = false; syncObject = new Object(); } //-------- //-------- Information Methods //-------- /** * 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 'DS9097E'. * * @return <code>String</code> representation of the port adapter. */ public String getAdapterName() { return "DS9097"; } /** * 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); // check if there is no such port if (serial == null) throw new OneWireException( "DS9097EAdapter: selectPort(), Not such serial port: " + newPortName); try { // acquire exclusive use of the port beginLocalExclusive(); // attempt to open the port serial.openPort(); serial.setBaudRate(115200); 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( "DS9097EAdapter-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 */ public void freePort() throws OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); // attempt to open 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 { if (!adapterPresent) { try { // acquire exclusive use of the port beginLocalExclusive(); adapterPresent(); } catch (OneWireIOException e) { System.err.println("DS9097EAdapter: Not detected " + e); } finally { // release local exclusive use of port endLocalExclusive(); } } return adapterPresent; } /** * Retrieve the version of the adapter.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -