Ejemplo n.º 1
0
    /// <summary>
    /// Method main
    ///
    /// </summary>
    /// <param name="args">
    ///  </param>
    public static void Main1(string[] args)
    {
        OneWireContainer owc = null;
        ADContainer      adc = null;

        Stream stream = loadResourceFile("ADcontainerDemo.input.txt");

        dis = new StreamReader(stream);

        // find and initialize the first OneWireContainer with
        // ADContainer interface
        owc = initContainer();

        if (!(owc is ADContainer))
        {
            cleanup();
            Debug.WriteLine("*************************************************************************");
            Debug.WriteLine("No ADContainer found. Exit program.");
            Debug.WriteLine("");
            return;
        }
        else
        {
            adc = (ADContainer)owc;
        }

        maxNumChan = adc.NumberADChannels;

        // array to determine whether a specific channel has been selected
        bool[] channel = new bool [maxNumChan];

        for (int i = 0; i < maxNumChan; i++)   // default, no channel selected
        {
            channel [i] = false;
        }

        byte[]   state     = null; // data from device
        double[] ranges    = null; // A/D ranges
        double   alarmLow  = 0;    // alarm low value
        double   alarmHigh = 0;    // alarm high value
        bool     alarming;

        // temporary storage for user input
        int    inputInt      = 0;
        double inputDouble   = 0.0;
        double inputLow      = 0.0;
        double inputHigh     = 0.0;
        string inputString   = null;
        bool   moreInput     = true;
        int    curMenuChoice = 0;

        initMenu();

        while (true)
        {
            curMenuChoice = getMenuChoice(hashMainMenu, mainMenuItemCount);

            try
            {
                switch (curMenuChoice)
                {
                case 0:             // Select Channel
                    Debug.WriteLine("*************************************************************************");
                    Debug.WriteLine("All previously selectd channels have been cleared.");

                    state = adc.readDevice();

                    for (int i = 0; i < maxNumChan; i++)
                    {
                        // clear all channel selection
                        channel [i] = false;

                        if (adc.hasADAlarms())
                        {
                            // disable alarms
                            adc.setADAlarmEnable(i, ADContainer_Fields.ALARM_LOW, false, state);
                            adc.setADAlarmEnable(i, ADContainer_Fields.ALARM_HIGH, false, state);
                        }
                    }

                    adc.writeDevice(state);

                    chanSelected = false;
                    state        = adc.readDevice();

                    int count = 1;

                    moreInput = true;

                    while (moreInput && (count <= maxNumChan))
                    {
                        Debug.Write("Please enter channel # " + count + " ( Enter -1 if no more): ");

                        inputInt = (int)Number;

                        if (inputInt == -1)
                        {
                            moreInput = false;
                        }
                        else
                        {
                            if (isChannelValid(inputInt))
                            {
                                channel [inputInt] = true;

                                count++;

                                chanSelected = true;

                                if (adc.hasADAlarms())
                                {
                                    // enable alarms
                                    adc.setADAlarmEnable(inputInt, ADContainer_Fields.ALARM_LOW, true, state);
                                    adc.setADAlarmEnable(inputInt, ADContainer_Fields.ALARM_HIGH, true, state);
                                }
                            }
                        }
                    }               // while (moreInput && (count <= maxNumChan))

                    adc.writeDevice(state);
                    Debug.Write("  Channels to monitor = ");

                    if (count == 1)
                    {
                        Debug.WriteLine("NONE");
                    }
                    else
                    {
                        for (int i = 0; i < maxNumChan; i++)
                        {
                            if (channel [i])
                            {
                                Debug.Write(i + " ");
                            }
                        }

                        Debug.WriteLine("");
                    }
                    break;

                case 1:             // Get A/D Once
                    getVoltage(adc, channel, 1);
                    break;

                case 2:             // Get A/D Multiple Time
                    Debug.Write("Please enter number of times: ");

                    inputInt = (int)Number;

                    getVoltage(adc, channel, inputInt);
                    break;

                case 3:             // Get A/D ranges
                    if (!chanSelected)
                    {
                        Debug.WriteLine("No channel selected yet. Cannot get A/D ranges.");
                    }
                    else
                    {
                        state = adc.readDevice();

                        for (int i = 0; i < maxNumChan; i++)
                        {
                            if (channel [i])
                            {
                                ranges = adc.getADRanges(i);

                                Debug.Write("Ch " + i + " - Available: " + ranges [0] + ADUnit);

                                for (int j = 1; j < ranges.Length; j++)
                                {
                                    Debug.Write(", " + ranges [j] + ADUnit);
                                }

                                Debug.WriteLine(". Current: " + adc.getADRange(i, state) + ADUnit);
                            }
                        }
                    }
                    break;

                case 4:             // Set A/D ranges
                    Debug.WriteLine("*************************************************************************");

                    state     = adc.readDevice();
                    moreInput = true;

                    while (moreInput)
                    {
                        Debug.Write("Please enter channel number: ");

                        inputInt = (int)Number;

                        if (isChannelValid(inputInt))
                        {
                            Debug.Write("Please enter range value: ");

                            inputDouble = Number;

                            adc.setADRange(inputInt, inputDouble, state);
                            adc.writeDevice(state);

                            state = adc.readDevice();

                            Debug.WriteLine("  Ch" + inputInt + " A/D Ranges set to: " + adc.getADRange(inputInt, state));
                            Debug.Write("Set more A/D ranges (Y/N)? ");

                            inputString = dis.ReadLine();

                            if (!inputString.Trim().ToUpper().Equals("Y"))
                            {
                                moreInput = false;
                            }
                        }
                    }               // while(moreInput)
                    break;

                case 5:             // Get High and Low Alarms
                    if (!adc.hasADAlarms())
                    {
                        Debug.WriteLine("A/D alarms not supported");
                    }
                    else if (!chanSelected)
                    {
                        Debug.WriteLine("No channel selected yet. Cannot get high and low alarms.");
                    }
                    else
                    {
                        state = adc.readDevice();

                        for (int i = 0; i < maxNumChan; i++)
                        {
                            if (channel [i])
                            {
                                alarmLow  = adc.getADAlarm(i, ADContainer_Fields.ALARM_LOW, state);
                                alarmHigh = adc.getADAlarm(i, ADContainer_Fields.ALARM_HIGH, state);

                                // show results up to 2 decimal places
                                Debug.WriteLine("Ch " + i + " Alarm: High = " + ((int)(alarmHigh * 100)) / 100.0 + ADUnit + ", Low = " + ((int)(alarmLow * 100)) / 100.0 + ADUnit);
                            }
                        }                  // for
                    }
                    break;

                case 6:             // Set High and Low Alarms
                    if (!adc.hasADAlarms())
                    {
                        Debug.WriteLine("A/D alarms not supported");
                    }
                    else
                    {
                        Debug.WriteLine("*************************************************************************");

                        state     = adc.readDevice();
                        moreInput = true;

                        while (moreInput)
                        {
                            Debug.Write("Please enter channel number: ");

                            inputInt = (int)Number;

                            if (isChannelValid(inputInt))
                            {
                                bool inputValid = false;

                                while (!inputValid)
                                {
                                    Debug.Write("Please enter alarm high value: ");

                                    inputHigh = Number;

                                    if (inputHigh > adc.getADRange(inputInt, state))
                                    {
                                        Debug.WriteLine("Current A/D range is: " + adc.getADRange(inputInt, state) + ADUnit + ". Invalid alarm high value.");
                                    }
                                    else
                                    {
                                        inputValid = true;
                                    }
                                }

                                Debug.Write("Please enter alarm low value: ");

                                inputLow = Number;

                                adc.setADAlarm(inputInt, ADContainer_Fields.ALARM_LOW, inputLow, state);
                                adc.setADAlarm(inputInt, ADContainer_Fields.ALARM_HIGH, inputHigh, state);
                                adc.writeDevice(state);

                                state = adc.readDevice();

                                // show results up to 2 decimal places
                                Debug.WriteLine("  Set Ch" + inputInt + " Alarm: High = " + ((int)(adc.getADAlarm(inputInt, ADContainer_Fields.ALARM_HIGH, state) * 100)) / 100.0 + ADUnit + ", Low = " + ((int)(adc.getADAlarm(inputInt, ADContainer_Fields.ALARM_LOW, state) * 100)) / 100.0 + ADUnit);
                                Debug.Write("Set more A/D alarms (Y/N)? ");

                                inputString = dis.ReadLine();

                                if (!inputString.Trim().ToUpper().Equals("Y"))
                                {
                                    moreInput = false;
                                }
                            }
                        }                  // while(moreInput)
                    }
                    break;

                case 7:             // hasADAlarmed
                    if (!adc.hasADAlarms())
                    {
                        Debug.WriteLine("A/D alarms not supported");
                    }
                    else
                    {
                        alarming = owc.Alarming;

                        if (alarming)
                        {
                            Debug.Write("  Alarms: ");

                            state = adc.readDevice();

                            for (int i = 0; i < maxNumChan; i++)
                            {
                                if (channel [i])
                                {
                                    if (adc.hasADAlarmed(i, ADContainer_Fields.ALARM_HIGH, state))
                                    {
                                        Debug.Write("Ch" + i + " alarmed high; ");
                                    }

                                    if (adc.hasADAlarmed(i, ADContainer_Fields.ALARM_LOW, state))
                                    {
                                        Debug.Write("Ch" + i + " alarmed low; ");
                                    }
                                }
                            }

                            Debug.WriteLine("");
                        }
                        else
                        {
                            Debug.WriteLine("  Not Alarming");
                        }
                    }
                    break;

                case 8:
                    cleanup();
                    return;
                }
            }
            catch (Exception e)
            {
                printException(e);
            }
        }   // while
    }
Ejemplo n.º 2
0
    // find the first OneWireContainer with ADContainer interface
    // if found, initialize the container
    internal static OneWireContainer initContainer()
    {
        byte[]           state = null;
        OneWireContainer owc   = null;
        ADContainer      adc   = 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 ADCotainer interface
                if (owc is ADContainer)
                {
                    adc = (ADContainer)owc;

                    // access One Wire device
                    state = adc.readDevice();

                    double[] range      = null;
                    double[] resolution = null;

                    // set resolution
                    for (int channel = 0; channel < adc.NumberADChannels; channel++)
                    {
                        range      = adc.getADRanges(channel);
                        resolution = adc.getADResolutions(channel, range [0]);

                        // set to largest range
                        adc.setADRange(channel, range [0], state);

                        // set to highest resolution
                        adc.setADResolution(channel, resolution [resolution.Length - 1], state);

                        if (adc.hasADAlarms())
                        {
                            // disable all alarms
                            adc.setADAlarmEnable(channel, ADContainer_Fields.ALARM_LOW, false, state);
                            adc.setADAlarmEnable(channel, ADContainer_Fields.ALARM_HIGH, false, state);
                        }
                    }

                    adc.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 Number of Channels: " + adc.NumberADChannels);
                    Debug.WriteLine("* 1-Wire Device Can Read MultiChannels: " + adc.canADMultiChannelRead());
                    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);
    }