📄 onewirecontainer18.java
字号:
* * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #waitForSuccessfulFinish() * @see #readScratchPad(byte[],int) * @see #copyScratchPad() */ public synchronized boolean eraseScratchPad (int page) throws OneWireIOException, OneWireException { if (doSpeedEnable && (!resume)) doSpeed(); // select the device if (!resume) adapter.select(address); else { adapter.reset(); adapter.putByte(RESUME); } // build block to send byte[] buffer = byte_buffer; buffer [0] = ERASE_SCRATCHPAD; buffer [1] = ( byte ) (page << 5); buffer [2] = ( byte ) (page >> 3); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Erase Scratchpad"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(address); IOHelper.write("target address: 0x"); IOHelper.writeHex((byte)buffer [2]); IOHelper.writeLineHex((byte)buffer [1]); IOHelper.writeLine("adapter.getSpeed()="+adapter.getSpeed()); IOHelper.writeLine("adapter.getPortTypeDescription()="+adapter.getPortTypeDescription()); IOHelper.writeLine("this.speed="+this.speed); IOHelper.writeLine("device isPresent: "+adapter.isPresent(this.address)); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ System.arraycopy(FF, 0, buffer, 3, 3); if(DEBUG) { IOHelper.writeLine("buffer:"); IOHelper.writeBytesHex(buffer,0,9); } // send block (check copy indication complete) adapter.dataBlock(buffer, 0, 6); if(DEBUG) { IOHelper.writeLine("buffer:"); IOHelper.writeBytesHex(buffer,0,9); IOHelper.writeLine("------------------------------------"); } if (buffer [5] == ( byte ) 0x0ff) return waitForSuccessfulFinish(); else return true; } /** * <p>Waits for the DS1963S's output to alternate. Several operations must * wait for the DS1963S to stop sending 0x0ff's and begin alternating its * output bits. This method reads until it finds a non-0x0ff <code>byte</code> or until it * reaches a specified number of tries, indicating failure.</p> * * <p>This method can often be optimized away. A normal 1-Wire transaction * involves writing and reading a known number of bytes. If a few more bytes * are read, a program can check to see if the DS1963S has started alternating * its output much quicker than calling this method will. For instance, * to copy the scratchpad, the source code might look like this: * <code><pre> * buffer [0] = COPY_SCRATCHPAD; * buffer [1] = TA1; * buffer [2] = TA2; * buffer [3] = ES; * * adapter.dataBlock(buffer,0,4); * return waitForSuccessfulFinish(); * </pre></code> * To optimize the code, read more bytes than required: * <code><pre> * buffer [0] = COPY_SCRATCHPAD; * buffer [1] = TA1; * buffer [2] = TA2; * buffer [3] = ES; * * //copy 0x0FF into the buffer, this effectively reads * System.arraycopy(FF, 0, buffer, 4, 5); * * //read 5 extra bytes * adapter.dataBlock(buffer, 0, 9); * * //if the last byte has not shown alternating output, * //still call waitForSuccessfulFinish(), else * //we are already done * if (buffer [8] == ( byte ) 0x0ff) * return waitForSuccessfulFinish(); * else * return true; * </pre></code></p> * * <p>The second method is faster because it is more expensive to * invoke another method that goes down to the native access layer * than it is to just read a few more bytes while the program is * already at the native access layer.</p> * * <p>See the datasheet for which operations function in this manner. * Only call this method after another method which has successfully * communicated with the DS1963S.</p> * * @return <code>true</code> if the DS1963S completed its operation successfully * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #copyScratchPad() * @see #eraseScratchPad(int) * @see #readAuthenticatedPage(int,byte[],int) */ public synchronized boolean waitForSuccessfulFinish () throws OneWireIOException, OneWireException { //this method should be called after another method, so let's not worry //about making sure the speed is right, it should be already int count = 0; while (adapter.getByte() == 0xff) { count++; if (count == block_wait_count) return false; } return true; } /** * <p>Reads a memory page from the DS1963S. Pages 0-15 are normal memory pages. * Pages 16 and 17 contain the secrets--the DS1963S will return all 1's (0x0ff * bytes). Page 18 is the physical location of the scratchpad, and will only * return valid data if the part is not in hidden mode (0x0ff bytes are returned * otherwise). Pages 19 and 20 contain the write cycle counters. * Page 21 contains the counter for the pseudo random number generator (PRNG). * Consult the datasheet for the memory maps of these special pages.</p> * * <p>Note that the same data can be returned through the <code>MemoryBank</code>s * returned by <code>getMemoryBanks()</code>. However, this method contains * several enhancements for faster reading.</p> * * @param pageNum page number to read * @param data array for the return of the data (must be at least 32 bytes long) * @param start offset into the byte array to start copying page data * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #readAuthenticatedPage(int,byte[],int) * @see #getMemoryBanks() */ public void readMemoryPage (int pageNum, byte[] data, int start) throws OneWireIOException, OneWireException { //don't need to be synchronized, since readMemoryPage(int, byte, int, byte[], int) is readMemoryPage(pageNum, READ_MEMORY, 32, data, start); } /* * read the contents of a data page */ private synchronized void readMemoryPage (int pageNum, byte COMMAND, int bytes_to_read, byte[] data, int start) throws OneWireIOException, OneWireException { if (doSpeedEnable && (!resume)) doSpeed(); if (!resume) adapter.select(address); else { adapter.reset(); adapter.putByte(RESUME); } byte[] buffer = byte_buffer; int addr = pageNum << 5; //pageNumber * 32 buffer [0] = COMMAND; buffer [1] = ( byte ) addr; buffer [2] = ( byte ) (addr >> 8); System.arraycopy(FF, 0, buffer, 3, bytes_to_read); adapter.dataBlock(buffer, 0, 3 + bytes_to_read); // copy data for return System.arraycopy(buffer, 3, data, start, bytes_to_read); } /** * <p>Reads and authenticates a page. See <code>readMemoryPage()</code> for a description * of page numbers and their contents. This method will also generate a signature for the * selected page, used in the authentication of roving (User) iButtons. * Extra data is returned with the page as well--such as the write cycle * counter for the page and the write cycle counter of the selected * secret page.</p> * <ul> * <li>32 bytes of page data </li> * <li>4 bytes secret counter for page </li> * <li>4 bytes write cycle counter for page </li> * <li>2 byte CRC </li> * </ul> * <p>Note that the CRC will be verified by this method, though it is returned with the data.</p> * * @param pageNum page number to read and authenticate * @param data array for the page data plus extra information (2 write cycle * counters of 4 bytes each, one 2 byte CRC, appended after 32 bytes of * the data page). This byte array must be at least 42 bytes long. * @param start offset to copy into the array * * @return <code>true</code> if successful, <code>false</code> if the operation * failed while waiting for the DS1963S's output to alternate * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #readMemoryPage(int,byte[],int) */ public boolean readAuthenticatedPage (int pageNum, byte[] data, int start) throws OneWireIOException, OneWireException { //don't need to be synchronized, since readMemoryPage(int, byte, int, byte[], int) is //read 42 bytes = 32 page bytes + 4 bytes secret counter + 4 bytes page counter + 2 bytes CRC readMemoryPage(pageNum, READ_AUTHENTICATED_PAGE, 42, data, start); int crc = CRC16.compute(READ_AUTHENTICATED_PAGE); crc = CRC16.compute(( byte ) (pageNum << 5), crc); //(pagenumber*32 = address) lower 8 bits crc = CRC16.compute(( byte ) (pageNum >>> 3), crc); //pagenumber*32 is pagenumber<<5, but //we want the upper 8 bits, so we would just do // (pagenum<<5)>>>8, so just make it one op // check CRC16 if (CRC16.compute(data, start, 42, crc) != 0xB001) { return false; } return (waitForSuccessfulFinish()); } /** * <p>Writes data to the scratchpad. In order to write to a data page using this method, * next call <code>readScratchPad()</code>, and then <code>copyScratchPad()</code>. * Note that the addresses passed to this method will be the addresses the data is * copied to if the <code>copyScratchPad()</code> method is called afterward.</p> * * <p>Also note that if too many bytes are written, this method will truncate the * data so that only a valid number of bytes will be sent.</p> * * @param targetPage the page number this data will eventually be copied to * @param targetPageOffset the offset on the page to copy this data to * @param inputbuffer the data that will be copied into the scratchpad * @param start offset into the input buffer for the data to write * @param length number of bytes to write * * @return <code>true</code> if successful, <code>false</code> on a CRC error * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. 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 #copyScratchPad() * @see #eraseScratchPad(int) * @see #readScratchPad(byte[],int) * @see #writeDataPage(int,byte[]) */ public synchronized boolean writeScratchPad (int targetPage, int targetPageOffset, byte[] inputbuffer, int start, int length) throws OneWireIOException, OneWireException { if (doSpeedEnable && (!resume)) doSpeed(); if (!resume) adapter.select(address); else { adapter.reset(); adapter.putByte(RESUME); } byte[] buffer = byte_buffer; int addr = (targetPage << 5) + targetPageOffset; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Writing Scratchpad of iButton"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(address); IOHelper.writeLine("targetPage: " + targetPage); IOHelper.writeLine("targetPageOffset: " + targetPageOffset); IOHelper.write("target address: 0x");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -