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

📄 upacketbuilder.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      setToDataMode();      // create the search sequence character array      char[] search_sequence = new char [16];      // get a copy of the current ID      char[] id = new char [8];      for (int i = 0; i < 8; i++)         id [i] = ( char ) (mState.ID [i] & 0xFF);      // clear the string      for (int i = 0; i < 16; i++)         search_sequence [i] = 0;      // provide debug output      if (doDebugMessages)         System.out.println("DEBUG: UPacketbuilder-search ["                            + Integer.toHexString(( int ) id.length) + "]");      // only modify bits if not the first search      if (mState.searchLastDiscrepancy != 0xFF)      {         // set the bits in the added buffer         for (int i = 0; i < 64; i++)         {            // before last discrepancy (go direction based on ID)            if (i < (mState.searchLastDiscrepancy - 1))               bitWrite(search_sequence, (i * 2 + 1), bitRead(id, i));               // at last discrepancy (go 1's direction)            else if (i == (mState.searchLastDiscrepancy - 1))               bitWrite(search_sequence, (i * 2 + 1), true);            // after last discrepancy so leave zeros         }      }      // remember this position      int return_position = totalReturnLength;      // add this sequence      packet.buffer.append(search_sequence);      // set to command mode      setToCommandMode();      // search mode off      packet.buffer.append(( char ) (FUNCTION_SEARCHOFF | uState.uSpeedMode));      // add to the return number of bytes      totalReturnLength   += 16;      packet.returnLength += 16;      return return_position;   }   /**    * Append a search off to set the current speed.    */   public void setSpeed ()   {      // set to command mode      setToCommandMode();      // search mode off and change speed      packet.buffer.append(( char ) (FUNCTION_SEARCHOFF | uState.uSpeedMode));      // no return byte   }   //--------   //-------- U mode commands    //--------   /**    * Set the U state to command mode.    */   public void setToCommandMode ()   {      if (!uState.inCommandMode)      {         // append the command to switch         packet.buffer.append(uState.MODE_COMMAND);         // switch the state         uState.inCommandMode = true;      }   }   /**    * Set the U state to data mode.    */   public void setToDataMode ()   {      if (uState.inCommandMode)      {         // append the command to switch         packet.buffer.append(uState.MODE_DATA);         // switch the state         uState.inCommandMode = false;      }   }   /**    * Append a get parameter to the packet.    *    * @param  parameter  parameter to get    *    * @return the number offset in the return packet to get the    *          result of this operation    */   public int getParameter (int parameter)   {      // set to command mode      setToCommandMode();      // append paramter get      packet.buffer.append(( char ) (CONFIG_MASK | parameter >> 3));      // add to the return number of bytes      totalReturnLength++;      packet.returnLength++;      // check for packet too large      if (packet.buffer.length() > MAX_BYTES_STREAMED)         newPacket();      return (totalReturnLength - 1);   }   /**    * Append a set parameter to the packet.    *    * @param  parameter       parameter to set    * @param  parameterValue  parameter value    *    * @return the number offset in the return packet to get the    *          result of this operation    */   public int setParameter (char parameter, char parameterValue)   {      // set to command mode      setToCommandMode();      // append the paramter set with value      packet.buffer.append(( char ) ((CONFIG_MASK | parameter)                                     | parameterValue));      // add to the return number of bytes      totalReturnLength++;      packet.returnLength++;      // check for packet too large      if (packet.buffer.length() > MAX_BYTES_STREAMED)         newPacket();      return (totalReturnLength - 1);   }   /**    * Append a send command to the packet.  This command does not    * elicit a response byte.    *    * @param  command       command to send    * @param expectResponse    *    * @return the number offset in the return packet to get the    *          result of this operation (if there is one)    */   public int sendCommand (char command, boolean expectResponse)   {      // set to command mode      setToCommandMode();      // append the paramter set with value      packet.buffer.append(command);      // check for response      if (expectResponse)      {         // add to the return number of bytes         totalReturnLength++;         packet.returnLength++;      }      // check for packet too large      if (packet.buffer.length() > MAX_BYTES_STREAMED)         newPacket();      return (totalReturnLength - 1);   }   //--------   //-------- 1-Wire Network result interpretation methods    //--------   /**    * Interpret the block of bytes     *    * @param dataByteResponse      * @param responseOffset    * @param result    * @param offset    * @param len    */   public void interpretDataBytes (char[] dataByteResponse, int responseOffset,                                    byte[] result, int offset, int len)   {      char result_byte;      int temp_offset, i, j;           for (i = 0; i < len; i++)      {         // convert the rest to OneWireIOExceptions         if (bitsOnly)         {            temp_offset = responseOffset + 8 * i;            // provide debug output            if (doDebugMessages)               System.out.println("DEBUG: UPacketbuilder-interpretDataBytes[] responseOffset "                            + responseOffset + " offset " + offset + " lenbuf " + dataByteResponse.length);            // loop through and interpret each bit            result_byte = 0;            for (j = 0; j < 8; j++)            {               result_byte = (char)(result_byte >>> 1);                  if (interpretOneWireBit(dataByteResponse [temp_offset + j]))                  result_byte |= 0x80;            }            result[offset + i] = (byte)(result_byte & 0xFF);         }         else            result[offset + i] = (byte)dataByteResponse[responseOffset + i];      }   }   /**    * Interpret the reset response byte from a U adapter    *    * @param  resetResponse  reset response byte from U    *    * @return the number representing the result of a 1-Wire reset    */   public int interpretOneWireReset (char resetResponse)   {      // make sure the response byte structure is correct      if ((resetResponse & 0xC0) == 0xC0)      {         // retrieve the chip version and program voltage state         uState.revision                =            ( char ) (UAdapterState.CHIP_VERSION_MASK & resetResponse);         uState.programVoltageAvailable = ((UAdapterState.PROGRAM_VOLTAGE_MASK                                            & resetResponse) != 0);         // provide debug output         if (doDebugMessages)            System.out.println("DEBUG: UPacketbuilder-reset response "                               + Integer.toHexString(( int ) resetResponse                                                     & 0x00FF));         // convert the response byte to the OneWire reset result         switch (resetResponse & RESPONSE_RESET_MASK)         {            case RESPONSE_RESET_SHORT :               return DSPortAdapter.RESET_SHORT;            case RESPONSE_RESET_PRESENCE :               // if in long alarm check, record this as a non alarm reset               if (uState.longAlarmCheck)               {                  // check if can give up checking                  if (uState.lastAlarmCount++ > uState.MAX_ALARM_COUNT)                     uState.longAlarmCheck = false;               }               return DSPortAdapter.RESET_PRESENCE;            case RESPONSE_RESET_ALARM :               // alarm presense so go into DS2480 long alarm check mode               uState.longAlarmCheck = true;               uState.lastAlarmCount = 0;               return DSPortAdapter.RESET_ALARM;            case RESPONSE_RESET_NOPRESENCE :            default :               return DSPortAdapter.RESET_NOPRESENCE;         }      }      else         return DSPortAdapter.RESET_NOPRESENCE;   }   /**    * Interpret the bit response byte from a U adapter    *    * @param  bitResponse  bit response byte from U    *    * @return boolean representing the result of a 1-Wire bit operation    */   public boolean interpretOneWireBit (char bitResponse)   {      // interpret the bit      if ((bitResponse & RESPONSE_BIT_MASK) == RESPONSE_BIT_ONE)         return true;      else         return false;   }   /**    * Interpret the search response and set the 1-Wire state accordingly.    *    * @param  bitResponse  bit response byte from U    *    * @param mState    * @param searchResponse    * @param responseOffset    *    * @return boolean return is true if a valid ID was found when    *                 interpreting the search results    */   public boolean interpretSearch (OneWireState mState,                                   char[] searchResponse, int responseOffset)   {      char[] temp_id = new char [8];      // change byte offset to bit offset      int bit_offset = responseOffset * 8;      // set the temp Last Descrep to none      int temp_last_descrepancy        = 0xFF;      int temp_last_family_descrepancy = 0;      // interpret the search response sequence      for (int i = 0; i < 64; i++)      {         // get the SerialNum bit         bitWrite(temp_id, i,                  bitRead(searchResponse, (i * 2) + 1 + bit_offset));         // check LastDiscrepancy         if (bitRead(searchResponse, i * 2 + bit_offset)                 &&!bitRead(searchResponse, i * 2 + 1 + bit_offset))         {            temp_last_descrepancy = i + 1;            // check LastFamilyDiscrepancy            if (i < 8)               temp_last_family_descrepancy = i + 1;         }      }      // check      byte[] id = new byte [8];      for (int i = 0; i < 8; i++)         id [i] = ( byte ) temp_id [i];      // check results       if ((!Address.isValid(id)) || (temp_last_descrepancy == 63)              || (temp_id [0] == 0))         return false;         // successful search      else      {         // check for lastone         if ((temp_last_descrepancy == mState.searchLastDiscrepancy)                 || (temp_last_descrepancy == 0xFF))            mState.searchLastDevice = true;         // copy the ID number to the buffer         for (int i = 0; i < 8; i++)            mState.ID [i] = ( byte ) temp_id [i];         // set the count         mState.searchLastDiscrepancy       = temp_last_descrepancy;         mState.searchFamilyLastDiscrepancy = temp_last_family_descrepancy;         return true;      }   }   /**    * Interpret the data response byte from a primed byte operation    *    * @param primedDataResponse    * @param responseOffset    *    * @return the byte representing the result of a 1-Wire data byte    */   public byte interpretPrimedByte (char[] primedDataResponse,                                    int responseOffset)   {      char result_byte = 0;      // loop through and interpret each bit      for (int i = 0; i < 8; i++)      {         result_byte = (char)(result_byte >>> 1);         if (interpretOneWireBit(primedDataResponse [responseOffset + i]))            result_byte |= 0x80;      }      return (byte)(result_byte & 0xFF);   }   //--------   //-------- Misc Utility methods    //--------   /**    * Request the maximum rate to do an operation    */   public static int getDesiredBaud (int operation, int owSpeed, int maxBaud)   {      int baud = 9600;      switch (operation)      {         case OPERATION_BYTE :            if (owSpeed == DSPortAdapter.SPEED_OVERDRIVE)               baud = 115200;            else               baud = 9600;            break;         case OPERATION_SEARCH :            if (owSpeed == DSPortAdapter.SPEED_OVERDRIVE)               baud = 57600;            else               baud = 9600;            break;      }      if (baud > maxBaud)         baud = maxBaud;      return baud;   }   /**    * Bit utility to read a bit in the provided array of chars.    *    * @param  bitBuffer array of chars where the bit to read is located    * @param  address   bit location to read (LSBit of first Byte in bitBuffer    *                    is postion 0)    *    * @return the boolean value of the bit position    */   public boolean bitRead (char[] bitBuffer, int address)   {      int byte_number, bit_number;      byte_number = (address / 8);      bit_number  = address - (byte_number * 8);      return ((( char ) ((bitBuffer [byte_number] >> bit_number) & 0x01))              == 0x01);   }   /**    * Bit utility to write a bit in the provided array of chars.    *    * @param  bitBuffer array of chars where the bit to write is located    * @param  address   bit location to write (LSBit of first Byte in bitBuffer    *                    is postion 0)    * @param  newBitState new bit state    */   public void bitWrite (char[] bitBuffer, int address, boolean newBitState)   {      int byte_number, bit_number;      byte_number = (address / 8);      bit_number  = address - (byte_number * 8);      if (newBitState)         bitBuffer [byte_number] |= ( char ) (0x01 << bit_number);      else         bitBuffer [byte_number] &= ( char ) (~(0x01 << bit_number));   }}

⌨️ 快捷键说明

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