Example #1
0
    /// <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();
            }
        }
    }
Example #2
0
        private void StartNewMission()
        {
            int tempBytes = 0;
            int humdBytes = 0;

            if (enableTemperatureSampling.Checked)
            {
                tempBytes++;
                if (temperatureResolution.Text.IndexOf("High") >= 0)
                {
                    tempBytes++;
                }
            }

            if (enableHumiditySampling.Checked)
            {
                humdBytes++;
                if (humidityResolution.Text.IndexOf("High") >= 0)
                {
                    humdBytes++;
                }
            }

            if (tempBytes == 1)
            {
                owc41.setMissionResolution(0, owc41.getMissionResolutions(0)[0]);
            }
            else
            {
                owc41.setMissionResolution(0, owc41.getMissionResolutions(0)[1]);
            }
            if (humdBytes == 1)
            {
                owc41.setMissionResolution(1, owc41.getMissionResolutions(1)[0]);
            }
            else
            {
                owc41.setMissionResolution(1, owc41.getMissionResolutions(1)[1]);
            }


            if (enableTempHighAlarm.Checked)
            {
                owc41.setMissionAlarm(
                    OneWireContainer41.TEMPERATURE_CHANNEL,
                    1, //TemperatureContainer.ALARM_HIGH,
                    (double)temperatureHighAlarm.Value);
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.TEMPERATURE_CHANNEL,
                    1, //TemperatureContainer.ALARM_HIGH,
                    true);
            }
            else
            {
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.TEMPERATURE_CHANNEL,
                    1, //TemperatureContainer.ALARM_HIGH,
                    false);
            }

            if (enableTempLowAlarm.Checked)
            {
                owc41.setMissionAlarm(
                    OneWireContainer41.TEMPERATURE_CHANNEL,
                    0, //TemperatureContainer.ALARM_LOW,
                    (double)temperatureLowAlarm.Value);
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.TEMPERATURE_CHANNEL,
                    0, //TemperatureContainer.ALARM_LOW,
                    true);
            }
            else
            {
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.TEMPERATURE_CHANNEL,
                    0, //TemperatureContainer.ALARM_LOW,
                    false);
            }

            if (enableHumdHighAlarm.Checked)
            {
                owc41.setMissionAlarm(
                    OneWireContainer41.DATA_CHANNEL,
                    1, //ADContainer.ALARM_HIGH,
                    (double)humidityHighAlarm.Value);
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.DATA_CHANNEL,
                    1, //ADContainer.ALARM_HIGH,
                    true);
            }
            else
            {
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.DATA_CHANNEL,
                    1, //ADContainer.ALARM_HIGH,
                    false);
            }

            if (enableHumdLowAlarm.Checked)
            {
                owc41.setMissionAlarm(
                    OneWireContainer41.DATA_CHANNEL,
                    0, //ADContainer.ALARM_LOW,
                    (double)humidityLowAlarm.Value);
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.DATA_CHANNEL,
                    0, //ADContainer.ALARM_LOW,
                    true);
            }
            else
            {
                owc41.setMissionAlarmEnable(
                    OneWireContainer41.DATA_CHANNEL,
                    0, //ADContainer.ALARM_LOW,
                    false);
            }

            owc41.setStartUponTemperatureAlarmEnable(suta.Checked);

            owc41.startNewMission((int)sampleRate.Value, (int)startDelay.Value,
                                  enableRolloverCheckBox.Checked, syncClockCheckBox.Checked,
                                  new bool[] { tempBytes > 0, humdBytes > 0 });

            counter++;
        }