/// <summary> /// Initialize the sensor. This step will perform a reset of the 1-wire bus. /// It will check for existence of a 1-wire device. If no address was provided, then the /// 1-wire bus will be searched and the first device that matches the family code will be latched on to. /// Developer should check for successful initialization by checking the Address property. /// It should have valid 64-bit value /// </summary> public override void Initialize() { bool found = true; if (Address == null) //search for a device with the required family code { found = false; if (_oneWire.FindFirstDevice(true, false)) //Current nF firmware works if reset if performed before a find operation { do { if (_oneWire.SerialNumber[0] == FAMILY_CODE) { //found the device Address = new byte[_oneWire.SerialNumber.Length]; Array.Copy(_oneWire.SerialNumber, Address, _oneWire.SerialNumber.Length); found = true; break; } } while (_oneWire.FindNextDevice(true, false));//keep searching until we get one } } if (!found) { throw new Exception(); } }
/// <summary> /// Initialize the sensor. This step will perform a reset of the 1-wire bus. /// It will check for existence of a 1-wire device. If no address was provided, then the /// 1-wire bus will be searched and the first device that matches the family code will be latched on to. /// Developer should check for successful initialization by checking the value returned. /// It must be bigger than 0. /// If in Multidrop mode will keep seaching until find last device, saving all in AddressNet array. /// </summary> public bool Initialize() { Found = 0; //ArrayList allDevices; ArrayList allDevices = new ArrayList(); _oneWire.TouchReset(); if (Address == null) //search for a device with the required family code { //found the device if (Multidrop) { if (_oneWire.FindFirstDevice(false, searchMode)) { do { if (_oneWire.SerialNumber[0] == FAMILY_CODE) { _oneWire.TouchReset(); Address = new byte[_oneWire.SerialNumber.Length]; Array.Copy(_oneWire.SerialNumber, Address, _oneWire.SerialNumber.Length); Found++; allDevices.Add(Address); //if (Found == 6) { break; } //Temp fix during test endless loop } } while (_oneWire.FindNextDevice(false, searchMode));//keep searching until we get one } if (Found > 0) { AddressNet = new byte[Found][]; int i = 0; foreach (byte[] device in allDevices) { AddressNet[i] = new byte[device.Length]; Array.Copy(device, AddressNet[i], device.Length); i++; } allDevices = null; } } else { if (_oneWire.FindFirstDevice(true, searchMode)) { do { if (_oneWire.SerialNumber[0] == FAMILY_CODE) { Address = new byte[_oneWire.SerialNumber.Length]; Array.Copy(_oneWire.SerialNumber, Address, _oneWire.SerialNumber.Length); Found = 1; break; } } while (_oneWire.FindNextDevice(true, searchMode));//keep searching until we get one } } } if (Found > 0) { return(true); } ; return(false); }
public static void Main() { // get controller for OneWire OneWireController oneWire = new OneWireController(); //////////////////////////////////////////////////////////////////// // Sample code to read serial number for a devices present in bus // // With this approach, three steps are needed: // - Reset 1-Wire bus // - Transmit Read ROM command // - Read 8 bytes (serial number) //////////////////////////////////////////////////////////////////// byte[] state = new byte[13]; // check for devices present with a bus reset if (oneWire.TouchReset()) { Debug.WriteLine("Device present"); // tx READ ROM var res = oneWire.WriteByte(0x33); // now read 8 byte for SN // and output serial number nicelly formated in hexa for (int i = 0; i < 8; i++) { // read byte state[i] = oneWire.TouchByte(0xFF); // output byte Debug.Write(state[i].ToString("X2")); } Debug.WriteLine(""); } else { Debug.WriteLine("!! No devices found !!"); } //////////////////////////////////////////////////////////////////// // Sample code to read serial number for a devices present in bus // // Just call FindFirstDevice and it will fill OneWire SerialNumber // buffer with the 1-Wire device serial number present in bus //////////////////////////////////////////////////////////////////// if (oneWire.FindFirstDevice(true, false)) { Debug.WriteLine("device found:"); // output serial number nicelly formated in hexa for (int i = 0; i < 8; i++) { Debug.Write(oneWire.SerialNumber[i].ToString("X2")); } Debug.WriteLine(""); } else { Debug.WriteLine("!! No devices found !!"); } //////////////////////////////////////////////////////////////////// // Sample code to read serial number for all devices present in bus // // Just call FindAllDevices and it will return an array list with // all 1-Wire devices serial numbers present in bus //////////////////////////////////////////////////////////////////// ArrayList snList = oneWire.FindAllDevices(); if (snList.Count > 0) { Debug.WriteLine(String.Format("{0} devices found", snList.Count)); foreach (byte[] sn in snList) { // output serial number nicelly formated in hexa for (int i = 0; i < 8; i++) { Debug.Write(sn[i].ToString("X2")); } Debug.WriteLine(""); } } else { Debug.WriteLine("!! No devices found !!"); } Thread.Sleep(Timeout.Infinite); }