// read temperature from device internal static void getTemperature(TemperatureContainer tc, int trial) { // get the current resolution and other settings of the device byte[] state = tc.readDevice(); double lastTemp; while (trial-- > 0) { // perform a temperature conversion tc.doTemperatureConvert(state); // read the result of the conversion state = tc.readDevice(); // extract the result out of state lastTemp = tc.getTemperature(state); if (tempMode == FAHRENHEIT) { lastTemp = convertToFahrenheit(lastTemp); } // show results up to 2 decimal places Debug.WriteLine(" Temperature = " + ((int)(lastTemp * 100)) / 100.0 + tempUnit); } }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> public static void Main1(string[] args) { byte[] state = null; double alarmLow; double alarmHigh; bool alarming; OneWireContainer owc = null; TemperatureContainer tc = null; Stream stream = loadResourceFile("TemperatureContainerDemo.input.txt"); dis = new StreamReader(stream); // find and initialize the first OneWireContainer with // TemperatureContainerDemo interface owc = initContainer(); if (!(owc is TemperatureContainer)) { cleanup(); Debug.WriteLine("*************************************************************************"); Debug.WriteLine("No TemperatureContainerDemo found. Exit program."); Debug.WriteLine(""); return; } else { tc = (TemperatureContainer)owc; } initMenu(); int curMenuChoice = 0; while (true) { curMenuChoice = getMenuChoice(hashMainMenu, mainMenuItemCount); try { switch (curMenuChoice) { case 0: // Read Temperature Once getTemperature(tc, 1); break; case 1: // Read Temperature Multiple Time Debug.Write("Please enter number of times: "); int trial = (int)Number; getTemperature(tc, trial); break; case 2: // Read High and Low Alarms if (!tc.hasTemperatureAlarms()) { Debug.WriteLine("Temperature alarms not supported"); } else { state = tc.readDevice(); alarmLow = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, state); alarmHigh = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, state); if (tempMode == FAHRENHEIT) { alarmHigh = convertToFahrenheit(alarmHigh); alarmLow = convertToFahrenheit(alarmLow); } Debug.WriteLine(" Alarm: High = " + alarmHigh + tempUnit + ", Low = " + alarmLow + tempUnit); } break; case 3: // Set High and Low Alarms Debug.WriteLine("*************************************************************************"); if (!tc.hasTemperatureAlarms()) { Debug.WriteLine("Temperature alarms not supported"); } else { if (tempMode == CELSIUS) { Debug.WriteLine("*** Temperature value in Celsius ***"); } else { Debug.WriteLine("*** Temperature value in Fehrenheit ***"); } Debug.Write("Enter alarm high value: "); double inputHigh = Number; Debug.Write("Enter alarm low value: "); double inputLow = Number; if (tempMode == CELSIUS) { alarmHigh = inputHigh; alarmLow = inputLow; } else { alarmHigh = convertToCelsius(inputHigh); alarmLow = convertToCelsius(inputLow); } state = tc.readDevice(); tc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, alarmHigh, state); tc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, alarmLow, state); tc.writeDevice(state); state = tc.readDevice(); alarmLow = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, state); alarmHigh = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, state); if (tempMode == FAHRENHEIT) { alarmHigh = convertToFahrenheit(alarmHigh); alarmLow = convertToFahrenheit(alarmLow); } Debug.WriteLine(" Set Alarm: High = " + alarmHigh + tempUnit + ", Low = " + alarmLow + tempUnit); } break; case 4: // isAlarming if (!tc.hasTemperatureAlarms()) { Debug.WriteLine("Temperature alarms not supported"); } else { alarming = owc.Alarming; if (alarming) { Debug.WriteLine(" Alarming"); } else { Debug.WriteLine(" Not Alarming"); } } break; case 5: // Temperature Display Option Debug.WriteLine("*************************************************************************"); Debug.WriteLine("1. Celsius"); Debug.WriteLine("2. Fehrenheit"); Debug.Write("Please enter temperature display option: "); int choice = (int)Number; if (choice == 2) { tempMode = FAHRENHEIT; tempUnit = " F"; Debug.WriteLine(" Set to Fehrenheit display mode "); } else { tempMode = CELSIUS; tempUnit = " C"; Debug.WriteLine(" Set to Celsius display mode (default) "); } break; case 6: cleanup(); return; } //switch } catch (Exception e) { printException(e); } } // while }
// find the first OneWireContainer with TemperatureContainerDemo // interface. If found, initialize the container internal static OneWireContainer initContainer() { byte[] state = null; OneWireContainer owc = null; TemperatureContainer tc = null; try { adapter = OneWireAccessProvider.DefaultAdapter; // get exclusive use of adapter adapter.beginExclusive(true); adapter.setSearchAllDevices(); adapter.targetAllFamilies(); adapter.Speed = DSPortAdapter.SPEED_REGULAR; // enumerate through all the one wire device found for (System.Collections.IEnumerator owc_enum = adapter.AllDeviceContainers; owc_enum.MoveNext();) { // get the next owc owc = (OneWireContainer)owc_enum.Current; // check for one wire device that implements TemperatureCotainer interface if (owc is TemperatureContainer) { tc = (TemperatureContainer)owc; // access One Wire device state = tc.readDevice(); if (tc.hasSelectableTemperatureResolution()) { double[] resolution = tc.TemperatureResolutions; tc.setTemperatureResolution(resolution [resolution.Length - 1], state); } tc.writeDevice(state); // print device information Debug.WriteLine(""); Debug.WriteLine("*************************************************************************"); Debug.WriteLine("* 1-Wire Device Name: " + owc.Name); Debug.WriteLine("* 1-Wire Device Other Names: " + owc.AlternateNames); Debug.WriteLine("* 1-Wire Device Address: " + owc.AddressAsString); Debug.WriteLine("* 1-Wire Device Max speed: " + ((owc.MaxSpeed == DSPortAdapter.SPEED_OVERDRIVE) ? "Overdrive" : "Normal")); Debug.WriteLine("* 1-Wire Device Description: " + owc.Description); Debug.WriteLine("*************************************************************************"); Debug.WriteLine(" Hit ENTER to continue..."); dis.ReadLine(); break; } } // enum all owc } catch (Exception e) { printException(e); } return(owc); }