netadapter.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 2,064 行 · 第 1/5 页
JAVA
2,064 行
* Takes an integer family code to avoid when searching for iButtons. * or 1-Wire devices. * If this method is used, then no devices of this family will be * found by any of the search methods. * * @param family the code of the family type NOT to target in searches * @see Address * @see #targetAllFamilies */ public void excludeFamily (int family) { try { synchronized(conn) { // send excludeFamily command conn.output.writeByte(CMD_EXCLUDEFAMILY); conn.output.writeInt(1); conn.output.writeByte((byte)family); conn.output.flush(); // check return value for success checkReturnValue(conn); } } catch(Exception e) { /* drain */ } } /** * Takes an array of bytes containing family codes to avoid when finding * iButtons or 1-Wire devices. If used, then no devices with family * codes in this array will be found by any of the search methods. * * @param family array of family cods NOT to target for searches * @see Address * @see #targetAllFamilies */ public void excludeFamily (byte family []) { try { synchronized(conn) { // send excludeFamily command conn.output.writeByte(CMD_EXCLUDEFAMILY); conn.output.writeInt(family.length); conn.output.write(family, 0, family.length); conn.output.flush(); // check return value for success checkReturnValue(conn); } } catch(Exception e) { /* drain */ } } //-------- //-------- 1-Wire Network Semaphore methods //-------- /** * Gets exclusive use of the 1-Wire to communicate with an iButton or * 1-Wire Device. * This method should be used for critical sections of code where a * sequence of commands must not be interrupted by communication of * threads with other iButtons, and it is permissible to sustain * a delay in the special case that another thread has already been * granted exclusive access and this access has not yet been * relinquished. <p> * * It can be called through the OneWireContainer * class by the end application if they want to ensure exclusive * use. If it is not called around several methods then it * will be called inside each method. * * @param blocking <code>true</code> if want to block waiting * for an excluse access to the adapter * @return <code>true</code> if blocking was false and a * exclusive session with the adapter was aquired * * @throws OneWireException on a setup error with the 1-Wire adapter */ public boolean beginExclusive (boolean blocking) throws OneWireException { boolean bGotLocalBlock = false, bGotServerBlock = false; if (blocking) { while (!beginExclusive()) { try{Thread.sleep(50);}catch(Exception e){} } bGotLocalBlock = true; } else bGotLocalBlock = beginExclusive(); try { synchronized(conn) { // send beginExclusive command conn.output.writeByte(CMD_BEGINEXCLUSIVE); conn.output.writeBoolean(blocking); conn.output.flush(); // check return value for success checkReturnValue(conn); // next parameter should be the return from beginExclusive bGotServerBlock = conn.input.readBoolean(); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } // if blocking, I shouldn't get here unless both are true return bGotLocalBlock && bGotServerBlock; } /** * Gets exclusive use of the 1-Wire to communicate with an iButton or * 1-Wire Device. * This method should be used for critical sections of code where a * sequence of commands must not be interrupted by communication of * threads with other iButtons, and it is permissible to sustain * a delay in the special case that another thread has already been * granted exclusive access and this access has not yet been * relinquished. This is private and non blocking<p> * * @return <code>true</code> a exclusive session with the adapter was * aquired * * @throws OneWireException */ private boolean beginExclusive () throws OneWireException { synchronized(currentThreadHash) { if (currentThreadHash == NOT_OWNED) { // not owned so take currentThreadHash = new Integer(Thread.currentThread().hashCode()); // provided debug on standard out if (DEBUG) { System.out.println("beginExclusive, now owned by: " + Thread.currentThread().getName()); } return true; } else if (currentThreadHash.intValue() == Thread.currentThread().hashCode()) { // provided debug on standard out if (DEBUG) { System.out.println("beginExclusive, already owned by: " + Thread.currentThread().getName()); } // already own return true; } else { // want port but don't own return false; } } } /** * Relinquishes exclusive control of the 1-Wire Network. * This command dynamically marks the end of a critical section and * should be used when exclusive control is no longer needed. */ public void endExclusive () { synchronized(currentThreadHash) { // if own then release if (currentThreadHash!=NOT_OWNED && currentThreadHash.intValue() == Thread.currentThread().hashCode()) { if (DEBUG) { System.out.println("endExclusive, was owned by: " + Thread.currentThread().getName()); } currentThreadHash = NOT_OWNED; try { synchronized(conn) { // send endExclusive command conn.output.writeByte(CMD_ENDEXCLUSIVE); conn.output.flush(); // check return value for success checkReturnValue(conn); } } catch(Exception e) { /* drain */ } } } } //-------- //-------- Primitive 1-Wire Network data methods //-------- /** * Sends a Reset to the 1-Wire Network. * * @return the result of the reset. Potential results are: * <ul> * <li> 0 (RESET_NOPRESENCE) no devices present on the 1-Wire Network. * <li> 1 (RESET_PRESENCE) normal presence pulse detected on the 1-Wire * Network indicating there is a device present. * <li> 2 (RESET_ALARM) alarming presence pulse detected on the 1-Wire * Network indicating there is a device present and it is in the * alarm condition. This is only provided by the DS1994/DS2404 * devices. * <li> 3 (RESET_SHORT) inticates 1-Wire appears shorted. This can be * transient conditions in a 1-Wire Network. Not all adapter types * can detect this condition. * </ul> * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public int reset() throws OneWireIOException, OneWireException { try { synchronized(conn) { // send reset command conn.output.writeByte(CMD_RESET); conn.output.flush(); // check return value for success checkReturnValue(conn); // next parameter should be the return from reset return conn.input.readInt(); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Sends a bit to the 1-Wire Network. * * @param bitValue the bit value to send to the 1-Wire Network. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public void putBit (boolean bitValue) throws OneWireIOException, OneWireException { try { synchronized(conn) { // send putBit command conn.output.writeByte(CMD_PUTBIT); // followed by the bit conn.output.writeBoolean(bitValue); conn.output.flush(); // check return value for success checkReturnValue(conn); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Gets a bit from the 1-Wire Network. * * @return the bit value recieved from the the 1-Wire Network. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public boolean getBit () throws OneWireIOException, OneWireException { try { synchronized(conn) { // send getBit command conn.output.writeByte(CMD_GETBIT); conn.output.flush(); // check return value for success checkReturnValue(conn); // next parameter should be the return from getBit return conn.input.readBoolean(); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Sends a byte to the 1-Wire Network. * * @param byteValue the byte value to send to the 1-Wire Network. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public void putByte (int byteValue) throws OneWireIOException, OneWireException { try { synchronized(conn) { // send putByte command conn.output.writeByte(CMD_PUTBYTE); // followed by the byte conn.output.writeByte(byteValue); conn.output.flush(); // check return value for success checkReturnValue(conn); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Gets a byte from the 1-Wire Network. * * @return the byte value received from the the 1-Wire Network. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public int getByte () throws OneWireIOException, OneWireException { try { synchronized(conn) { // send getByte command conn.output.writeByte(CMD_GETBYTE); conn.output.flush(); // check return value for success checkReturnValue(conn); // next parameter should be the return from getByte return conn.input.readByte()&0x0FF; } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Gets a block of data from the 1-Wire Network. * * @param len length of data bytes to receive * * @return the data received from the 1-Wire Network. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public byte[] getBlock (int len) throws OneWireIOException, OneWireException { byte[] buffer = new byte[len]; getBlock(buffer,0,len); return buffer; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?