📄 userialadapter.java
字号:
// set new level state owState.oneWireLevel = LEVEL_POWER_DELIVERY; // extract the result byte dataBlock [off] = uBuild.interpretPrimedByte(ret_data, data_offset); } else { data_offset = uBuild.dataBytes(dataBlock, off, len); // send and receive ret_data = uTransaction(uBuild); // extract the result byte(s) uBuild.interpretDataBytes(ret_data, data_offset, dataBlock, off, len); } } else throw new OneWireIOException("Error communicating with adapter"); } catch(IOException ioe) { throw new OneWireIOException(ioe.toString()); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Sends a Reset to the 1-Wire Network. * * @return the result of the reset. Potential results are: * <ul> * <li> 0 (RESET_NOPRESENCE) no devices present on the 1-Wire Network. * <li> 1 (RESET_PRESENCE) normal presence pulse detected on the 1-Wire * Network indicating there is a device present. * <li> 2 (RESET_ALARM) alarming presence pulse detected on the 1-Wire * Network indicating there is a device present and it is in the * alarm condition. This is only provided by the DS1994/DS2404 * devices. * <li> 3 (RESET_SHORT) inticates 1-Wire appears shorted. This can be * transient conditions in a 1-Wire Network. Not all adapter types * can detect this condition. * </ul> * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public int reset () throws OneWireIOException, OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); // make sure adapter is present if (uAdapterPresent()) { // check for pending power conditions if (owState.oneWireLevel != LEVEL_NORMAL) setPowerNormal(); // flush out the com buffer serial.flush(); // build a message to read the baud rate from the U brick uBuild.restart(); int reset_offset = uBuild.oneWireReset(); // send and receive char[] result_array = uTransaction(uBuild); // check the result if (result_array.length == (reset_offset + 1)) return uBuild.interpretOneWireReset( result_array [reset_offset]); else throw new OneWireIOException( "USerialAdapter-reset: no return byte form 1-Wire reset"); } else throw new OneWireIOException("Error communicating with adapter"); } catch(IOException ioe) { throw new OneWireIOException(ioe.toString()); } finally { // release local exclusive use of port endLocalExclusive(); } } //-------- //-------- OneWire power methods //-------- /** * Sets the duration to supply power to the 1-Wire Network. * This method takes a time parameter that indicates the program * pulse length when the method startPowerDelivery().<p> * * Note: to avoid getting an exception, * use the canDeliverPower() and canDeliverSmartPower() * method to check it's availability. <p> * * @param timeFactor * <ul> * <li> 0 (DELIVERY_HALF_SECOND) provide power for 1/2 second. * <li> 1 (DELIVERY_ONE_SECOND) provide power for 1 second. * <li> 2 (DELIVERY_TWO_SECONDS) provide power for 2 seconds. * <li> 3 (DELIVERY_FOUR_SECONDS) provide power for 4 seconds. * <li> 4 (DELIVERY_SMART_DONE) provide power until the * the device is no longer drawing significant power. * <li> 5 (DELIVERY_INFINITE) provide power until the * setBusNormal() method is called. * </ul> * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public void setPowerDuration (int timeFactor) throws OneWireIOException, OneWireException { if (timeFactor != DELIVERY_INFINITE) throw new OneWireException( "USerialAdapter-setPowerDuration, does not support this duration, infinite only"); else owState.levelTimeFactor = DELIVERY_INFINITE; } /** * Sets the 1-Wire Network voltage to supply power to an iButton device. * This method takes a time parameter that indicates whether the * power delivery should be done immediately, or after certain * conditions have been met. <p> * * Note: to avoid getting an exception, * use the canDeliverPower() and canDeliverSmartPower() * method to check it's availability. <p> * * @param changeCondition * <ul> * <li> 0 (CONDITION_NOW) operation should occur immediately. * <li> 1 (CONDITION_AFTER_BIT) operation should be pending * execution immediately after the next bit is sent. * <li> 2 (CONDITION_AFTER_BYTE) operation should be pending * execution immediately after next byte is sent. * </ul> * * @return <code>true</code> if the voltage change was successful, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public boolean startPowerDelivery (int changeCondition) throws OneWireIOException, OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); if (changeCondition == CONDITION_AFTER_BIT) { owState.levelChangeOnNextBit = true; owState.primedLevelValue = LEVEL_POWER_DELIVERY; } else if (changeCondition == CONDITION_AFTER_BYTE) { owState.levelChangeOnNextByte = true; owState.primedLevelValue = LEVEL_POWER_DELIVERY; } else if (changeCondition == CONDITION_NOW) { // make sure adapter is present if (uAdapterPresent()) { // check for pending power conditions if (owState.oneWireLevel != LEVEL_NORMAL) setPowerNormal(); // flush out the com buffer serial.flush(); // build a message to read the baud rate from the U brick uBuild.restart(); // set the SPUD time value int set_SPUD_offset = uBuild.setParameter(UParameterSettings.PARAMETER_5VPULSE, UParameterSettings.TIME5V_infinite); // add the command to begin the pulse uBuild.sendCommand(UPacketBuilder.FUNCTION_5VPULSE_NOW, false); // send and receive char[] result_array = uTransaction(uBuild); // check the result if (result_array.length == (set_SPUD_offset + 1)) { owState.oneWireLevel = LEVEL_POWER_DELIVERY; return true; } } else throw new OneWireIOException( "Error communicating with adapter"); } else throw new OneWireException("Invalid power delivery condition"); return false; } catch(IOException ioe) { throw new OneWireIOException(ioe.toString()); } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Sets the duration for providing a program pulse on the * 1-Wire Network. * This method takes a time parameter that indicates the program * pulse length when the method startProgramPulse().<p> * * Note: to avoid getting an exception, * use the canDeliverPower() method to check it's * availability. <p> * * @param timeFactor * <ul> * <li> 6 (DELIVERY_EPROM) provide program pulse for 480 microseconds * <li> 5 (DELIVERY_INFINITE) provide power until the * setBusNormal() method is called. * </ul> * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter */ public void setProgramPulseDuration (int timeFactor) throws OneWireIOException, OneWireException { if (timeFactor != DELIVERY_EPROM) throw new OneWireException( "Only support EPROM length program pulse duration"); } /** * Sets the 1-Wire Network voltage to eprom programming level. * This method takes a time parameter that indicates whether the * power delivery should be done immediately, or after certain * conditions have been met. <p> * * Note: to avoid getting an exception, * use the canProgram() method to check it's * availability. <p> * * @param changeCondition * <ul> * <li> 0 (CONDITION_NOW) operation should occur immediately. * <li> 1 (CONDITION_AFTER_BIT) operation should be pending * execution immediately after the next bit is sent. * <li> 2 (CONDITION_AFTER_BYTE) operation should be pending * execution immediately after next byte is sent. * </ul> * * @return <code>true</code> if the voltage change was successful, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter * or the adapter does not support this operation */ public boolean startProgramPulse (int changeCondition) throws OneWireIOException, OneWireException { // check if adapter supports program if (!uState.programVoltageAvailable) throw new OneWireException( "USerialAdapter: startProgramPulse, program voltage not available"); // check if correct change condition if (changeCondition != CONDITION_NOW) throw new OneWireException( "USerialAdapter: startProgramPulse, CONDITION_NOW only currently supported"); try { // acquire exclusive use of the port beginLocalExclusive(); // build a message to read the baud rate from the U brick uBuild.restart(); //int set_SPUD_offset = uBuild.setParameter(UParameterSettings.PARAMETER_12VPULSE, UParameterSettings.TIME12V_512us); // add the command to begin the pulse //int pulse_offset = uBuild.sendCommand(UPacketBuilder.FUNCTION_12VPULSE_NOW, true); // send the command //char[] result_array = uTransaction(uBuild); // check the result ?? return true; } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Sets the 1-Wire Network voltage to 0 volts. This method is used * rob all 1-Wire Network devices of parasite power delivery to force * them into a hard reset. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter * or the adapter does not support this operation */ public void startBreak () throws OneWireIOException, OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); // power down the 2480 (dropping the 1-Wire) serial.setDTR(false); serial.setRTS(false); // wait for power to drop sleep(200); // set the level state owState.oneWireLevel = LEVEL_BREAK; } finally { // release local exclusive use of port endLocalExclusive(); } } /** * Sets the 1-Wire Network voltage to normal level. This method is used * to disable 1-Wire conditions created by startPowerDelivery and * startProgramPulse. This method will automatically be called if * a communication method is called while an outstanding power * command is taking place. * * @throws OneWireIOException on a 1-Wire communication error * @throws OneWireException on a setup error with the 1-Wire adapter * or the adapter does not support this operation */ public void setPowerNormal () throws OneWireIOException, OneWireException { try { // acquire exclusive use of the port beginLocalExclusive(); if (owState.oneWireLevel == LEVEL_POWER_DELIVERY) { // make sure adapter is present if (uAdapterPresent()) { // flush out the com buffer serial.flush(); // build a message to read the baud rate from the U brick uBuild.restart(); //\\//\\//\\//\\//\\//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -