private static void GetSupportedAdapters() { var adapters = OneWireAccessProvider.enumerateAllAdapters(); if (!adapters.hasMoreElements()) { throw new Exception("No adapter is supported. Maybe the 1-wire driver is not correctly installed (see http://www.maximintegrated.com/products/ibutton/software/tmex/download_drivers.cfm)"); } var supportedAdapterString = "Supported adapters are : "; while (adapters.hasMoreElements()) { supportedAdapterString += ((DSPortAdapter)adapters.nextElement()).getAdapterName() + " "; } Logger.Logger.Log(supportedAdapterString); }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <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.targetAllFamilies(); access.beginExclusive(true); access.reset(); access.setSearchAllDevices(); bool next = access.findFirstDevice(); if (!next) { Debug.WriteLine("Could not find any iButtons!"); return; } while (next) { OneWireContainer owc = access.DeviceContainer; Debug.WriteLine("===================================================="); Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + " ="); Debug.WriteLine("===================================================="); Debug.WriteLine("="); bool isPotContainer = false; PotentiometerContainer pc = null; try { pc = (PotentiometerContainer)owc; isPotContainer = true; } catch (InvalidCastException) { pc = null; isPotContainer = false; //just to reiterate } if (isPotContainer) { Debug.WriteLine("= This device is a " + owc.Name); Debug.WriteLine("= Also known as a " + owc.AlternateNames); Debug.WriteLine("="); Debug.WriteLine("= It is a Potentiometer Container"); byte[] state = pc.readDevice(); bool charge = pc.isChargePumpOn(state); int wiper = pc.getCurrentWiperNumber(state); int position = pc.WiperPosition; bool linear = pc.isLinear(state); int numPots = pc.numberOfPotentiometers(state); int numSettings = pc.numberOfWiperSettings(state); int resistance = pc.potentiometerResistance(state); Debug.WriteLine("="); Debug.WriteLine("= Charge pump is " + (charge ? "ON" : "OFF")); Debug.WriteLine("= This device has " + numPots + " potentiometer" + ((numPots > 1) ? "s" : "")); Debug.WriteLine("= This device has a " + (linear ? "linear" : "logarithmic") + " potentiometer"); Debug.WriteLine("= It uses " + numSettings + " potentiometer wiper settings"); Debug.WriteLine("= The potentiometer resistance is " + resistance + " kOhms"); Debug.WriteLine("= CURRENT WIPER NUMBER : " + wiper); Debug.WriteLine("= CURRENT WIPER POSITION: " + position); Debug.WriteLine("= Trying to toggle the charge pump..."); pc.setChargePump(!charge, state); pc.writeDevice(state); state = pc.readDevice(); if (charge == pc.isChargePumpOn(state)) { Debug.WriteLine("= Could not toggle charge pump. Must have external power supplied."); } else { Debug.WriteLine("= Toggled charge pump successfully"); } //int newwiper = (int)((DateTime.Now.Ticks & 0x0ff00) >> 8); //pc.WiperPosition = newwiper; //Debug.WriteLine("= Setting wiper position to " + (newwiper)); Debug.WriteLine("= CURRENT WIPER POSITION: " + pc.WiperPosition); } else { Debug.WriteLine("= This device is not a potentiometer device."); Debug.WriteLine("="); Debug.WriteLine("="); } next = access.findNextDevice(); } }
/// <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])); }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <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.targetAllFamilies(); access.beginExclusive(true); access.reset(); access.setSearchAllDevices(); bool next = access.findFirstDevice(); if (!next) { Debug.WriteLine("Could not find any iButtons!"); return; } while (next) { OneWireContainer owc = access.DeviceContainer; Debug.WriteLine("===================================================="); Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + " ="); Debug.WriteLine("===================================================="); Debug.WriteLine("="); bool isClockContainer = false; ClockContainer cc = null; try { cc = (ClockContainer)owc; isClockContainer = true; } catch (Exception) { cc = null; isClockContainer = false; //just to reiterate } if (isClockContainer) { Debug.WriteLine("= This device is a " + owc.Name); Debug.WriteLine("= Also known as a " + owc.AlternateNames); Debug.WriteLine("="); Debug.WriteLine("= It is a Clock Container"); byte[] state = cc.readDevice(); long resolution = cc.ClockResolution; Debug.WriteLine("= The clock resolution is " + resolution + " milliseconds"); bool alarm = cc.hasClockAlarm(); Debug.WriteLine("= This clock " + (alarm ? "does" : "does not") + " have a clock alarm"); long rtc = cc.getClock(state); Debug.WriteLine("= Clock raw time: " + rtc); Debug.WriteLine("= Readable clock time: " + new DateTime(rtc)); bool alarmenabled = false; if (alarm) { long aclock = cc.getClockAlarm(state); alarmenabled = cc.isClockAlarmEnabled(state); bool alarming = cc.isClockAlarming(state); Debug.WriteLine("= Raw clock alarm: " + aclock); Debug.WriteLine("= Readable clock alarm: " + new DateTime(aclock)); Debug.WriteLine("= The alarm is" + (alarmenabled ? "" : " not") + " enabled"); Debug.WriteLine("= The alarm is" + (alarming ? "" : " not") + " alarming"); } bool running = cc.isClockRunning(state); bool candisable = cc.canDisableClock(); Debug.WriteLine("= The clock is" + (running ? "" : " not") + " running"); Debug.WriteLine("= The clock can" + (candisable ? "" : " not") + " be disabled"); TimeSpan t = new TimeSpan(DateTime.Now.Ticks); cc.setClock((long)t.TotalMilliseconds, state); if (alarm) { try { cc.setClockAlarm((long)t.TotalMilliseconds + 1000 * 60, state); Debug.WriteLine("= Set the clock alarm"); } catch (Exception) { Debug.WriteLine("= Could not set the clock alarm"); } cc.setClockAlarmEnable(!alarmenabled, state); Debug.WriteLine("= " + (alarmenabled ? "Disabled" : "Enabled") + " the clock alarm"); } if (candisable) { cc.setClockRunEnable(!running, state); Debug.WriteLine("= " + (running ? "Disabled" : "Enabled") + " the clock oscillator"); } Debug.WriteLine("= Writing device state..."); try { cc.writeDevice(state); Debug.WriteLine("= Successfully wrote device state"); } catch (Exception e) { Debug.WriteLine("= Failed to write device state: " + e.ToString()); } } else { Debug.WriteLine("= This device is not a Clock device."); Debug.WriteLine("="); Debug.WriteLine("="); } next = access.findNextDevice(); } }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <exception cref="OneWireException"> </exception> /// <exception cref="OneWireIOException"> /// </exception> public static void Main1([ReadOnlyArray()] 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) { //parse device instance //string[] st = args[0].Split(new char[] { '_' }); if (args.Length != 2) { printUsageString(); return; } adapter_name = args[0]; port_name = args[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 : " + ((DeviceInformation)f.Current).Id); } } return; } } access.adapterDetected(); access.targetAllFamilies(); access.beginExclusive(true); access.reset(); access.setSearchAllDevices(); bool next = access.findFirstDevice(); if (!next) { Debug.WriteLine("Could not find any iButtons!"); return; } while (next) { OneWireContainer owc = access.DeviceContainer; Debug.WriteLine("===================================================="); Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + " ="); Debug.WriteLine("===================================================="); Debug.WriteLine("="); bool isTempContainer = false; TemperatureContainer tc = null; try { tc = (TemperatureContainer)owc; isTempContainer = true; } catch (System.InvalidCastException) { tc = null; isTempContainer = false; //just to reiterate } if (isTempContainer) { Debug.WriteLine("= This device is a " + owc.Name); Debug.WriteLine("= Also known as a " + owc.AlternateNames); Debug.WriteLine("="); Debug.WriteLine("= It is a Temperature Container"); double max = tc.MaxTemperature; double min = tc.MinTemperature; bool hasAlarms = tc.hasTemperatureAlarms(); Debug.WriteLine("= This device " + (hasAlarms ? "has" : "does not have") + " alarms"); Debug.WriteLine("= Maximum temperature: " + max); Debug.WriteLine("= Minimum temperature: " + min); double high = 0.0; double low = 0.0; byte[] state = tc.readDevice(); if (hasAlarms) { high = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, state); low = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, state); Debug.WriteLine("= High temperature alarm set to : " + high); Debug.WriteLine("= Low temperature alarm set to : " + low); } double resol = 0.0; bool selectable = tc.hasSelectableTemperatureResolution(); if (hasAlarms) { resol = tc.TemperatureAlarmResolution; Debug.WriteLine("= Temperature alarm resolution : " + resol); } double tempres = tc.getTemperatureResolution(state); double[] resolution = null; Debug.WriteLine("= Temperature resolution : " + tempres); Debug.WriteLine("= Resolution is selectable : " + selectable); if (selectable) { try { resolution = tc.TemperatureResolutions; for (int i = 0; i < resolution.Length; i++) { Debug.WriteLine("= Available resolution " + i + " : " + resolution[i]); } } catch (Exception e) { Debug.WriteLine("= Could not get available resolutions : " + e.ToString()); } } if (hasAlarms) { Debug.WriteLine("= Setting high temperature alarm to 28.0 C..."); tc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, 28.0, state); Debug.WriteLine("= Setting low temperature alarm to 23.0 C..."); tc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, 23.0, state); } if (selectable) { try { Debug.WriteLine("= Setting temperature resolution to " + resolution[0] + "..."); tc.setTemperatureResolution(resolution[0], state); } catch (Exception e) { Debug.WriteLine("= Could not set resolution: " + e.ToString()); } } try { tc.writeDevice(state); Debug.WriteLine("= Device state written."); } catch (Exception e) { Debug.WriteLine("= Could not write device state, all changes lost."); Debug.WriteLine("= Exception occurred: " + e.ToString()); } Debug.WriteLine("= Doing temperature conversion..."); try { tc.doTemperatureConvert(state); } catch (Exception) { Debug.WriteLine("= Could not complete temperature conversion..."); } state = tc.readDevice(); if (hasAlarms) { high = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, state); low = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, state); Debug.WriteLine("= High temperature alarm set to : " + high); Debug.WriteLine("= Low temperature alarm set to : " + low); } double temp = tc.getTemperature(state); Debug.WriteLine("= Reported temperature: " + temp); } else { Debug.WriteLine("= This device is not a temperature device."); Debug.WriteLine("="); Debug.WriteLine("="); } next = access.findNextDevice(); } }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <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.targetAllFamilies(); access.beginExclusive(true); access.reset(); access.setSearchAllDevices(); bool next = access.findFirstDevice(); if (!next) { Debug.WriteLine("Could not find any iButtons!"); return; } while (next) { OneWireContainer owc = access.DeviceContainer; Debug.WriteLine("===================================================="); Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + " ="); Debug.WriteLine("===================================================="); Debug.WriteLine("="); bool isSwitchContainer = false; SwitchContainer sc = null; try { sc = (SwitchContainer)owc; isSwitchContainer = true; } catch (InvalidCastException) { sc = null; isSwitchContainer = false; //just to reiterate } if (isSwitchContainer) { Debug.WriteLine("= This device is a " + owc.Name); Debug.WriteLine("= Also known as a " + owc.AlternateNames); Debug.WriteLine("="); Debug.WriteLine("= It is a Switch Container"); if (sc.hasActivitySensing()) { sc.clearActivity(); } byte[] state = sc.readDevice(); int channels = sc.getNumberChannels(state); bool activity = sc.hasActivitySensing(); bool level = sc.hasLevelSensing(); bool smart = sc.hasSmartOn(); Debug.WriteLine("= This device has " + channels + " channel" + (channels > 1 ? "s" : "")); Debug.WriteLine("= It " + (activity ? "has" : "does not have") + " activity sensing abilities"); Debug.WriteLine("= It " + (level ? "has" : "does not have") + " level sensing abilities"); Debug.WriteLine("= It " + (smart ? "is" : "is not") + " smart-on capable"); for (int ch = 0; ch < channels; ch++) { Debug.WriteLine("======================"); Debug.WriteLine("= Channel " + ch + " ="); Debug.WriteLine("=--------------------="); bool latchstate = sc.getLatchState(ch, state); Debug.WriteLine("= State " + (latchstate ? "ON " : "OFF") + " ="); if (level) { bool sensedLevel = sc.getLevel(ch, state); Debug.WriteLine("= Level " + (sensedLevel ? "HIGH" : "LOW ") + " ="); } if (activity) { bool sensedActivity = sc.getSensedActivity(ch, state); Debug.WriteLine("= Activity " + (sensedActivity ? "YES" : "NO ") + " ="); } Debug.WriteLine("= Toggling switch... ="); try { Thread.Sleep(500); } catch (Exception) { /*drain it*/ } sc.setLatchState(ch, !latchstate, smart, state); sc.writeDevice(state); state = sc.readDevice(); if (latchstate == sc.getLatchState(ch, state)) { Debug.WriteLine("= Toggle Failed ="); } else { try { Thread.Sleep(500); } catch (Exception) { /*drain it*/ } Debug.WriteLine("= Toggling back... ="); sc.setLatchState(ch, latchstate, smart, state); sc.writeDevice(state); } } } else { Debug.WriteLine("= This device is not a Switch device."); Debug.WriteLine("="); Debug.WriteLine("="); } next = access.findNextDevice(); } }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <exception cref="OneWireException"> </exception> /// <exception cref="OneWireIOException"> /// </exception> public void Main([ReadOnlyArray()] 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) { //parse device instance //string[] st = args[0].Split(new char[] { '_' }); if (args.Length != 2) { printUsageString(); return; } adapter_name = args[0]; port_name = args[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 : " + ((DeviceInformation)f.Current).Id); } } return; } } access.adapterDetected(); access.targetAllFamilies(); access.beginExclusive(true); access.reset(); access.setSearchAllDevices(); bool next = access.findFirstDevice(); if (!next) { Debug.WriteLine("Could not find any iButtons!"); return; } while (next) { OneWireContainer owc = access.DeviceContainer; Debug.WriteLine("===================================================="); Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + " ="); Debug.WriteLine("===================================================="); Debug.WriteLine("="); bool isShaContainer = false; OneWireContainer33 sc = null; try { sc = (OneWireContainer33)owc; isShaContainer = true; } catch (System.InvalidCastException) { sc = null; isShaContainer = false; //just to reiterate } if (isShaContainer) { Debug.WriteLine("= This device is a " + owc.Name); Debug.WriteLine("= Also known as a " + owc.AlternateNames); Debug.WriteLine("="); Debug.WriteLine("= It is a 1K-Bit protected 1-Wire EEPROM with SHA-1 Engine device."); byte[] statusPage = null; ReadMemoryBank(sc.StatusPageMemoryBank, out statusPage); Debug.WriteLine("Status Page"); for (var i = 0; i < statusPage.Length; i++) { Debug.Write(statusPage[i].ToString("X") + " "); } Debug.WriteLine(""); byte[] mem = new byte[8]; Array.Copy(statusPage, 8, mem, 0, 8); if ((mem[0] == 0xAA) || (mem[0] == 0x55)) { Debug.WriteLine("SECRET IS WRITE PROTECTED - CANNOT LOAD FIRST SECRET"); } else { Debug.WriteLine("SECRET IS NOT WRITE PROTECTED!"); } if ((mem[1] == 0xAA) || (mem[1] == 0x55)) { Debug.WriteLine("memoryPages[0].readOnly = true"); Debug.WriteLine("memoryPages[1].readOnly = true"); Debug.WriteLine("memoryPages[2].readOnly = true"); } else { Debug.WriteLine("memoryPages[0].readWrite = true"); Debug.WriteLine("memoryPages[1].readWrite = true"); Debug.WriteLine("memoryPages[2].readWrite = true"); } if ((mem[4] == 0xAA) || (mem[4] == 0x55)) { Debug.WriteLine("memoryPages[1].writeOnce = true"); } else { Debug.WriteLine("memoryPages[1].writeOnce = false"); } if ((mem[5] == 0xAA) || (mem[5] == 0x55)) { Debug.WriteLine("memoryPages[0].readOnly = true"); } else { Debug.WriteLine("memoryPages[0].readWrite = true"); } Debug.WriteLine("mem[0] " + mem[0].ToString("X")); Debug.WriteLine("mem[1] " + mem[1].ToString("X")); Debug.WriteLine("mem[3] " + mem[3].ToString("X")); if (((mem[0] != 0xAA) && (mem[0] != 0x55)) && ((mem[3] != 0xAA) && (mem[3] != 0x55))) { Debug.WriteLine("Case 1"); //// Clear all memory to 0xFFh //for (i = 0; i < 16; i++) //{ // if (!LoadFirSecret(portnum, (ushort)(i * 8), data, 8)) // printf("MEMORY ADDRESS %d DIDN'T WRITE\n", i * 8); //} //printf("Current Bus Master Secret Is:\n"); //for (i = 0; i < 8; i++) // printf("%02X ", secret[i]); //printf("\n"); } else if ((mem[1] != 0xAA) || (mem[1] != 0x55)) { Debug.WriteLine("Case 2"); //printf("Please Enter the Current Secret\n"); //printf("AA AA AA AA AA AA AA AA <- Example\n"); //scanf("%s %s %s %s %s %s %s %s", &hexstr[0], &hexstr[2], &hexstr[4], // &hexstr[6], &hexstr[8], &hexstr[10], &hexstr[12], &hexstr[14]); //if (!ParseData(hexstr, strlen(hexstr), data, 16)) // printf("DIDN'T PARSE\n"); //else //{ // printf("The secret read was:\n"); // for (i = 0; i < 8; i++) // { // secret[i] = data[i]; // printf("%02X ", secret[i]); // data[i] = 0xFF; // } // printf("\n"); //} //printf("\n"); //if ((indata[13] == 0xAA) || (indata[13] == 0x55)) // skip = 4; //else // skip = 0; //for (i = (ushort)skip; i < 16; i++) //{ // ReadMem(portnum, (ushort)(((i * 8) / ((ushort)32)) * 32), memory); // if (WriteScratchSHAEE(portnum, (ushort)(i * 8), &data[0], 8)) // CopyScratchSHAEE(portnum, (ushort)(i * 8), secret, sn, memory); //} } else { Debug.WriteLine("Case 3"); //printf("Please Enter the Current Secret\n"); //printf("AA AA AA AA AA AA AA AA <- Example\n"); //scanf("%s %s %s %s %s %s %s %s", &hexstr[0], &hexstr[2], &hexstr[4], // &hexstr[6], &hexstr[8], &hexstr[10], &hexstr[12], &hexstr[14]); //if (!ParseData(hexstr, strlen(hexstr), secret, 16)) // printf("DIDN'T PARSE\n"); //else //{ // printf("The secret that was read:\n"); // for (i = 0; i < 8; i++) // { // printf("%02X ", secret[i]); // } // printf("\n"); //} } } else { Debug.WriteLine("= Not a SHA-1 Engine device."); Debug.WriteLine("="); Debug.WriteLine("="); } next = access.findNextDevice(); } }