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

📄 memorybankscratch.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      // 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      {         forceVerify();         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];      // attempt to put device at speed      checkSpeed();      // read the scratchpad, discard extra information      readScratchpad(raw_buf, 0, pageLength, extraInfo);      // check if length is realistic      if (raw_buf [0] > maxPacketDataLength)      {         forceVerify();         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      {         forceVerify();         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 OneWireException(            "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   {      // 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");   }   //--------   //-------- ScratchPad methods   //--------   /**    * Read the scratchpad page of memory from a NVRAM device    * This method reads and returns the entire scratchpad after the byte    * offset regardless of the actual ending offset    *    * @param  readBuf       byte array to place read data into    *                       length of array is always pageLength.    * @param  offset        offset into readBuf to pug data    * @param  len           length in bytes to read    * @param  extraInfo     byte array to put extra info read into    *                       (TA1, TA2, e/s byte)    *                       length of array is always extraInfoLength.    *                       Can be 'null' if extra info is not needed.    *    * @throws OneWireIOException    * @throws OneWireException    */   public void readScratchpad (byte[] readBuf, int offset, int len,                               byte[] extraInfo)      throws OneWireIOException, OneWireException   {      // select the device      if (!ib.adapter.select(ib.address))      {         forceVerify();         throw new OneWireIOException("Device select failed");      }      // build first block      byte[] raw_buf = new byte [1 + extraInfoLength];      raw_buf [0] = READ_SCRATCHPAD_COMMAND;      System.arraycopy(ffBlock, 0, raw_buf, 1, extraInfoLength);      // do the first block for TA1, TA2, and E/S      ib.adapter.dataBlock(raw_buf, 0, 1 + extraInfoLength);      // optionally extract the extra info      if (extraInfo != null)         System.arraycopy(raw_buf, 1, extraInfo, 0, extraInfoLength);      // build the next block      System.arraycopy(ffBlock, 0, readBuf, offset, len);      // send second block to read data, return result      ib.adapter.dataBlock(readBuf, offset, len);   }   /**    * Write to the scratchpad page of memory a NVRAM device.    *    * @param  startAddr     starting address    * @param  writeBuf      byte array containing data to write    * @param  offset        offset into readBuf to place data    * @param  len           length in bytes to write    *    * @throws OneWireIOException    * @throws OneWireException    */   public void writeScratchpad (int startAddr, byte[] writeBuf, int offset,                                int len)      throws OneWireIOException, OneWireException   {      // select the device      if (!ib.adapter.select(ib.address))      {         forceVerify();         throw new OneWireIOException("Device select failed");      }      // build block to send      byte[] raw_buf = new byte [3 + len];      raw_buf [0] = WRITE_SCRATCHPAD_COMMAND;      raw_buf [1] = ( byte ) (startAddr & 0xFF);      raw_buf [2] = ( byte ) (((startAddr & 0xFFFF) >>> 8) & 0xFF);      System.arraycopy(writeBuf, offset, raw_buf, 3, len);      // send block, return result      ib.adapter.dataBlock(raw_buf, 0, len + 3);   }   /**    * Copy the scratchpad page to memory.    *    * @param  startAddr     starting address    * @param  len           length in bytes that was written already    *    * @throws OneWireIOException    * @throws OneWireException    */   public void copyScratchpad (int startAddr, int len)      throws OneWireIOException, OneWireException   {      // select the device      if (!ib.adapter.select(ib.address))      {         forceVerify();         throw new OneWireIOException("Device select failed");      }      // build block to send      byte[] raw_buf = new byte [5];      raw_buf [0] = COPY_SCRATCHPAD_COMMAND;      raw_buf [1] = ( byte ) (startAddr & 0xFF);      raw_buf [2] = ( byte ) (((startAddr & 0xFFFF) >>> 8) & 0xFF);      raw_buf [3] = ( byte ) ((startAddr + len - 1) & 0x1F);      raw_buf [4] = ( byte ) 0xFF;      // send block (check copy indication complete)      ib.adapter.dataBlock(raw_buf, 0, 5);      if ((raw_buf [4] & 0x0F0) != 0)      {         forceVerify();         throw new OneWireIOException("Copy scratchpad complete not found");      }   }   //--------   //-------- checkSpeed methods   //--------   /**    * 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         // 9-23-2003 shughes: added the check to see if the adapter is         // currently at the ibutton's max speed.  If it isn't, we should         // call the doSpeed() method, since the adapter might have         // changed speeds.         if (doSetSpeed || (ib.adapter.getSpeed()!=ib.getMaxSpeed()))         {            // attempt to set the correct speed and verify device present            ib.doSpeed();            // no execptions so clear flag            doSetSpeed = false;         }      }   }   /**    * 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 + -