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

📄 memorybankeeprom.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      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   {      throw new OneWireException(            "Read page with CRC not supported in 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   {      throw new OneWireException(            "Read page with CRC not supported in this memory bank");   }   //--------   //-------- OTPMemoryBank I/O methods   //--------   /**    * Lock the specifed page in the current memory bank.  Not supported    * by all devices.  See the method 'canLockPage()'.    *    * @param  page   number of page to lock    *    * @throws OneWireIOException    * @throws OneWireException    */   public void lockPage (int page)      throws OneWireIOException, OneWireException   {      byte[] wr_byte = new byte[1];      wr_byte[0] = (byte) 0x6C;      if(page == 0)      {         mbLock.write(0, wr_byte, 0, 1);      }      else      {         mbLock.write(1, wr_byte, 0, 1);      }      // read back to verify      if (!isPageLocked(page))      {         forceVerify();         throw new OneWireIOException(            "Read back from write incorrect, could not lock page");      }   }   /**    * Query to see if the specified page is locked.    * See the method 'canLockPage()'.    *    * @param  page  number of page to see if locked    *    * @return  'true' if page locked.    *    * @throws OneWireIOException    * @throws OneWireException    */   public boolean isPageLocked (int page)      throws OneWireIOException, OneWireException   {      byte[] rd_byte = new byte[2];      if(page == 0)      {         mbLock.read(0,false,rd_byte,0,2);         if((rd_byte[0] == 0x6C) || (rd_byte[1] == 0x6C))            return true;         else            return false;      }      else      {         mbLock.read(1,false,rd_byte,0,1);         if(rd_byte[0] == 0x6C)            return true;         else            return false;      }   }   /**    * Redirect the specifed page in the current memory bank to a new page.    * Not supported by all devices.  See the method 'canRedirectPage()'.    *    * @param  page      number of page to redirect    * @param  newPage   new page number to redirect to    *    * @throws OneWireIOException    * @throws OneWireException    */   public void redirectPage (int page, int newPage)      throws OneWireIOException, OneWireException   {      throw new OneWireException("This memory bank does not support redirection.");   }   /**    * Query to see if the specified page is redirected.    * Not supported by all devices.  See the method 'canRedirectPage()'.    *    * @param  page      number of page check for redirection    *    * @return  return the new page number or 0 if not redirected    *    * @throws OneWireIOException    * @throws OneWireException    *    * @deprecated  As of 1-Wire API 0.01, replaced by {@link #getRedirectedPage(int)}    */   public int isPageRedirected (int page)      throws OneWireIOException, OneWireException   {      throw new OneWireException("This memory bank does not support redirection.");   }   /**    * Gets the page redirection of the specified page.    * Not supported by all devices.    *    * @param  page  page to check for redirection    *    * @return  the new page number or 0 if not redirected    *    * @throws OneWireIOException on a 1-Wire communication error such as    *         no device present or a CRC read from the device is incorrect.  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 #canRedirectPage() canRedirectPage    * @see #redirectPage(int,int) redirectPage    * @since 1-Wire API 0.01    */   public int getRedirectedPage (int page)      throws OneWireIOException, OneWireException   {      throw new OneWireException("This memory bank does not support redirection.");   }   /**    * Lock the redirection option for the specifed page in the current    * memory bank. Not supported by all devices.  See the method    * 'canLockRedirectPage()'.    *    * @param  page      number of page to redirect    *    * @throws OneWireIOException    * @throws OneWireException    */   public void lockRedirectPage (int page)      throws OneWireIOException, OneWireException   {      throw new OneWireException("This memory bank does not support redirection.");   }   /**    * Query to see if the specified page has redirection locked.    * Not supported by all devices.  See the method 'canRedirectPage()'.    *    * @param  page      number of page check for locked redirection    *    * @return  return 'true' if redirection is locked for this page    *    * @throws OneWireIOException    * @throws OneWireException    */   public boolean isRedirectPageLocked (int page)      throws OneWireIOException, OneWireException   {      throw new OneWireException("This memory bank does not support redirection.");   }   //--------   //-------- 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         if (doSetSpeed)         {            // 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;      }   }   /**    * Write to the Scratch Pad, which is a max of 8 bytes...  Note that if    * less than 8 bytes are written, the ending offset will still report    * that a full eight bytes are on the buffer.  This means that all 8 bytes    * of the data in the scratchpad will be copied, not just the bytes user    * wrote into it.    *    * @param  addr          the address to write the data to    * @param  out_buf       byte array to write into scratch pad    * @param  offset        offset into out_buf to write the data    * @param  len           length of the write data    *    * @throws OneWireIOException    * @throws OneWireException    */   public boolean writeScratchpad(int addr, byte[] out_buf, int offset, int len)      throws OneWireIOException, OneWireException   {      byte[] send_block = new byte [14];      // protect send buffer      // since the scratchpad is only eight bytes, there is no reason to write      // more than eight bytes..  and we can optimize our send buffer's size.      if(len>8)         len = 8;      // access the device      if (ib.adapter.select(ib.getAddress()))      {         int cnt = 0;         // set data block up         // start by sending the write scratchpad command         send_block [cnt++] = WRITE_SCRATCHPAD_COMMAND;         // followed by the target address         send_block [cnt++] = ( byte ) (addr & 0x00FF);         send_block [cnt++] = ( byte ) (((addr & 0x00FFFF) >>> 8) & 0x00FF);         // followed by the data to write to the scratchpad         System.arraycopy(out_buf,offset,send_block,3,len);         cnt += len;         // followed by two bytes for reading CRC16 value         send_block [cnt++] = ( byte ) 0x00FF;         send_block [cnt++] = ( byte ) 0x00FF;         // send the data         ib.adapter.dataBlock(send_block, 0, cnt);         // verify the CRC is correct         if (CRC16.compute(send_block, 0, cnt) != 0x0000B001)            throw new OneWireIOException("Invalid CRC16 in Writing Scratch Pad");      }      else         throw new OneWireIOException("Device select failed.");      return true;   }   /**    * Copy all 8 bytes of the Sratch Pad to a certain address in memory.    *    * @param addr the address to copy the data to    * @param auth byte[] containing write authorization    *    * @throws OneWireIOException    * @throws OneWireException    */   public synchronized void copyScratchpad(byte[] es_data)      throws OneWireIOException, OneWireException   {      byte[] send_block = new byte[4];      // access the device      if (ib.adapter.select(ib.getAddress()))      {         // ending address with data status         send_block[3] = es_data[2];//ES;         // address 2         send_block[2] = es_data[1];//TA2         // address 1         send_block[1] = es_data[0];//TA1;         // Copy command         send_block[0] = COPY_SCRATCHPAD_COMMAND;         // send copy scratchpad command         ib.adapter.dataBlock(send_block, 0, 3);         // provide strong pull-up for copy         ib.adapter.setPowerDuration(DSPortAdapter.DELIVERY_INFINITE);         ib.adapter.startPowerDelivery(DSPortAdapter.CONDITION_AFTER_BYTE);         ib.adapter.putByte(send_block[3]);         // pause before checking result         try         {            Thread.sleep(12);         }         catch (InterruptedException e){}         ib.adapter.setPowerNormal();         // get result         byte test = ( byte ) ib.adapter.getByte();         if(test == ( byte ) 0x00FF)         {            throw new OneWireIOException("The scratchpad did not get copied to memory.");         }      }      else         throw new OneWireIOException("Device select failed.");   }   /**    * Read from the Scratch Pad, which is a max of 8 bytes.    *    * @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)    *                       Can be 'null' if extra info is not needed.    *    * @throws OneWireIOException    * @throws OneWireException    */   public boolean readScratchpad (byte[] readBuf, int offset, int len,                                  byte[] es_data)      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 [14];      raw_buf [0] = READ_SCRATCHPAD_COMMAND;      System.arraycopy(ffBlock, 0, raw_buf, 1, 13);      // do data block for TA1, TA2, and E/S      // followed by 8 bytes of data and 2 bytes of crc      ib.adapter.dataBlock(raw_buf, 0, 14);      // verify CRC16 is correct      if (CRC16.compute(raw_buf, 0, 14) == 0x0000B001)      {         // optionally extract the extra info         if (es_data != null)            System.arraycopy(raw_buf, 1, es_data, 0, 3);         System.arraycopy(raw_buf, 4, readBuf, offset, len);         // success         return true;      }      else         throw new OneWireException("Error due to CRC.");   }}

⌨️ 快捷键说明

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