⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memorybanknv.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   /**    * 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 extraInfoDescription;   }   //--------   //-------- 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;      // attempt to put device at max desired speed      if (!readContinue)         sp.checkSpeed();      // check if read exceeds memory      if ((startAddr + len) > (pageLength * numberPages))         throw new OneWireException("Read exceeds memory bank end");      // see if need to access the device      if (!readContinue)      {         // select the device         if (!ib.adapter.select(ib.address))         {            sp.forceVerify();            throw new OneWireIOException("Device select failed");         }         // build start reading memory block         int    addr    = startAddr + startPhysicalAddress;         byte[] raw_buf = new byte [3];         raw_buf [0] = READ_MEMORY_COMMAND;         raw_buf [1] = ( byte ) (addr & 0xFF);         raw_buf [2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF);         // do the first block for command, address         ib.adapter.dataBlock(raw_buf, 0, 3);      }      // pre-fill readBuf with 0xFF       int pgs   = len / pageLength;      int extra = len % pageLength;      for (i = 0; i < pgs; i++)         System.arraycopy(ffBlock, 0, readBuf, offset + i * pageLength,                          pageLength);      System.arraycopy(ffBlock, 0, readBuf, offset + pgs * pageLength, extra);      // send second block to read data, return result      ib.adapter.dataBlock(readBuf, offset, len);   }   /**    * 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, room_left;      // return if nothing to do      if (len == 0)         return;      // attempt to put device at speed      sp.checkSpeed();      // check if write exceeds memory      if ((startAddr + len) > size)         throw new OneWireException("Write exceeds memory bank end");      // check if trying to write read only bank      if (isReadOnly())         throw new OneWireException("Trying to write read-only memory bank");      // loop while still have pages to write      int    startx    = 0, nextx = 0;   // (start and next index into writeBuf)      byte[] raw_buf   = new byte [pageLength];      byte[] extra_buf = new byte [sp.getExtraInfoLength()];      int    abs_addr  = startPhysicalAddress + startAddr;      int    pl        = pageLength;      do      {         // calculate room left in current page         room_left = pl - ((abs_addr + startx) % pl);         // check if block left will cross end of page         if ((len - startx) > room_left)            nextx = startx + room_left;         else            nextx = len;         // write the page of data to scratchpad         sp.writeScratchpad(abs_addr + startx, writeBuf, offset + startx,                            nextx - startx);         // read to verify ok         sp.readScratchpad(raw_buf, 0, pl, extra_buf);         // check to see if the same         for (i = 0; i < (nextx - startx); i++)            if (raw_buf [i] != writeBuf [i + offset + startx])            {               sp.forceVerify();               throw new OneWireIOException(                  "Read back of scratchpad had incorrect data");            }         // check to make sure that the address is correct           if ((((extra_buf [0] & 0x00FF) | ((extra_buf [1] << 8) & 0x00FF00))                 & 0x00FFFF) != (abs_addr + startx))         {            sp.forceVerify();            throw new OneWireIOException(               "Address read back from scrachpad was incorrect");         }         // do the copy         sp.copyScratchpad(abs_addr + startx, nextx - startx);         // point to next index         startx = nextx;      }      while (nextx < len);   }   //--------   //-------- 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   {      read(page * pageLength, readContinue, readBuf, offset, pageLength);   }   /**    * 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 [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      int abs_page = (startPhysicalAddress / pageLength) + page;      if (CRC16.compute(raw_buf, 0, raw_buf [0] + 3, abs_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   {      // only needs to be implemented if supported by hardware      throw new OneWireException(         "Read packet with extra-info not supported by this memory bank");   }   /**    * 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 abs_page = (startPhysicalAddress / pageLength) + page;      int crc = CRC16.compute(raw_buf, 0, len + 1, abs_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   {      // only needs to be implemented if supported by hardware      throw new OneWireException(         "Read page with CRC not supported by this memory bank");   }   /**    * 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   {      // only needs to be implemented if supported by hardware      throw new OneWireException(         "Read page with CRC and extra-info not supported by this memory bank");   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -