📄 dsportadapter.java
字号:
{ throw new OneWireException( "Break delivery not supported by this adapter type"); } /** * Sets the 1-Wire Network voltage to normal level. This method is used * to disable 1-Wire conditions created by startPowerDelivery and * startProgramPulse. This method will automatically be called if * a communication method is called while an outstanding power * command is taking place. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter * or the adapter does not support this operation */ public void setPowerNormal () throws OneWireIOException, OneWireException { return; } //-------- //-------- 1-Wire Network speed methods //-------- /** * Sets the new speed of data * transfer on the 1-Wire Network. <p> * * @param speed * <ul> * <li> 0 (SPEED_REGULAR) set to normal communciation speed * <li> 1 (SPEED_FLEX) set to flexible communciation speed used * for long lines * <li> 2 (SPEED_OVERDRIVE) set to normal communciation speed to * overdrive * <li> 3 (SPEED_HYPERDRIVE) set to normal communciation speed to * hyperdrive * <li> >3 future speeds * </ul> * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter * or the adapter does not support this operation */ public void setSpeed (int speed) throws OneWireIOException, OneWireException { if (speed != SPEED_REGULAR) throw new OneWireException( "Non-regular 1-Wire speed not supported by this adapter type"); } /** * Returns the current data transfer speed on the 1-Wire Network. <p> * * @return <code>int</code> representing the current 1-Wire speed * <ul> * <li> 0 (SPEED_REGULAR) set to normal communication speed * <li> 1 (SPEED_FLEX) set to flexible communication speed used * for long lines * <li> 2 (SPEED_OVERDRIVE) set to normal communication speed to * overdrive * <li> 3 (SPEED_HYPERDRIVE) set to normal communication speed to * hyperdrive * <li> >3 future speeds * </ul> */ public int getSpeed () { return SPEED_REGULAR; } //-------- //-------- Misc //-------- /** * Constructs a <code>OneWireContainer</code> object with a user supplied 1-Wire network address. * * @param address device address with which to create a new container * * @return The <code>OneWireContainer</code> object * @see Address */ public OneWireContainer getDeviceContainer (byte[] address) { int family_code = address [0] & 0x7F; String family_string = ((family_code) < 16) ? ("0" + Integer.toHexString(family_code)).toUpperCase() : (Integer.toHexString(family_code)).toUpperCase(); Class ibutton_class = null; OneWireContainer new_ibutton; // If any user registered button exist, check the hashtable. if (!registeredOneWireContainerClasses.isEmpty()) { Integer familyInt = new Integer(family_code); // Try and get a user provided container class first. ibutton_class = ( Class ) registeredOneWireContainerClasses.get(familyInt); } // If we don't get one, do the normal lookup method. if (ibutton_class == null) { // try to load the ibutton container class try { ibutton_class = Class.forName("com.dalsemi.onewire.container.OneWireContainer" + family_string); } catch (Exception e) { ibutton_class = null; } // if did not get specific container try the general one if (ibutton_class == null) { // try to load the ibutton container class try { ibutton_class = Class.forName( "com.dalsemi.onewire.container.OneWireContainer"); } catch (Exception e) { System.out.println("EXCEPTION: Unable to load OneWireContainer" + e); return null; } } } // try to load the ibutton container class try { // create the iButton container with a reference to this adapter new_ibutton = ( OneWireContainer ) ibutton_class.newInstance(); new_ibutton.setupContainer(this, address); } catch (Exception e) { System.out.println( "EXCEPTION: Unable to instantiate OneWireContainer " + ibutton_class + ": " + e); e.printStackTrace(); return null; } // return this new container return new_ibutton; } /** * Constructs a <code>OneWireContainer</code> object with a user supplied 1-Wire network address. * * @param address device address with which to create a new container * * @return The <code>OneWireContainer</code> object * @see Address */ public OneWireContainer getDeviceContainer (long address) { return getDeviceContainer(Address.toByteArray(address)); } /** * Constructs a <code>OneWireContainer</code> object with a user supplied 1-Wire network address. * * @param address device address with which to create a new container * * @return The <code>OneWireContainer</code> object * @see Address */ public OneWireContainer getDeviceContainer (String address) { return getDeviceContainer(Address.toByteArray(address)); } /** * Constructs a <code>OneWireContainer</code> object using the current 1-Wire network address. * The internal state of the port adapter keeps track of the last * address found and is able to create container objects from this * state. * * @return the <code>OneWireContainer</code> object */ public OneWireContainer getDeviceContainer () { // Mask off the upper bit. byte[] address = new byte [8]; getAddress(address); return getDeviceContainer(address); } /** * Checks to see if the family found is in the desired * include group. * * @return <code>true</code> if in include group */ protected boolean isValidFamily (byte[] address) { byte familyCode = address [0]; if (exclude != null) { for (int i = 0; i < exclude.length; i++) { if (familyCode == exclude [i]) { return false; } } } if (include != null) { for (int i = 0; i < include.length; i++) { if (familyCode == include [i]) { return true; } } return false; } return true; } /** * Performs a 'strongAccess' with the provided 1-Wire address. * 1-Wire Network has already been reset and the 'search' * command sent before this is called. * * @param address device address to do strongAccess on * * @return true if device participated and was present * in the strongAccess search */ private boolean strongAccess (byte[] address) throws OneWireIOException, OneWireException { byte[] send_packet = new byte [24]; int i; // set all bits at first for (i = 0; i < 24; i++) send_packet [i] = ( byte ) 0xFF; // now set or clear apropriate bits for search for (i = 0; i < 64; i++) arrayWriteBit(arrayReadBit(i, address), (i + 1) * 3 - 1, send_packet); // send to 1-Wire Net dataBlock(send_packet, 0, 24); // check the results of last 8 triplets (should be no conflicts) int cnt = 56, goodbits = 0, tst, s; for (i = 168; i < 192; i += 3) { tst = (arrayReadBit(i, send_packet) << 1) | arrayReadBit(i + 1, send_packet); s = arrayReadBit(cnt++, address); if (tst == 0x03) // no device on line { goodbits = 0; // number of good bits set to zero break; // quit } if (((s == 0x01) && (tst == 0x02)) || ((s == 0x00) && (tst == 0x01))) // correct bit goodbits++; // count as a good bit } // check too see if there were enough good bits to be successful return (goodbits >= 8); } /** * Writes the bit state in a byte array. * * @param state new state of the bit 1, 0 * @param index bit index into byte array * @param buf byte array to manipulate */ private void arrayWriteBit (int state, int index, byte[] buf) { int nbyt = (index >>> 3); int nbit = index - (nbyt << 3); if (state == 1) buf [nbyt] |= (0x01 << nbit); else buf [nbyt] &= ~(0x01 << nbit); } /** * Reads a bit state in a byte array. * * @param index bit index into byte array * @param buf byte array to read from * * @return bit state 1 or 0 */ private int arrayReadBit (int index, byte[] buf) { int nbyt = (index >>> 3); int nbit = index - (nbyt << 3); return ((buf [nbyt] >>> nbit) & 0x01); } //-------- //-------- java.lang.Object methods //-------- /** * Returns a hashcode for this object * @return a hascode for this object */ /*public int hashCode() { return this.toString().hashCode(); }*/ /** * Returns true if the given object is the same or equivalent * to this DSPortAdapter. * * @param o the Object to compare this DSPortAdapter to * @return true if the given object is the same or equivalent * to this DSPortAdapter. */ public boolean equals(Object o) { if(o!=null && o instanceof DSPortAdapter) { if(o==this || o.toString().equals(this.toString())); { return true; } } return false; } /** * Returns a string representation of this DSPortAdapter, in the format * of "<adapter name> <port name>". * * @return a string representation of this DSPortAdapter */ public String toString() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -