📄 lserialadapter.java
字号:
* * @return <code>String</code> of the adapter version. It will return * "<na>" if the adapter version is not or cannot be known. * * @throws OneWireIOException on a 1-Wire communication error such as * no device present. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public String getAdapterVersion() throws OneWireIOException, OneWireException { String version_string = "DS9097 adapter"; // Does not look for DS9097E yet return version_string; } /** * Retrieve the address of the adapter if it has one. * * @return <code>String</code> of the adapter address. It will return "<na>" if * the adapter does not have an address. The address is a string representation of an * 1-Wire address. * * @throws OneWireIOException on a 1-Wire communication error such as * no device present. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * @see com.dalsemi.onewire.utils.Address */ public String getAdapterAddress() throws OneWireIOException, OneWireException { // there is no ID return "<no adapter address>"; } //-------- //-------- Finding iButtons //-------- /** Field currentPosition */ int currentPosition; // the current position in the list of all devices. /** * Returns <code>true</code> if the first iButton or 1-Wire device * is found on the 1-Wire Network. * If no devices are found, then <code>false</code> will be returned. * * @return <code>true</code> if an iButton or 1-Wire device is found. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public boolean findFirstDevice() throws OneWireIOException, OneWireException { // reset the internal rom buffer resetSearch = true; return findNextDevice(); } /** * Returns <code>true</code> if the next iButton or 1-Wire device * is found. The previous 1-Wire device found is used * as a starting point in the search. If no more devices are found * then <code>false</code> will be returned. * * @return <code>true</code> if an iButton or 1-Wire device is found. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public boolean findNextDevice() throws OneWireIOException, OneWireException { boolean retval; try { // acquire exclusive use of the port beginLocalExclusive(); while (true) { retval = search(resetSearch); if (retval) { resetSearch = false; // check if this is an OK family type if (isValidFamily(CurrentDevice)) return true; // Else, loop to the top and do another search. } else { resetSearch = true; return false; } } } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Copies the 'current' iButton address being used by the adapter into * the array. This address is the last iButton or 1-Wire device found * in a search (findNextDevice()...). * * @param address An array to be filled with the current iButton address. * @see com.dalsemi.onewire.utils.Address */ public void getAddress(byte[] address) { System.arraycopy(CurrentDevice, 0, address, 0, 8); } /** * Copies the provided 1-Wire device address into the 'current' * array. This address will then be used in the getDeviceContainer() * method. Permits the adapter instance to create containers * of devices it did not find in a search. * * @param address An array to be copied into the current iButton * address. */ public void setAddress(byte[] address) { System.arraycopy(address, 0, CurrentDevice, 0, 8); } //-------- //-------- Finding iButton options //-------- /** * Set the 1-Wire Network search to find only iButtons and 1-Wire * devices that are in an 'Alarm' state that signals a need for * attention. Not all iButton types * have this feature. Some that do: DS1994, DS1920, DS2407. * This selective searching can be canceled with the * 'setSearchAllDevices()' method. * * @see #setNoResetSearch */ public void setSearchOnlyAlarmingDevices() { searchOnlyAlarmingButtons = true; } /** * Set the 1-Wire Network search to not perform a 1-Wire * reset before a search. This feature is chiefly used with * the DS2409 1-Wire coupler. * The normal reset before each search can be restored with the * 'setSearchAllDevices()' method. */ public void setNoResetSearch() { skipResetOnSearch = true; } /** * Set the 1-Wire Network search to find all iButtons and 1-Wire * devices whether they are in an 'Alarm' state or not and * restores the default setting of providing a 1-Wire reset * command before each search. (see setNoResetSearch() method). * * @see #setNoResetSearch */ public void setSearchAllDevices() { searchOnlyAlarmingButtons = false; skipResetOnSearch = false; } //-------- //-------- 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("DS9097EAdapter: 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 { char send_byte; try { // acquire exclusive use of the port beginLocalExclusive(); // make sure adapter is present if (adapterDetected()) { if (bitValue) send_byte = (char) 0xFF; else send_byte = (char) 0x00; serial.flush(); serial.write(send_byte); char[] result = serial.readWithTimeout(1); if (result[0] != send_byte) throw new OneWireIOException("Error during putBit(), 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 (adapterDetected()) { serial.flush(); serial.write((char)0x00FF); char[] result = serial.readWithTimeout(1); return (result[0] == 0xFF); } 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); } /** * 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -