private void startNewMission_Click(object sender, System.EventArgs e) { counter = 0; try { adapter.beginExclusive(true); byte[] buffer = new byte [10]; // make sure no mission is running on device buffer[0] = OneWireContainer41.STOP_MISSION_PW_COMMAND; buffer[9] = (byte)0xFF; if (owc41 != null) { // stop mission on single device adapter.reset(); adapter.assertSelect(owc41.Address); adapter.dataBlock(buffer, 0, buffer.Length); StartNewMission(); } else { // stop mission on all device adapter.reset(); adapter.putByte(0xCC); // Skip rom adapter.dataBlock(buffer, 0, buffer.Length); adapter.targetFamily(0x41); owc41 = (OneWireContainer41)adapter.FirstDeviceContainer; for (; owc41 != null; owc41 = (OneWireContainer41)adapter.NextDeviceContainer) { StartNewMission(); } } MessageBox.Show("Succesfully missioned " + counter + " device(s)"); } catch (Exception ex) { MessageBox.Show("Exception: " + ex.Message + "\n" + ex.StackTrace); } finally { adapter.endExclusive(); } }
public StartMissionForm(DSPortAdapter adapter, OneWireContainer41 owc41) { this.adapter = adapter; this.owc41 = owc41; // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // temperatureResolution.SelectedIndex = 0; humidityResolution.SelectedIndex = 0; if (owc41 == null) { this.Text += " on all DS1922s"; this.humidityGroupBox.Text = "Data"; } else { this.humidityGroupBox.Text = owc41.getMissionLabel(OneWireContainer41.DATA_CHANNEL); this.Text += " on device: " + owc41.AddressAsString; } if (this.humidityGroupBox.Text.Equals("Humidity")) { this.enableHumiditySampling.Text = "Enable Humidity Sampling"; this.enableHumdHighAlarm.Text = "Enable High Alarm (%RH)"; this.enableHumdLowAlarm.Text = "Enable Low Alarm (%RH)"; } else { this.enableHumiditySampling.Text = "Enable Sampling"; this.enableHumdHighAlarm.Text = "Enable High Alarm"; this.enableHumdLowAlarm.Text = "Enable Low Alarm"; } if (owc41 != null && (owc41.Name.Equals("DS1922T") || owc41.Name.Equals("DS1922L") || owc41.Name.Equals("DS1922E"))) { this.humidityGroupBox.Enabled = false; this.enableHumiditySampling.Checked = false; } }
/// <summary> /// the main routine, parses the input switches, dumps the mission data </summary> public static void Main1(string[] args) { DSPortAdapter adapter = null; bool showHumidity = true; bool showTemperature = true; bool stopMission = false; bool useOverdrive = false; byte[] readWritePass = null, readOnlyPass = null; try { if (args.Length > 0) { for (int i = 0; i < args.Length; i++) { string arg = args[i].ToUpper(); if (arg.Equals(usageString[0][0].ToUpper())) { showTemperature = false; } else if (arg.Equals(usageString[1][0].ToUpper())) { showHumidity = false; } else if (arg.Equals(usageString[2][0].ToUpper())) { stopMission = true; } else if (arg.Equals(usageString[3][0].ToUpper())) { useOverdrive = true; } else if (arg.IndexOf(usageString[4][0].ToUpper(), StringComparison.Ordinal) == 0) { //TODO confirm this conversion readWritePass = Encoding.UTF8.GetBytes(arg.Substring(usageString[4][0].Length)); } else if (arg.IndexOf(usageString[5][0].ToUpper(), StringComparison.Ordinal) == 0) { //TODO confirm this conversion readOnlyPass = Encoding.UTF8.GetBytes(arg.Substring(usageString[5][0].Length)); } else if (arg.Equals("-H")) { usage(); } else { Debug.WriteLine("bad argument: '" + args[i] + "'"); usage(); } } } adapter = OneWireAccessProvider.DefaultAdapter; adapter.beginExclusive(true); adapter.targetFamily(0x41); OneWireContainer41 owc = (OneWireContainer41)adapter.FirstDeviceContainer; if (owc != null) { Debug.WriteLine("Found " + owc.ToString()); if (useOverdrive) { Debug.WriteLine("setting speed as overdrive"); owc.setSpeed(DSPortAdapter.SPEED_OVERDRIVE, true); } if (readWritePass != null) { owc.setContainerReadWritePassword(readWritePass, 0); } if (readOnlyPass != null) { owc.setContainerReadOnlyPassword(readOnlyPass, 0); } byte[] state = owc.readDevice(); //read to set container variables if (stopMission) { Debug.WriteLine("Stopping mission"); bool missionStopped = false; while (!missionStopped) { try { if (!owc.MissionRunning) { Debug.WriteLine("Mission is stopped"); missionStopped = true; } else { owc.stopMission(); } } catch (Exception) { ; } } } bool loadResults = false; while (!loadResults) { try { Debug.WriteLine("loading mission results"); owc.loadMissionResults(); loadResults = true; } catch (Exception e) { Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); } } Debug.WriteLine("Is Mission Running: " + owc.MissionRunning); if (owc.MissionSUTA) { Debug.WriteLine("Start Upon Temperature Alarm: " + (owc.MissionWFTA?"Waiting for Temperature Alarm":"Got Temperature Alarm, Mission Started")); } Debug.WriteLine("Sample Rate: " + owc.getMissionSampleRate(0) + " secs"); Debug.WriteLine("Mission Start Time: " + (new DateTime(owc.getMissionTimeStamp(0)))); Debug.WriteLine("Mission Sample Count: " + owc.getMissionSampleCount(0)); Debug.WriteLine("Rollover Enabled: " + owc.MissionRolloverEnabled); if (owc.MissionRolloverEnabled) { Debug.WriteLine("Rollover Occurred: " + owc.hasMissionRolloverOccurred()); if (owc.hasMissionRolloverOccurred()) { Debug.WriteLine("First Sample Timestamp: " + (new DateTime(owc.getMissionSampleTimeStamp(OneWireContainer41.TEMPERATURE_CHANNEL, 0) | owc.getMissionSampleTimeStamp(OneWireContainer41.DATA_CHANNEL, 0)))); Debug.WriteLine("Total Mission Samples: " + owc.getMissionSampleCountTotal(0)); } } Debug.WriteLine("Temperature Logging: " + (!owc.getMissionChannelEnable(OneWireContainer41.TEMPERATURE_CHANNEL)? "Disabled": owc.getMissionResolution(OneWireContainer41.TEMPERATURE_CHANNEL) + " bit")); Debug.WriteLine("Temperature Low Alarm: " + (!owc.getMissionAlarmEnable(OneWireContainer41.TEMPERATURE_CHANNEL, 0)? "Disabled": owc.getMissionAlarm(OneWireContainer41.TEMPERATURE_CHANNEL, 0) + "C (" + (owc.hasMissionAlarmed(OneWireContainer41.TEMPERATURE_CHANNEL, 0)? "ALARM)":"no alarm)"))); Debug.WriteLine("Temperature High Alarm: " + (!owc.getMissionAlarmEnable(OneWireContainer41.TEMPERATURE_CHANNEL, 1)? "Disabled": owc.getMissionAlarm(OneWireContainer41.TEMPERATURE_CHANNEL, 1) + "C (" + (owc.hasMissionAlarmed(OneWireContainer41.TEMPERATURE_CHANNEL, 1)? "ALARM)":"no alarm)"))); Debug.WriteLine(owc.getMissionLabel(OneWireContainer41.DATA_CHANNEL) + " Logging: " + (!owc.getMissionChannelEnable(OneWireContainer41.DATA_CHANNEL)? "Disabled": owc.getMissionResolution(OneWireContainer41.DATA_CHANNEL) + " bit")); Debug.WriteLine(owc.getMissionLabel(OneWireContainer41.DATA_CHANNEL) + " Low Alarm: " + (!owc.getMissionAlarmEnable(OneWireContainer41.DATA_CHANNEL, 0)? "Disabled": owc.getMissionAlarm(OneWireContainer41.DATA_CHANNEL, 0) + "% RH (" + (owc.hasMissionAlarmed(OneWireContainer41.DATA_CHANNEL, 0)? "ALARM)":"no alarm)"))); Debug.WriteLine(owc.getMissionLabel(OneWireContainer41.DATA_CHANNEL) + " High Alarm: " + (!owc.getMissionAlarmEnable(OneWireContainer41.DATA_CHANNEL, 1)? "Disabled": owc.getMissionAlarm(OneWireContainer41.DATA_CHANNEL, 1) + "% RH (" + (owc.hasMissionAlarmed(OneWireContainer41.DATA_CHANNEL, 1)? "ALARM)":"no alarm)"))); Debug.WriteLine("Total Device Samples: " + owc.DeviceSampleCount); if (showTemperature) { Debug.WriteLine("Temperature Readings"); if (!owc.getMissionChannelEnable(OneWireContainer41.TEMPERATURE_CHANNEL)) { Debug.WriteLine("Temperature Mission Not enabled"); } else { int dataCount = owc.getMissionSampleCount(OneWireContainer41.TEMPERATURE_CHANNEL); Debug.WriteLine("SampleCount = " + dataCount); for (int i = 0; i < dataCount; i++) { Debug.WriteLine(owc.getMissionSample(OneWireContainer41.TEMPERATURE_CHANNEL, i)); } } } if (showHumidity) { Debug.WriteLine(owc.getMissionLabel(OneWireContainer41.DATA_CHANNEL) + " Readings"); if (!owc.getMissionChannelEnable(OneWireContainer41.DATA_CHANNEL)) { Debug.WriteLine(owc.getMissionLabel(OneWireContainer41.DATA_CHANNEL) + " Mission Not enabled"); } else { int dataCount = owc.getMissionSampleCount(OneWireContainer41.DATA_CHANNEL); Debug.WriteLine("SampleCount = " + dataCount); for (int i = 0; i < dataCount; i++) { Debug.WriteLine(owc.getMissionSample(OneWireContainer41.DATA_CHANNEL, i)); } } } } else { Debug.WriteLine("No DS1922/DS2422 device found"); } } catch (Exception e) { Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); } finally { if (adapter != null) { adapter.endExclusive(); } } }
/// <summary> /// the main routine, parses the input switches, starts the mission </summary> public static void Main2(string[] args) { DSPortAdapter adapter = null; bool startUponTempAlarm = false; bool rollover = false; int sampleRate = -1; int tempBytes = 1; int dataBytes = 1; int startDelay = 0; double tempAlarmHigh = -1, tempAlarmLow = -1; double dataAlarmHigh = -1, dataAlarmLow = -1; byte[] readWritePass = null; if (args.Length == 0) { usage(); } for (int i = 0; i < args.Length; i++) { string arg = args[i].ToUpper(); if (arg.IndexOf(usageString[0][0].ToUpper(), StringComparison.Ordinal) == 0) { sampleRate = int.Parse(arg.Substring(usageString[0][0].Length)); } else if (arg.IndexOf(usageString[1][0].ToUpper(), StringComparison.Ordinal) == 0) { tempBytes = int.Parse(arg.Substring(usageString[1][0].Length)); if (tempBytes > 2) { tempBytes = 2; } else if (tempBytes < 0) { tempBytes = 0; } } else if (arg.IndexOf(usageString[2][0].ToUpper(), StringComparison.Ordinal) == 0) { dataBytes = int.Parse(arg.Substring(usageString[2][0].Length)); if (dataBytes > 2) { dataBytes = 2; } else if (dataBytes < 0) { dataBytes = 0; } } else if (arg.IndexOf(usageString[3][0].ToUpper(), StringComparison.Ordinal) == 0) { tempAlarmHigh = toDouble(arg.Substring(usageString[3][0].Length)); } else if (arg.IndexOf(usageString[4][0].ToUpper(), StringComparison.Ordinal) == 0) { tempAlarmLow = toDouble(arg.Substring(usageString[4][0].Length)); } else if (arg.IndexOf(usageString[5][0].ToUpper(), StringComparison.Ordinal) == 0) { dataAlarmHigh = toDouble(arg.Substring(usageString[5][0].Length)); } else if (arg.IndexOf(usageString[6][0].ToUpper(), StringComparison.Ordinal) == 0) { dataAlarmLow = toDouble(arg.Substring(usageString[5][0].Length)); } else if (arg.Equals(usageString[7][0].ToUpper())) { startUponTempAlarm = true; } else if (arg.Equals(usageString[8][0].ToUpper())) { rollover = true; } else if (arg.IndexOf(usageString[9][0].ToUpper(), StringComparison.Ordinal) == 0) { startDelay = int.Parse(arg.Substring(usageString[9][0].Length)); } else if (arg.IndexOf(usageString[10][0].ToUpper(), StringComparison.Ordinal) == 0) { readWritePass = Encoding.UTF8.GetBytes(arg.Substring(usageString[10][0].Length)); } else if (arg.Equals("-H")) { usage(); } else { Debug.WriteLine("bad argument: '" + args[i] + "'"); usage(); } } if (sampleRate == -1) { Debug.WriteLine("You must provide a sample rate"); usage(); } Debug.WriteLine("Sample Rate (seconds) = " + sampleRate); Debug.WriteLine("Temperature Bytes = " + tempBytes); Debug.WriteLine("Humidity Bytes = " + dataBytes); if (tempAlarmHigh != -1) { Debug.WriteLine("Temperature Alarm High = " + tempAlarmHigh); } if (tempAlarmLow != -1) { Debug.WriteLine("Temperature Alarm Low = " + tempAlarmLow); } if (dataAlarmHigh != -1) { Debug.WriteLine("Data Alarm High = " + dataAlarmHigh); } if (dataAlarmLow != -1) { Debug.WriteLine("Data Alarm Low = " + dataAlarmLow); } Debug.WriteLine("Start Upon Temp Alarm = " + startUponTempAlarm); Debug.WriteLine("Rollover Enabled = " + rollover); Debug.WriteLine("Start Delay (minutes) = " + startDelay); Debug.WriteLine(""); if (startUponTempAlarm && (tempAlarmHigh == -1 || tempAlarmLow == -1)) { Debug.WriteLine("You selected a SUTA mission, but didn't specify high and low temp alarms"); usage(); } try { adapter = OneWireAccessProvider.DefaultAdapter; adapter.beginExclusive(true); adapter.targetFamily(0x41); OneWireContainer41 owc = (OneWireContainer41)adapter.FirstDeviceContainer; if (readWritePass != null) { owc.setContainerReadWritePassword(readWritePass, 0); } byte[] state = owc.readDevice(); //read to set container variables if (owc != null) { Debug.WriteLine("Found " + owc.ToString()); Debug.WriteLine("Stopping current mission, if there is one"); if (owc.MissionRunning) { owc.stopMission(); } Debug.WriteLine("Starting a new mission"); if (tempBytes == 1) { owc.setMissionResolution(0, owc.getMissionResolutions(0)[0]); } else { owc.setMissionResolution(0, owc.getMissionResolutions(0)[1]); } if (dataBytes == 1) { owc.setMissionResolution(1, owc.getMissionResolutions(1)[0]); } else { owc.setMissionResolution(1, owc.getMissionResolutions(1)[1]); } if (tempAlarmHigh != -1) { owc.setMissionAlarm(OneWireContainer41.TEMPERATURE_CHANNEL, TemperatureContainer_Fields.ALARM_HIGH, tempAlarmHigh); owc.setMissionAlarmEnable(OneWireContainer41.TEMPERATURE_CHANNEL, TemperatureContainer_Fields.ALARM_HIGH, true); } else { owc.setMissionAlarmEnable(OneWireContainer41.TEMPERATURE_CHANNEL, TemperatureContainer_Fields.ALARM_HIGH, false); } if (tempAlarmLow != -1) { owc.setMissionAlarm(OneWireContainer41.TEMPERATURE_CHANNEL, TemperatureContainer_Fields.ALARM_LOW, tempAlarmLow); owc.setMissionAlarmEnable(OneWireContainer41.TEMPERATURE_CHANNEL, TemperatureContainer_Fields.ALARM_LOW, true); } else { owc.setMissionAlarmEnable(OneWireContainer41.TEMPERATURE_CHANNEL, TemperatureContainer_Fields.ALARM_LOW, false); } if (dataAlarmHigh != -1) { owc.setMissionAlarm(OneWireContainer41.DATA_CHANNEL, ADContainer_Fields.ALARM_HIGH, dataAlarmHigh); owc.setMissionAlarmEnable(OneWireContainer41.DATA_CHANNEL, ADContainer_Fields.ALARM_HIGH, true); } else { owc.setMissionAlarmEnable(OneWireContainer41.DATA_CHANNEL, ADContainer_Fields.ALARM_HIGH, false); } if (dataAlarmLow != -1) { owc.setMissionAlarm(OneWireContainer41.DATA_CHANNEL, ADContainer_Fields.ALARM_LOW, dataAlarmLow); owc.setMissionAlarmEnable(OneWireContainer41.DATA_CHANNEL, ADContainer_Fields.ALARM_LOW, true); } else { owc.setMissionAlarmEnable(OneWireContainer41.DATA_CHANNEL, ADContainer_Fields.ALARM_LOW, false); } owc.StartUponTemperatureAlarmEnable = startUponTempAlarm; owc.startNewMission(sampleRate, startDelay, rollover, true, new bool[] { tempBytes > 0, dataBytes > 0 }); Debug.WriteLine("Mission Started"); } else { Debug.WriteLine("No DS1922/DS2422 device found"); } } catch (Exception e) { Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); } finally { if (adapter != null) { adapter.endExclusive(); } } }