/// <summary> /// The readSensor method returns a relative humidity reading /// in %RH /// /// @param--none. /// </summary> /// <returns> String humidity in %RH </returns> public virtual string readSensor() { HumidityContainer hc = DeviceContainer as HumidityContainer; // read the device first to get the state byte[] state = hc.readDevice(); // convert humidity hc.doHumidityConvert(state); // construct the return string string return_string = (int)roundDouble(hc.getHumidity(state)) + "%"; if (hc.Relative) { return_string += "RH"; } return(return_string); }
public static void Main1(string[] args) { try { List <OneWireContainer> humidity_devices = new List <OneWireContainer>(1); // get the default adapter and show header DSPortAdapter adapter = OneWireAccessProvider.DefaultAdapter; Debug.WriteLine(""); Debug.WriteLine("Adapter: " + adapter.AdapterName + " Port: " + adapter.PortName); Debug.WriteLine(""); Debug.WriteLine("Devices Found:"); Debug.WriteLine("--------------"); // get exclusive use of adapter/port adapter.beginExclusive(true); // find all devices adapter.setSearchAllDevices(); adapter.targetAllFamilies(); if (adapter.canFlex()) { adapter.Speed = DSPortAdapter.SPEED_FLEX; } // enumerate through all the 1-Wire devices found to find // containers that implement HumidityContainer for (IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();) { OneWireContainer owd = (OneWireContainer)owd_enum.Current; Debug.Write(owd.AddressAsString); if (owd is HumidityContainer) { humidity_devices.Add(owd); Debug.WriteLine(" Humidity Sensor, Relative=" + ((HumidityContainer)owd).Relative); } else { Debug.WriteLine(" NOT Humidity Sensor"); } } if (humidity_devices.Count == 0) { throw new Exception("No Humitiy devices found!"); } // display device found Debug.WriteLine(""); Debug.WriteLine("Hit ENTER to stop reading humidity"); Stream stream = loadResourceFile("HumidityTest.input.txt"); StreamReader keyboard = new StreamReader(stream); string str = null; // loop and read RH or ENTER to quit for (;;) { // read each RH temp found for (int i = 0; i < humidity_devices.Count; i++) { HumidityContainer hc = (HumidityContainer)humidity_devices[i]; byte[] state = hc.readDevice(); hc.doHumidityConvert(state); Debug.WriteLine(((OneWireContainer)hc).AddressAsString + " humidity = " + hc.getHumidity(state) + "%"); } // check for ENTER str = keyboard.ReadLine(); if (str == null) { break; } } // end exclusive use of adapter adapter.endExclusive(); // free port used by adapter adapter.freePort(); } catch (Exception e) { Debug.WriteLine(e); } return; }