📄 memorybankshaee.java
字号:
* 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 [pageLength]; // read the page readPage(page, readContinue, raw_buf, 0); // check if length is realistic if ((raw_buf [0] & 0x00FF) > maxPacketDataLength) throw new OneWireIOException("Invalid length in packet."); // verify the CRC is correct if (CRC16.compute(raw_buf, 0, raw_buf [0] + 3, page) == 0x0000B001) { // extract the data out of the packet System.arraycopy(raw_buf, 1, readBuf, offset, raw_buf [0]); // return the length return raw_buf [0]; } else throw new OneWireIOException("Invalid CRC16 in packet read."); } /** * Read a Universal Data Packet and extra information. See the * method 'readPagePacket()' for a description of the packet structure. * 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 * 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 * @param extraInfo byte array to put extra info read into * * @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, byte[] extraInfo) throws OneWireIOException, OneWireException { byte[] raw_buf = new byte [pageLength]; if(!checked) checked = ib.checkStatus(); if(!hasPageAutoCRC()) throw new OneWireException("This memory bank page doesn't have CRC capabilites."); // read the page readAuthenticatedPage(page, raw_buf, 0, extraInfo, 0); // check if length is realistic if ((raw_buf [0] & 0x00FF) > maxPacketDataLength) throw new OneWireIOException("Invalid length in packet."); // verify the CRC is correct if (CRC16.compute(raw_buf, 0, raw_buf [0] + 3, page) == 0x0000B001) { // extract the data out of the packet System.arraycopy(raw_buf, 1, readBuf, offset, raw_buf [0]); // return the length return raw_buf [0]; } else throw new OneWireIOException("Invalid CRC16 in packet read."); } /** * Write a Universal Data Packet. See the method 'readPagePacket()' * for a description of the packet structure. * * @param page page number to write packet to * @param writeBuf data byte array to write * @param offset offset into writeBuf where data to write is * @param len number of bytes to write * * @throws OneWireIOException * @throws OneWireException */ public void writePagePacket (int page, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { // make sure length does not exceed max if (len > maxPacketDataLength) throw new OneWireIOException( "Length of packet requested exceeds page size."); // see if this bank is general read/write if (!generalPurposeMemory) throw new OneWireException( "Current bank is not general purpose memory."); // construct the packet to write byte[] raw_buf = new byte [len + 3]; raw_buf [0] = ( byte ) len; System.arraycopy(writeBuf, offset, raw_buf, 1, len); int crc = CRC16.compute(raw_buf, 0, len + 1, page); raw_buf [len + 1] = ( byte ) (~crc & 0xFF); raw_buf [len + 2] = ( byte ) (((~crc & 0xFFFF) >>> 8) & 0xFF); // write the packet, return result write(page * pageLength, raw_buf, 0, len + 3); } /** * Read a complete memory page with CRC verification provided by the * device. Not supported by all devices. See the method * 'hasPageAutoCRC()'. * * @param page page number to read * @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 * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { byte[] extra = new byte [20]; byte[] pg = new byte [32]; if(!checked) checked = ib.checkStatus(); if(!hasPageAutoCRC()) throw new OneWireException("This memory bank doesn't have CRC capabilites."); if(!readAuthenticatedPage(page,pg,0,extra,0)) throw new OneWireException("Read didn't work."); System.arraycopy(pg,0,readBuf,offset,pageLength); } /** * Read a complete memory page with CRC verification provided by the * device with extra information. Not supported by all devices. * See the method 'hasPageAutoCRC()'. * See the method 'hasExtraInfo()' for a description of the optional * extra information. * * @param page page number to read * @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 * @param extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { byte[] pg = new byte [32]; if(!checked) checked = ib.checkStatus(); if(!hasPageAutoCRC()) throw new OneWireException("This memory bank doesn't have CRC capabilities."); if(!readAuthenticatedPage(page,pg,0,extraInfo,0)) throw new OneWireException("Read didn't work."); System.arraycopy(pg,0,readBuf,offset,pageLength); } // ------------------------ // Setting status // ------------------------ /** * Write protect the memory bank. */ public void writeprotect() { readOnly = true; readWrite = false; } /** * Sets the EPROM mode for this page. */ public void setEPROM() { writeOnce = true; } // ------------------------ // Extras // ------------------------ /** * Check the device speed if has not been done before or if * an error was detected. * * @throws OneWireIOException * @throws OneWireException */ public void checkSpeed () throws OneWireIOException, OneWireException { synchronized(this) { // only check the speed if (doSetSpeed) { // attempt to set the correct speed and verify device present ib.doSpeed(); // no execptions so clear flag doSetSpeed = false; } } } /** * Reads authenticated page. * * @param page the page number in this bank to read from. * @param data the data read from the address * @param extra_info the MAC calculated for this function * * @throws OneWireIOException * @throws OneWireException */ public boolean readAuthenticatedPage(int page, byte[] data, int dataStart, byte[] extra_info, int extraStart) throws OneWireException, OneWireIOException { byte[] send_block = new byte [40]; byte[] challenge = new byte [8]; int addr = (page*pageLength) + startPhysicalAddress; ib.getChallenge(challenge, 4); scratchpad.writeScratchpad(addr,challenge,0,8); // access the device if (!adapter.select(ib.getAddress())) throw new OneWireIOException("Device select failed."); // Read Authenticated Command send_block[0] = READ_AUTH_PAGE; // address 1 send_block[1] = ( byte ) (addr & 0xFF); // address 2 send_block[2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF); // data + FF byte System.arraycopy(ffBlock,0,send_block,3,35); // now send the block adapter.dataBlock(send_block,0,38); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("-------------------------------------------------------------"); IOHelper.writeLine("ReadAuthPage - send_block:"); IOHelper.writeBytesHex(send_block,0,38); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ // verify CRC16 is correct if (CRC16.compute(send_block,0,38,0) != 0x0000B001) { throw new OneWireException("First CRC didn't pass."); } System.arraycopy(send_block,3,data,dataStart,32); System.arraycopy(ffBlock,0,send_block,0,22); //adapter.startPowerDelivery(DSPortAdapter.CONDITION_NOW); try { Thread.sleep(2); } catch(InterruptedException ie) {;} adapter.dataBlock(send_block,0,22); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("ReadAuthPage - MAC:"); IOHelper.writeBytesHex(send_block,0,20); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ // verify CRC16 is correct if (CRC16.compute(send_block, 0, 22,0) != 0x0000B001) { throw new OneWireException("Second CRC didn't pass."); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("next read:"); IOHelper.writeBytesHex(send_block,0,22); IOHelper.writeLine("-------------------------------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ System.arraycopy(send_block,0,extra_info,extraStart,20); return true; } /** * Set the flag to indicate the next 'checkSpeed()' will force * a speed set and verify 'doSpeed()'. */ public void forceVerify () { synchronized (this) { doSetSpeed = true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -