Example #1
0
    // read A/D from device
    internal static void getVoltage(ADContainer adc, bool[] channel, int trial)
    {
        byte[]   state;
        double[] curVoltage = new double [channel.Length];

        if (!chanSelected)
        {
            Debug.WriteLine("No channel selected yet. Cannot get voltage reading.");

            return;
        }

        while (trial-- > 0)
        {
            state = adc.readDevice();

            if (adc.canADMultiChannelRead())
            {
                // do all channels together
                adc.doADConvert(channel, state);

                curVoltage = adc.getADVoltage(state);
            }
            else
            {
                // do one channel at a time;
                for (int i = 0; i < maxNumChan; i++)
                {
                    if (channel [i])
                    {
                        adc.doADConvert(i, state);

                        curVoltage [i] = adc.getADVoltage(i, state);
                    }
                }
            }

            Debug.Write("  Voltage Reading:");

            for (int i = 0; i < maxNumChan; i++)
            {
                if (channel [i])         // show value up to 2 decimal places
                {
                    Debug.Write(" Ch" + i + " = " + ((int)(curVoltage [i] * 10000)) / 10000.0 + ADUnit);
                }
            }

            Debug.WriteLine("");
        }
    }
Example #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);
    }