📄 jibcomm.java
字号:
else if (owms_code==CE_RESPONSE_INCOMPLETE) { //means the response message isn't ready yet //this fixes the very common "No header in output buffer" problem if (doDebugMessages) System.out.println("Clayton response = 12"); run(0); } else if ((check_status[1]==0) && (owms_code==CE_JAVAVM_INCOMPLETE)) { //this fixes another 'No header in output buffer' problem where there is a lot of //data coming out, so the button wants to transmit but doesn't have any data ready to go, //so we let it run a little bit longer so it can get all the data to its output buffer if (doDebugMessages) System.out.println("Not quite ready yet...run a little bit more"); run(0); my_min_read_runtime++; } else { // Check to see if send or receive called, procceed acordingly. if (dir == SEND) { // Check the number of free bytes in the input buffer if (check_status [0] < HEADER_SIZE) throw new OneWireIOException("JibComm Error - No room in header input buffer."); else break; // If all is good then break out of loop. } else { // Check number of free bytes in output buffer. if (check_status [1] == 0) { throw new OneWireIOException("JibComm Error - No header in output buffer."); } else if (check_status [1] != HEADER_SIZE) throw new OneWireIOException("JibComm Error - Bad header in output buffer."); else break; // If all is good then break out of loop. } } } // if(POR) } // while(true) System.arraycopy(check_status, 0, status, start, 4); } // checkStatus /** * Sets status register of this Java iButton. * * @param runTime a <code>4</code> bit value <code>(0 -15)</code> * that represents the expected run time of the device * Actual run time is calculated as followed: * <code><pre> * runTime * 250 + 62.5 mS <BR> * Therefore, 0 -> 0 * 250 + 62.5 = 62.5 mS * 1 -> 1 * 250 + 62.5 = 312.5 mS </pre></code> * and so on. * * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * * @see #checkStatus * @see #getStatus */ public synchronized void setStatus (int runTime) throws OneWireException, OneWireIOException { if (adapter.getSpeed()!=DSPortAdapter.SPEED_OVERDRIVE) { if (doDebugMessages) System.out.println("FIXING SPEED!"); container.doSpeed(); } if (runTime > MAX_RUNTIME) runTime = MAX_RUNTIME; // Set up the command block set_status_command_buffer [0] = WRITE_STATUS_COMMAND; set_status_command_buffer [1] = ( byte ) (runTime & 0x0F); set_status_command_buffer [2] = ( byte ) 0xFF; // Fill with 0xFF's to read the CRC. set_status_command_buffer [3] = ( byte ) 0xFF; // Set up the release code block set_status_command_buffer [4] = ( byte ) 0x7F; // Release byte required by the iButton. set_status_command_buffer [5] = ( byte ) 0x51; // Rest of the Release byte. //this is a trick, we have to send the buffer as the release buffer //so that sendCommand knows we want to use pull-up sendCommand(null, set_status_command_buffer, false, 0); return; } int mycksum = 0; /** * Sets header to be written to this Java iButton. * * @param header byte array with the header information * * @throws IllegalArgumentException Invalid header length * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * * @see #checkStatus * @see #getHeader */ public synchronized void setHeader (byte[] header) throws OneWireException, OneWireIOException, IllegalArgumentException { if (header.length != HEADER_SIZE) throw new IllegalArgumentException("The header must be of length " + HEADER_SIZE); if (doDebugMessages) { mycksum = 0; for (int i=0;i<header.length;i++) { mycksum += (header[i] & 0x0ff); } } // Set up the command block // set to command length + CRC + HEADER_SIZE. set_header_buffer [0] = WRITE_IO_BUFFER_COMMAND; set_header_buffer [1] = ( byte ) HEADER_SIZE; System.arraycopy(header, 0, set_header_buffer, 2, HEADER_SIZE); set_header_buffer [10] = ( byte ) 0xFF; // Fill with 0xFF's to read the CRC. set_header_buffer [11] = ( byte ) 0xFF; // Set up the release code block set_header_buffer [4 + HEADER_SIZE] = ( byte ) 0xB3; // Release byte required by the iButton. set_header_buffer [4 + HEADER_SIZE + 1] = ( byte ) 0x9D; // Rest of the Release byte. //this is a trick, we have to send the buffer as the release buffer //so that sendCommand knows we want to use pull-up sendCommand(null, set_header_buffer, false, 0); return; } /** * Gets header from this Java iButton. * * @return header read from this Java iButton. * * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * * @see #checkStatus * @see #setHeader */ public synchronized void getHeader (byte[] header, int start) throws OneWireException, OneWireIOException { // Set up the command block get_header_buffer [0] = READ_IO_BUFFER_COMMAND; get_header_buffer [1] = ( byte ) HEADER_SIZE; System.arraycopy(ffBlock, 0, get_header_buffer, 2, 10); // Set up the release code block get_header_buffer [12] = ( byte ) 0x4C; // Release byte required by the iButton. get_header_buffer [13] = ( byte ) 0x62; // Rest of the Release byte. //this is a trick, we pass the buffer as the 'release buffer' to make //sure sendCommand does the right thing sendCommand(null, get_header_buffer, false, 0); System.arraycopy(get_header_buffer, 2, header, start, HEADER_SIZE); } /** * Sets data to be written to this Java iButton. * * @param data data to be written to this Java iButton * * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * * @see #checkStatus * @see #getData */ public void setData (byte[] data) throws OneWireException, OneWireIOException { // Set up the command block, we can't make this a buffer // without a good deal more work byte[] commandBuffer = new byte [4 + data.length]; commandBuffer [0] = WRITE_IPR_COMMAND; commandBuffer [1] = ( byte ) data.length; if (doDebugMessages) { System.out.println("Setting data length "+data.length + "\r\n" + toHexString(data)); for (int i=0;i<data.length;i++) { mycksum += (data[i] & 0x0ff); } System.out.println("My checksum is "+Integer.toHexString(mycksum)); } System.arraycopy(data, 0, commandBuffer, 2, data.length); commandBuffer [data.length + 2] = ( byte ) 0xFF; // Fill with 0xFF's to read the CRC. commandBuffer [data.length + 3] = ( byte ) 0xFF; sendCommand(commandBuffer, null, false, 0); return; } byte[] hex = "0123456789abcdef".getBytes(); String toHexString(byte[] b) { byte[] c = new byte[b.length * 3]; for (int i=0;i<b.length;i++) { c[i*3] = hex[(b[i] >> 4) & 0x0f]; c[i*3+1] = hex[(b[i]) & 0x0f]; c[i*3+2] = (byte)' '; } return new String(c); } /** * Gets data from this Java iButton. * * @param length expected number of bytes of data to be read from the IPR * * @return data from this Java iButton. * * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * * @see #checkStatus * @see #setData */ public byte[] getData (int length) throws OneWireException, OneWireIOException { // Set up the command block byte[] commandBuffer = new byte [length + 4]; commandBuffer [0] = READ_IPR_COMMAND; commandBuffer [1] = ( byte ) (length & 0xFF); // Fill the rest with 0xFF. System.arraycopy(ffBlock, 0, commandBuffer, 2, length + 2); sendCommand(commandBuffer, null, false, 0); byte[] data = new byte [length]; System.arraycopy(commandBuffer, 2, data, 0, length); return data; } /** * Runs the Micro in this Java iButton. * * @param runTime a <code>4</code> bit value <code>(0 -15)</code> * that represents the expected run time of the device * Actual run time is calculated as followed: * <code><pre> * runTime * 250 + 62.5 mS <BR> * Therefore, 0 -> 0 * 250 + 62.5 = 62.5 mS * 1 -> 1 * 250 + 62.5 = 312.5 mS </pre></code> * and so on. * * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * */ public synchronized void run (int runTime) throws OneWireException, OneWireIOException { long sleepTime = (runTime * RUNTIME_MULTIPLIER) + MIN_RUNTIME_IN_MILLIS; // Set up the release code block run_release_buffer [0] = RUN_MICRO_COMMAND; run_release_buffer [1] = ( byte ) 0x73; // Release code for runing micro. run_release_buffer [2] = ( byte ) 0x5D; sendCommand(null, run_release_buffer, true, sleepTime); return; } /** * Interrupts the Micro in this Java iButton. * * @param runTime a <code>4</code> bit value <code>(0 -15)</code> * that represents the expected run time of the device * Actual run time is calculated as followed: * <code><pre> * runTime * 250 + 62.5 mS <BR> * Therefore, 0 -> 0 * 250 + 62.5 = 62.5 mS * 1 -> 1 * 250 + 62.5 = 312.5 mS </pre></code> * and so on. * * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * */ public synchronized void interrupt (int runTime) throws OneWireException, OneWireIOException { long sleepTime = (runTime * RUNTIME_MULTIPLIER) + MIN_RUNTIME_IN_MILLIS; // Set up the release code block interrupt_release_buffer [0] = INTERRUPT_MICRO_COMMAND; interrupt_release_buffer [1] = ( byte ) 0x43; // Release code for intterupt function. interrupt_release_buffer [2] = ( byte ) 0x6D; // Release code. sendCommand(null, interrupt_release_buffer, true, sleepTime); return; } /** * Resets the Micro in this Java iButton. * * @throws OneWireException Part could not be found [ fatal ] * @throws OneWireIOException Data wasn't transferred properly [ recoverable ] * */ public void reset () throws OneWireException, OneWireIOException { // Set up the release code block byte[] releaseBuffer = new byte [3]; releaseBuffer [0] = RESET_MICRO_COMMAND; releaseBuffer [1] = ( byte ) 0xBC; // Release code for reset function. releaseBuffer [2] = ( byte ) 0x92; // Rest of the Release byte. sendCommand(null, releaseBuffer, false, 0); return; } /** * Sends command to this Java iButton. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -