/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <exception cref="OneWireException"> </exception> /// <exception cref="OneWireIOException"> /// </exception> public static void Main1(string[] args) { bool show_history = false; bool show_log = false; bool show_histogram = false; bool pre_kill = false; bool post_kill = false; bool usedefault = false; DSPortAdapter access = null; string adapter_name = null; string port_name = null; Stopwatch stopWatch = new Stopwatch(); if ((args == null) || (args.Length < 1) || (args [0].IndexOf("_", StringComparison.Ordinal) == -1)) { try { access = OneWireAccessProvider.DefaultAdapter; if (access == null) { throw new Exception(); } } catch (Exception) { Debug.WriteLine("Couldn't get default adapter!"); printUsageString(); return; } usedefault = true; } if ((args != null) && (args.Length > 0) && (usedefault)) { string arg = args [0]; if (arg.IndexOf("a", StringComparison.Ordinal) != -1) { show_history = true; } if (arg.IndexOf("l", StringComparison.Ordinal) != -1) { show_log = true; } if (arg.IndexOf("h", StringComparison.Ordinal) != -1) { show_histogram = true; } if (arg.IndexOf("k", StringComparison.Ordinal) != -1) { pre_kill = true; } if (arg.IndexOf("s", StringComparison.Ordinal) != -1) { post_kill = true; } } if (!usedefault) { string[] st = args[0].Split(new char[] { '_' }); if (st.Length != 2) { printUsageString(); return; } if (args.Length > 1) { string arg = args [1]; if (arg.IndexOf("a", StringComparison.Ordinal) != -1) { show_history = true; } if (arg.IndexOf("l", StringComparison.Ordinal) != -1) { show_log = true; } if (arg.IndexOf("h", StringComparison.Ordinal) != -1) { show_histogram = true; } if (arg.IndexOf("k", StringComparison.Ordinal) != -1) { pre_kill = true; } if (arg.IndexOf("s", StringComparison.Ordinal) != -1) { post_kill = true; } } adapter_name = st[0]; port_name = st[1]; Debug.WriteLine("Adapter Name: " + adapter_name); Debug.WriteLine("Port Name: " + port_name); } if (access == null) { try { access = OneWireAccessProvider.getAdapter(adapter_name, port_name); } catch (Exception) { Debug.WriteLine("That is not a valid adapter/port combination."); System.Collections.IEnumerator en = OneWireAccessProvider.enumerateAllAdapters(); while (en.MoveNext()) { DSPortAdapter temp = (DSPortAdapter)en.Current; Debug.WriteLine("Adapter: " + temp.AdapterName); System.Collections.IEnumerator f = temp.PortNames; while (f.MoveNext()) { Debug.WriteLine(" Port name : " + ((string)f.Current)); } } return; } } access.adapterDetected(); access.targetFamily(0x21); access.beginExclusive(true); access.reset(); access.setSearchAllDevices(); bool next = access.findFirstDevice(); if (!next) { Debug.WriteLine("Could not find any DS1921 Thermocrons!"); return; } OneWireContainer21 owc = new OneWireContainer21(); owc.setupContainer(access, access.AddressAsLong); //put the part into overdrive...make it sizzle! owc.setSpeed(DSPortAdapter.SPEED_OVERDRIVE, true); //let's gather our information here... stopWatch.Start(); if (pre_kill) { try { owc.disableMission(); } catch (Exception e) { Debug.WriteLine("Couldn't end mission before reading: " + e.ToString()); } } bool mission_in_progress = owc.getFlag(OneWireContainer21.STATUS_REGISTER, OneWireContainer21.MISSION_IN_PROGRESS_FLAG); byte[] state; DateTime cal = new DateTime(); // first, check to make sure that the thermochron isn't // sampling, or at least that a sample isn't about to occur. bool isSampling = false; do { state = owc.readDevice(); cal = new DateTime(owc.getClock(state) * TimeSpan.TicksPerMillisecond); isSampling = mission_in_progress && (owc.getFlag(OneWireContainer21.STATUS_REGISTER, OneWireContainer21.SAMPLE_IN_PROGRESS_FLAG, state) || (cal.Second > 55)); if (isSampling) { // wait for current sample to finish Thread.Sleep(1000); } } while (isSampling); int mission_count = owc.getMissionSamplesCounter(state); int device_count = owc.getDeviceSamplesCounter(state); long rtc = owc.getClock(state); long next_alarm = owc.getClockAlarm(state); DateTime time_stamp = owc.getMissionTimeStamp(state); int sample_rate = owc.getSampleRate(state); double high_alarm = owc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, state); double low_alarm = owc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, state); int[] histogram = owc.TemperatureHistogram; byte[] log = owc.getTemperatureLog(state); byte[] high_history = owc.getAlarmHistory(OneWireContainer21.TEMPERATURE_HIGH_ALARM); byte[] low_history = owc.getAlarmHistory(OneWireContainer21.TEMPERATURE_LOW_ALARM); stopWatch.Stop(); bool clock_enabled = owc.isClockRunning(state); bool alarm_enabled = owc.isClockAlarmEnabled(state); bool clock_alarm = owc.isClockAlarming(state); bool rollover = owc.getFlag(OneWireContainer21.CONTROL_REGISTER, OneWireContainer21.ROLLOVER_ENABLE_FLAG, state); double current_temp = 0; string mission_in_progress_string; if (!mission_in_progress) { owc.doTemperatureConvert(state); current_temp = owc.getTemperature(state); mission_in_progress_string = "- NO MISSION IN PROGRESS AT THIS TIME"; } else { mission_in_progress_string = "- MISSION IS CURRENTLY RUNNING"; } //spew all this data Debug.WriteLine("Dallas Semiconductor DS1921 Thermocron Mission Summary Demo"); Debug.WriteLine("-----------------------------------------------------------"); Debug.WriteLine("- Device ID : " + owc.AddressAsString); Debug.WriteLine(mission_in_progress_string); if (!mission_in_progress) { Debug.WriteLine("- Current Temperature : " + current_temp); } else { Debug.WriteLine("- Cannot read current temperature with mission in progress"); } Debug.WriteLine("-----------------------------------------------------------"); Debug.WriteLine("- Number of mission samples: " + mission_count); Debug.WriteLine("- Total number of samples : " + device_count); Debug.WriteLine("- Real Time Clock : " + (clock_enabled ? "ENABLED" : "DISABLED")); Debug.WriteLine("- Real Time Clock Value : " + (new DateTime(rtc * TimeSpan.TicksPerMillisecond)).ToString("r")); Debug.WriteLine("- Clock Alarm : " + (alarm_enabled ? "ENABLED" : "DISABLED")); if (alarm_enabled) { Debug.WriteLine("- Clock Alarm Status : " + (clock_alarm ? "ALARMING" : "NOT ALARMING")); Debug.WriteLine("- Next alarm occurs at : " + (new DateTime(next_alarm * TimeSpan.TicksPerMillisecond)).ToString("r")); } Debug.WriteLine("- Last mission started : " + time_stamp); Debug.WriteLine("- Sample rate : Every " + sample_rate + " minutes"); Debug.WriteLine("- High temperature alarm : " + high_alarm); Debug.WriteLine("- Low temperature alarm : " + low_alarm); Debug.WriteLine("- Rollover enabled : " + (rollover ? "YES" : "NO")); Debug.WriteLine("- Time to read Thermocron : " + (stopWatch.ElapsedMilliseconds) + " milliseconds"); Debug.WriteLine("-----------------------------------------------------------"); if (show_history) { int start_offset, violation_count; Debug.WriteLine("-"); Debug.WriteLine("- ALARM HISTORY"); if (low_history.Length == 0) { Debug.WriteLine("- No violations against the low temperature alarm."); Debug.WriteLine("-"); } for (int i = 0; i < low_history.Length / 4; i++) { start_offset = (low_history [i * 4] & 0x0ff) | ((low_history [i * 4 + 1] << 8) & 0x0ff00) | ((low_history [i * 4 + 2] << 16) & 0x0ff0000); violation_count = 0x0ff & low_history [i * 4 + 3]; Debug.WriteLine("- Low alarm started at : " + (start_offset * sample_rate)); Debug.WriteLine("- : Lasted " + (violation_count * sample_rate) + " minutes"); } if (high_history.Length == 0) { Debug.WriteLine("- No violations against the high temperature alarm."); Debug.WriteLine("-"); } for (int i = 0; i < high_history.Length / 4; i++) { start_offset = (high_history [i * 4] & 0x0ff) | ((high_history [i * 4 + 1] << 8) & 0x0ff00) | ((high_history [i * 4 + 2] << 16) & 0x0ff0000); violation_count = 0x0ff & high_history [i * 4 + 3]; Debug.WriteLine("- High alarm started at : " + (start_offset * sample_rate)); Debug.WriteLine("- : Lasted " + (violation_count * sample_rate) + " minutes"); } Debug.WriteLine("-----------------------------------------------------------"); } if (show_log) { long time = (time_stamp.Ticks / TimeSpan.TicksPerMillisecond) + owc.getFirstLogOffset(state); Debug.WriteLine("-"); Debug.WriteLine("- TEMPERATURE LOG"); for (int i = 0; i < log.Length; i++) { DateTime gc = new DateTime(time * TimeSpan.TicksPerMillisecond); Debug.WriteLine("- Temperature recorded at : " + gc.ToString("r")); Debug.WriteLine("- was : " + owc.decodeTemperature(log [i]) + " C"); time += sample_rate * 60 * 1000; } Debug.WriteLine("-----------------------------------------------------------"); } if (show_histogram) { double resolution = owc.TemperatureResolution; double histBinWidth = owc.HistogramBinWidth; double start = owc.HistogramLowTemperature; Debug.WriteLine("-"); Debug.WriteLine("- TEMPERATURE HISTOGRAM"); for (int i = 0; i < histogram.Length; i++) { Debug.WriteLine("- Histogram entry : " + histogram [i] + " at temperature " + start + " to " + (start + (histBinWidth - resolution)) + " C"); start += histBinWidth; } } if (post_kill) { try { owc.disableMission(); } catch (Exception e) { Debug.WriteLine("Couldn't end mission after reading: " + e.ToString()); } } access.endExclusive(); access.freePort(); access = null; Debug.WriteLine("Finished."); }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <exception cref="IOException"> </exception> /// <exception cref="OneWireException"> </exception> /// <exception cref="OneWireIOException"> /// </exception> public static void Main1(string[] args) { bool usedefault = false; DSPortAdapter access = null; string adapter_name = null; string port_name = null; if ((args == null) || (args.Length < 1)) { try { access = OneWireAccessProvider.DefaultAdapter; if (access == null) { throw new Exception(); } } catch (Exception) { Debug.WriteLine("Couldn't get default adapter!"); printUsageString(); return; } usedefault = true; } if (!usedefault) { string[] st = args[0].Split(new char[] { '_' }); if (st.Length != 2) { printUsageString(); return; } adapter_name = st[0]; port_name = st[1]; Debug.WriteLine("Adapter Name: " + adapter_name); Debug.WriteLine("Port Name: " + port_name); } if (access == null) { try { access = OneWireAccessProvider.getAdapter(adapter_name, port_name); } catch (Exception) { Debug.WriteLine("That is not a valid adapter/port combination."); System.Collections.IEnumerator en = OneWireAccessProvider.enumerateAllAdapters(); while (en.MoveNext()) { DSPortAdapter temp = (DSPortAdapter)en.Current; Debug.WriteLine("Adapter: " + temp.AdapterName); System.Collections.IEnumerator f = temp.PortNames; while (f.MoveNext()) { Debug.WriteLine(" Port name : " + ((string)f.Current)); } } return; } } access.adapterDetected(); access.targetFamily(0x21); access.beginExclusive(true); access.reset(); access.setSearchAllDevices(); bool next = access.findFirstDevice(); if (!next) { Debug.WriteLine("Could not find any DS1921 Thermochrons!"); return; } OneWireContainer21 owc = new OneWireContainer21(); owc.setupContainer(access, access.AddressAsLong); Stream stream = loadResourceFile("Thermochron.input.txt"); System.IO.StreamReader din = new StreamReader(stream); //the following section of code jus gets all these options from the command line //to see how to actually talk to the iButton, scroll down until you find //the code "disableMission() Debug.WriteLine("Dallas Semiconductor DS1921 Thermochron Demo"); Debug.WriteLine("--------------------------------------------"); Debug.WriteLine("Initializing mission on iButton " + owc.AddressAsString + "\r\n"); string tstr; bool rollover = false; Debug.Write("Enable rollover (y or n)? "); tstr = din.ReadLine(); if (tstr.Equals("y", StringComparison.CurrentCultureIgnoreCase) || tstr.Equals("yes", StringComparison.CurrentCultureIgnoreCase)) { rollover = true; } Debug.Write("Enter low temperature alarm in celsius (23) : "); int low = parseInt(din, 23); Debug.Write("Enter high temperature alarm in celsius (28) : "); int high = parseInt(din, 28); bool clockalarm = false; Debug.Write("Enable clock alarm (y or n)? "); tstr = din.ReadLine(); if (tstr.Equals("y", StringComparison.CurrentCultureIgnoreCase) || tstr.Equals("yes", StringComparison.CurrentCultureIgnoreCase)) { clockalarm = true; } int second = 0; int minute = 0; int hour = 0; int day = 0; int frequency = -1; if (clockalarm) { Debug.WriteLine("Clock alarm enabled. Enter alarm frequency: "); Debug.WriteLine(" 0 Once per second"); Debug.WriteLine(" 1 Once per minute"); //need second Debug.WriteLine(" 2 Once per hour"); //need second, minute Debug.WriteLine(" 3 Once per day"); //need hour, minute, second Debug.WriteLine(" 4 Once per week"); //need hour, minute, second, day Debug.Write(" ? "); int x = parseInt(din, -1); if ((x < 0) || (x > 4)) { Debug.WriteLine("That is not a valid clock alarm frequency."); return; } switch (x) //noteice no breaks! { case 4: if (frequency == -1) { frequency = OneWireContainer21.ONCE_PER_WEEK; } Debug.Write("Day of week to alarm (1==Sunday) : "); day = parseInt(din, 1); goto case 3; case 3: if (frequency == -1) { frequency = OneWireContainer21.ONCE_PER_DAY; } Debug.Write("Hour of day to alarm (0 - 23) : "); hour = parseInt(din, 0); goto case 2; case 2: if (frequency == -1) { frequency = OneWireContainer21.ONCE_PER_HOUR; } Debug.Write("Minute of hour to alarm (0 - 59) : "); minute = parseInt(din, 0); goto case 1; case 1: if (frequency == -1) { frequency = OneWireContainer21.ONCE_PER_MINUTE; } Debug.Write("Second of minute to alarm (0 - 59) : "); second = parseInt(din, 0); goto case 0; case 0: if (frequency == -1) { frequency = OneWireContainer21.ONCE_PER_SECOND; } break; } } bool synchronize = false; Debug.Write("Set thermochron clock to system clock (y or n)? "); tstr = din.ReadLine(); if (tstr.Equals("y", StringComparison.CurrentCultureIgnoreCase) || tstr.Equals("yes", StringComparison.CurrentCultureIgnoreCase)) { synchronize = true; } Debug.Write("Start the mission in how many minutes? "); int delay = 0; delay = parseInt(din, 0); Debug.Write("Sampling Interval in minutes (1 to 255)? "); int rate = 1; rate = parseInt(din, 1); //now do some bounds checking if (rate < 1) { rate = 1; } if (rate > 255) { rate = 255; } delay = delay & 0x0ffff; int physicalLow = (int)owc.PhysicalRangeLowTemperature; int physicalHigh = (int)owc.PhysicalRangeHighTemperature; if (low < physicalLow) { low = physicalLow; } if (low > physicalHigh) { low = physicalHigh; } if (high < physicalLow) { high = physicalLow; } if (high > physicalHigh) { high = physicalHigh; } if (day < 1) { day = 1; } if (day > 7) { day = 7; } second = second % 60; minute = minute % 60; hour = hour % 24; //some regurgitation first.... Debug.WriteLine("\r\n\r\nSummary---------------------"); Debug.WriteLine("Rollover enabled : " + rollover); Debug.WriteLine("Low temperature alarm trigger : " + low); Debug.WriteLine("High temperature alarm trigger: " + high); Debug.WriteLine("Clock alarm enabled : " + clockalarm); if (clockalarm) { Debug.Write("Alarm frequency : "); switch (frequency) { case OneWireContainer21.ONCE_PER_SECOND: Debug.WriteLine("Once per second"); break; case OneWireContainer21.ONCE_PER_MINUTE: Debug.WriteLine("Once per minute"); break; case OneWireContainer21.ONCE_PER_HOUR: Debug.WriteLine("Once per hour"); break; case OneWireContainer21.ONCE_PER_DAY: Debug.WriteLine("Once per day"); break; case OneWireContainer21.ONCE_PER_WEEK: Debug.WriteLine("Once per week"); break; default: Debug.WriteLine("Unknown alarm frequency!!! Bailing!!!"); return; } Debug.Write("Alarm setting : " + hour + ":" + minute + ":" + second + " "); switch (day) { case 1: Debug.WriteLine("Sunday"); break; case 2: Debug.WriteLine("Monday"); break; case 3: Debug.WriteLine("Tuesday"); break; case 4: Debug.WriteLine("Wednesday"); break; case 5: Debug.WriteLine("Thursday"); break; case 6: Debug.WriteLine("Friday"); break; case 7: Debug.WriteLine("Saturday"); break; default: Debug.WriteLine("Unknown day of week! Bailing!"); return; } } Debug.WriteLine("Synchonizing with host clock : " + synchronize); Debug.WriteLine("Mission starts in (minutes) : " + delay); Debug.WriteLine("Sampling rate (minutes) : " + rate); Debug.WriteLine("-------------------------------\r\n"); //now let's start talking to the thermochron //first lets put it into overdrive Debug.WriteLine("Putting the part into overdrive..."); owc.setSpeed(DSPortAdapter.SPEED_OVERDRIVE, true); Debug.WriteLine("Disabling current mission..."); owc.disableMission(); Debug.WriteLine("Clearing memory..."); owc.clearMemory(); Debug.WriteLine("Reading device state..."); byte[] state = owc.readDevice(); Debug.WriteLine("Setting rollover flag in state..."); owc.setFlag(OneWireContainer21.CONTROL_REGISTER, OneWireContainer21.ROLLOVER_ENABLE_FLAG, rollover, state); Debug.WriteLine("Setting high alarm in state..."); owc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, (double)high, state); Debug.WriteLine("Setting low alarm in state..."); owc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, (double)low, state); if (clockalarm) { Debug.WriteLine("Setting clock alarm in state..."); owc.setClockAlarm(hour, minute, second, day, frequency, state); Debug.WriteLine("Enabling clock alarm in state..."); owc.setClockAlarmEnable(true, state); } if (synchronize) { Debug.WriteLine("Synchonizing with host clock in state..."); owc.setClock(DateTime.Now.Ticks * TimeSpan.TicksPerMillisecond, state); } Debug.WriteLine("Setting mission delay in state..."); owc.setMissionStartDelay(delay, state); Debug.WriteLine("Enabling the clock oscillator in state..."); owc.setClockRunEnable(true, state); Debug.WriteLine("Writing state back to Thermochron..."); owc.writeDevice(state); Debug.WriteLine("Enabling mission..."); owc.enableMission(rate); Debug.WriteLine("Initialization Complete."); // state = owc.readDevice(); // for (int i=0;i<state.length;i++) // System.out.println("State["+(i < 0x10 ? "0" : "")+Integer.toHexString(i)+"] == "+Integer.toHexString(0x0ff & state[i])); }