/// <summary> /// Append a search to the packet. Assume that any reset and search /// command have already been appended. This will add only the search /// itself. /// </summary> /// <param name="mState"> OneWire state /// </param> /// <returns> the number offset in the return packet to get the /// result of this operation </returns> public virtual int search(OneWireState mState) { // create the search sequence character array byte[] search_sequence = new byte[8]; // get a copy of the current ID for (int i = 0; i < 8; i++) { search_sequence[i] = mState.ID[i]; } // only modify bits if not the first search if (mState.searchLastDiscrepancy != 0xFF) { if (mState.searchLastDiscrepancy > 0) { bitWrite(search_sequence, mState.searchLastDiscrepancy - 1, true); } for (int i = mState.searchLastDiscrepancy; i < 64; i++) { bitWrite(search_sequence, i, false); } } // add this sequence packet.writer.Write(search_sequence); return(totalReturnLength); }
//-------- //-------- Constructors //-------- /// <summary> /// Construct the state of the USB interface with the defaults /// </summary> public UsbAdapterState(OneWireState newOneWireState) { syncObject = new object(); // get a pointer to the OneWire state object oneWireState = newOneWireState; // Power-On defaults StrongPullup = false; DynamicSpeedChange = false; BusCommSpeed = BUSCOMSPEED_REGULAR; ReqBusCommSpeed = -1; StrongPullupDuration = GetPullUpDurationByte(.512); PulldownSlewRateControl = SLEWRATE_0p83Vus; Write1LowTime = FLEXWRITE1LOWTIME_8us; DSOW0RecoveryTime = DSOW0RECOVERYTIME_18us; //// create the three speed logical parameter settings //uParameters = new UParameterSettings [3]; //uParameters [0] = new UParameterSettings(); //uParameters [1] = new UParameterSettings(); //uParameters [2] = new UParameterSettings(); //// adjust flex time //uParameters [DSPortAdapter.SPEED_FLEX].pullDownSlewRate = UParameterSettings.SLEWRATE_0p83Vus; //uParameters [DSPortAdapter.SPEED_FLEX].write1LowTime = UParameterSettings.WRITE1TIME_12us; //uParameters [DSPortAdapter.SPEED_FLEX].sampleOffsetTime = UParameterSettings.SAMPLEOFFSET_TIME_10us; }
/// <summary> /// UsbAdapterIo Constructor called by UsbAdapter class /// </summary> /// <param name="usbDevice"></param> /// <param name="usbstate"></param> /// <param name="owstate"></param> internal UsbAdapterIo(UsbDevice usbdevice, string deviceid, UsbAdapterState usbstate, OneWireState owstate) { usbDevice = usbdevice; deviceId = deviceid; usbState = usbstate; owState = owstate; syncObject = new object(); ReadResultWait = new AutoResetEvent(false); }
/// <summary> /// Interpret the search response and set the 1-Wire state accordingly. /// </summary> /// <param name="mState"> </param> /// <param name="searchResponse"> </param> /// <param name="responseOffset"> /// </param> /// <returns> bool return is true if a valid ID was found when /// interpreting the search results </returns> public virtual bool interpretSearch(OneWireState mState, byte[] searchResponse, int responseOffset) { bool rt = false; // check byte[] id = new byte[8]; for (int i = 0; i < 8; i++) { id[i] = searchResponse[i]; } // set the temp Last Descrep to none int temp_last_descrepancy = 0xFF; if (Address.isValid(id) && id[0] != 0) { if (searchResponse.Length == 8) { mState.searchLastDevice = true; } else { for (int i = 0; i < 64; i++) { // if descrepancy if (bitRead(searchResponse, 64 + i) && (bitRead(searchResponse, i) == false)) { temp_last_descrepancy = i + 1; } } } // copy the ID number to the buffer for (int i = 0; i < 8; i++) { mState.ID[i] = id[i]; } rt = true; } else { for (int i = 0; i < 8; i++) { mState.ID[i] = 0; } } mState.searchLastDiscrepancy = temp_last_descrepancy; return(rt); }
/// <summary> /// Interpret the search response and set the 1-Wire state accordingly. /// </summary> /// <param name="mState"> </param> /// <param name="searchResponse"> </param> /// <param name="responseOffset"> /// </param> /// <returns> bool return is true if a valid ID was found when /// interpreting the search results </returns> public virtual bool interpretSearch(OneWireState mState, byte[] searchResponse, int responseOffset) { byte[] temp_id = new byte[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] = 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] = temp_id[i]; } // set the count mState.searchLastDiscrepancy = temp_last_descrepancy; mState.searchFamilyLastDiscrepancy = temp_last_family_descrepancy; return(true); } }
/// <summary> /// Append a search to the packet. Assume that any reset and search /// command have already been appended. This will add only the search /// itself. /// </summary> /// <param name="mState"> OneWire state /// </param> /// <returns> the number offset in the return packet to get the /// result of this operation </returns> public virtual int search(OneWireState mState) { // set to command mode setToCommandMode(); // search mode on packet.writer.Write((byte)(FUNCTION_SEARCHON | uState.uSpeedMode)); // set to data mode setToDataMode(); // create the search sequence character array byte[] search_sequence = new byte[16]; // get a copy of the current ID byte[] id = new byte[8]; for (int i = 0; i < 8; i++) { id[i] = mState.ID[i]; } // clear the string for (int i = 0; i < 16; i++) { search_sequence[i] = 0; } // provide debug output if (doDebugMessages) { Debug.WriteLine("DEBUG: UPacketbuilder-search, [{0:X02}]", 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.writer.Write(search_sequence); // set to command mode setToCommandMode(); // search mode off packet.writer.Write((byte)(FUNCTION_SEARCHOFF | uState.uSpeedMode)); // add to the return number of bytes totalReturnLength += 16; packet.returnLength += 16; return(return_position); }