📄 userialadapter.java
字号:
* @param family the code of the family type NOT to target in searches * @see Address * @see #targetAllFamilies */ public void excludeFamily (int familyID) { // replace exclude family array with 1 element array owState.searchExcludeFamilies = new byte [1]; owState.searchExcludeFamilies [0] = ( byte ) familyID; } /** * 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 familyID []) { // replace exclude family array with new array owState.searchExcludeFamilies = new byte [familyID.length]; System.arraycopy(familyID, 0, owState.searchExcludeFamilies, 0, familyID.length); } //-------- //-------- 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> * * @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 { return serial.beginExclusive(blocking); } /** * 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 () { serial.endExclusive(); } /** * Gets exclusive use of the 1-Wire to communicate with an iButton or * 1-Wire Device if it is not already done. Used to make methods * thread safe. * * @throws OneWireException on a setup error with the 1-Wire adapter */ private void beginLocalExclusive () throws OneWireException { // check if there is no such port if (serial == null) throw new OneWireException("USerialAdapter: port not selected "); // check if already have exclusive use if (serial.haveExclusive()) return; else { synchronized (syncObject) { serial.beginExclusive(true); haveLocalUse = true; } } } /** * Relinquishes local exclusive control of the 1-Wire Network. This * just checks if we did our own 'beginExclusive' block and frees it. */ private void endLocalExclusive () { synchronized (syncObject) { if (haveLocalUse) { haveLocalUse = false; serial.endExclusive(); } } } //-------- //-------- Primitive 1-Wire Network data methods //-------- /** * 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 { // acquire exclusive use of the port beginLocalExclusive(); // make sure adapter is present if (uAdapterPresent()) { // check for pending power conditions if (owState.oneWireLevel != LEVEL_NORMAL) setPowerNormal(); // flush out the com buffer serial.flush(); // build a message to send bit to the U brick uBuild.restart(); int bit_offset = uBuild.dataBit(bitValue, owState.levelChangeOnNextBit); // check if just started power delivery if (owState.levelChangeOnNextBit) { // clear the primed condition owState.levelChangeOnNextBit = false; // set new level state owState.oneWireLevel = LEVEL_POWER_DELIVERY; } // send and receive char[] result_array = uTransaction(uBuild); // check for echo if (bitValue != uBuild.interpretOneWireBit(result_array [bit_offset])) throw new OneWireIOException( "1-Wire communication error, echo was incorrect"); } else throw new OneWireIOException("Error communicating with adapter"); } catch(IOException ioe) { throw new OneWireIOException(ioe.toString()); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * 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 { // acquire exclusive use of the port beginLocalExclusive(); // make sure adapter is present if (uAdapterPresent()) { // check for pending power conditions if (owState.oneWireLevel != LEVEL_NORMAL) setPowerNormal(); // flush out the com buffer serial.flush(); // build a message to send bit to the U brick uBuild.restart(); int bit_offset = uBuild.dataBit(true, owState.levelChangeOnNextBit); // check if just started power delivery if (owState.levelChangeOnNextBit) { // clear the primed condition owState.levelChangeOnNextBit = false; // set new level state owState.oneWireLevel = LEVEL_POWER_DELIVERY; } // send and receive char[] result_array = uTransaction(uBuild); // check the result if (result_array.length == (bit_offset + 1)) return uBuild.interpretOneWireBit(result_array [bit_offset]); else return false; } else throw new OneWireIOException("Error communicating with adapter"); } catch(IOException ioe) { throw new OneWireIOException(ioe.toString()); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * 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 { byte[] temp_block = new byte [1]; temp_block [0] = ( byte ) byteValue; dataBlock(temp_block, 0, 1); // check to make sure echo was what was sent if (temp_block[0] != ( byte ) byteValue) throw new OneWireIOException("Error short on 1-Wire during putByte"); } /** * 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 { byte[] temp_block = new byte [1]; temp_block [0] = ( byte ) 0xFF; dataBlock(temp_block, 0, 1); if (temp_block.length == 1) return (temp_block [0] & 0xFF); else throw new OneWireIOException("Error communicating with adapter"); } /** * Get 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[] temp_block = new byte [len]; // set block to read 0xFF for (int i = 0; i < len; i++) temp_block [i] = ( byte ) 0xFF; getBlock(temp_block, len); return temp_block; } /** * Get a block of data from the 1-Wire Network and write it into * the provided array. * * @param arr array in which to write the received bytes * @param len length of data bytes to receive * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public void getBlock (byte[] arr, int len) throws OneWireIOException, OneWireException { getBlock(arr, 0, len); } /** * Get a block of data from the 1-Wire Network and write it into * the provided array. * * @param arr array in which to write the received bytes * @param off offset into the array to start * @param len length of data bytes to receive * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public void getBlock (byte[] arr, int off, int len) throws OneWireIOException, OneWireException { // set block to read 0xFF for (int i = off; i < len; i++) arr [i] = ( byte ) 0xFF; dataBlock(arr, off, len); } /** * Sends a block of data and returns the data received in the same array. * This method is used when sending a block that contains reads and writes. * The 'read' portions of the data block need to be pre-loaded with 0xFF's. * It starts sending data from the index at offset 'off' for length 'len'. * * @param dataBlock array of data to transfer to and from the 1-Wire Network. * @param off offset into the array of data to start * @param len length of data to send / receive starting at 'off' * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public void dataBlock (byte dataBlock [], int off, int len) throws OneWireIOException, OneWireException { int data_offset; char[] ret_data; try { // acquire exclusive use of the port beginLocalExclusive(); // make sure adapter is present if (uAdapterPresent()) { // check for pending power conditions if (owState.oneWireLevel != LEVEL_NORMAL) setPowerNormal(); // set the correct baud rate to stream this operation setStreamingSpeed(uBuild.OPERATION_BYTE); // flush out the com buffer serial.flush(); // build a message to write/read data bytes to the U brick uBuild.restart(); // check for primed byte if ((len == 1) && owState.levelChangeOnNextByte) { data_offset = uBuild.primedDataByte(dataBlock [off]); owState.levelChangeOnNextByte = false; // send and receive ret_data = uTransaction(uBuild);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -