📄 memorybankee.java
字号:
* methods with an 'extraInfo' parameter can be used: * {@link #readPage(int,boolean,byte[],int,byte[]) readPage}, * {@link #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC}, and * {@link #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket}. * * @return <CODE> true </CODE> if reading the this memory bank's * pages provides extra information * * @see #readPage(int,boolean,byte[],int,byte[]) readPage(extra) * @see #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC(extra) * @see #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket(extra) * @since 1-Wire API 0.01 */ public boolean hasExtraInfo () { return false; } /** * Query to get the length in bytes of extra information that * is read when read a page in the current memory bank. See * 'hasExtraInfo()'. * * @return number of bytes in Extra Information read when reading * pages in the current memory bank. */ public int getExtraInfoLength () { return 0; } /** * Query to get a string description of what is contained in * the Extra Informationed return when reading pages in the current * memory bank. See 'hasExtraInfo()'. * * @return string describing extra information. */ public String getExtraInfoDescription () { return null; } //-------- //-------- MemoryBank I/O methods //-------- /** * Read memory in the current bank with no CRC checking (device or * data). The resulting data from this API may or may not be what is on * the 1-Wire device. It is recommends that the data contain some kind * of checking (CRC) like in the readPagePacket() method or have * the 1-Wire device provide the CRC as in readPageCRC(). readPageCRC() * however is not supported on all memory types, see 'hasPageAutoCRC()'. * If neither is an option then this method could be called more * then once to at least verify that the same thing is read consistantly. * * @param startAddr starting physical address * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * read() continious where the last one led off * and it is inside a 'beginExclusive/endExclusive' * block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * @param len length in bytes to read * * @throws OneWireIOException * @throws OneWireException */ public void read (int startAddr, boolean readContinue, byte[] readBuf, int offset, int len) throws OneWireIOException, OneWireException { int i, cnt = 0; byte[] raw_buf = new byte [2]; // attempt to put device at max desired speed if (!readContinue) checkSpeed(); // check if read exceeds memory if ((startAddr + len) > PAGE_LENGTH) throw new OneWireException("Read exceeds memory bank end"); // loop until get a non-0xFF value, or max 6 reads do { // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("Device select failed"); } // build start reading memory block raw_buf [0] = READ_MEMORY_COMMAND; raw_buf [1] = ( byte ) (startAddr & 0xFF); // do the first block for command, address ib.adapter.dataBlock(raw_buf, 0, 2); // pre-fill readBuf with 0xFF System.arraycopy(ffBlock, 0, readBuf, offset, len); // send the block ib.adapter.dataBlock(readBuf, offset, len); // see if result is non-0xFF for (i = 0; i < len; i++) if (readBuf [offset + i] != ( byte ) (0xFF)) return; try { Thread.sleep(10); } catch (InterruptedException e){} ; if (!ib.isPresent()) { forceVerify(); throw new OneWireIOException("Device not present on 1-Wire"); } } while (++cnt < 6); // still present, assume data really is 0xFF's } /** * Write memory in the current bank. It is recommended that * when writing data that some structure in the data is created * to provide error free reading back with read(). Or the * method 'writePagePacket()' could be used which automatically * wraps the data in a length and CRC. * * When using on Write-Once devices care must be taken to write into * into empty space. If write() is used to write over an unlocked * page on a Write-Once device it will fail. If write verification * is turned off with the method 'setWriteVerification(false)' then * the result will be an 'AND' of the existing data and the new data. * * @param startAddr starting address * @param writeBuf byte array containing data to write * @param offset offset into writeBuf to get data * @param len length in bytes to write * * @throws OneWireIOException * @throws OneWireException */ public void write (int startAddr, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { int i; // return if nothing to do if (len == 0) return; // check if power delivery is available if (!ib.adapter.canDeliverPower()) throw new OneWireException( "Power delivery required but not available"); // attempt to put device at max desired speed checkSpeed(); // check if write exceeds memory if ((startAddr + len) > PAGE_LENGTH) throw new OneWireException("Write exceeds memory bank end"); // write the page of data to scratchpad writeScratchpad(startAddr, writeBuf, offset, len); // read to verify ok byte[] raw_buf = new byte [PAGE_LENGTH]; readScratchpad(raw_buf, startAddr, 0, PAGE_LENGTH); // check to see if the same for (i = 0; i < len; i++) if (raw_buf [i] != writeBuf [i + offset]) { forceVerify(); throw new OneWireIOException( "Read back scratchpad verify had incorrect data"); } // do the copy copyScratchpad(); // check on write verification if (writeVerification) { // read back to verify read(startAddr, false, raw_buf, 0, len); for (i = 0; i < len; i++) if (raw_buf [i] != writeBuf [i + offset]) { forceVerify(); throw new OneWireIOException( "Read back verify had incorrect data"); } } } //-------- //-------- PagedMemoryBank I/O methods //-------- /** * Read page in the current bank with no * CRC checking (device or data). The resulting data from this API * may or may not be what is on the 1-Wire device. It is recommends * that the data contain some kind of checking (CRC) like in the * readPagePacket() method or have the 1-Wire device provide the * CRC as in readPageCRC(). readPageCRC() however is not * supported on all memory types, see 'hasPageAutoCRC()'. * If neither is an option then this method could be called more * then once to at least verify that the same thing is read consistantly. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPage() continious where the last one * led off and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { // check if read exceeds memory if (page != 0) throw new OneWireException("Page read exceeds memory bank end"); read(0, readContinue, readBuf, offset, PAGE_LENGTH); } /** * Read page with extra information in the current bank with no * CRC checking (device or data). The resulting data from this API * may or may not be what is on the 1-Wire device. It is recommends * that the data contain some kind of checking (CRC) like in the * readPagePacket() method or have the 1-Wire device provide the * CRC as in readPageCRC(). readPageCRC() however is not * supported on all memory types, see 'hasPageAutoCRC()'. * If neither is an option then this method could be called more * then once to at least verify that the same thing is read consistantly. * See the method 'hasExtraInfo()' for a description of the optional * extra information some devices have. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPage() continious where the last one * led off and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * @param extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page with extra-info not supported by this memory bank"); } /** * Read a Universal Data Packet. * * The Universal Data Packet always starts on page boundaries but * can end anywhere in the page. The structure specifies the length of * data bytes not including the length byte and the CRC16 bytes. * There is one length byte. The CRC16 is first initialized to * the page number. This provides a check to verify the page that * was intended is being read. The CRC16 is then calculated over * the length and data bytes. The CRC16 is then inverted and stored * low byte first followed by the high byte. This is structure is * used by this method to verify the data but is not returned, only * the data payload is returned. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * * @return number of data bytes read from the device and written to * readBuf at the offset. * * @throws OneWireIOException * @throws OneWireException */ public int readPagePacket (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { byte[] raw_buf = new byte [PAGE_LENGTH]; // attempt to put device at speed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -