USB-1608G CSharp (C#) single point measurement


The following example demonstrates how to acquire a fixed number of samples. The complete project is attached to the article and was created with Visual Studio 2008 using CSharp. Compiling and execution is quick as there is minimal Windows overhead and associated code. 

Disclaimer:
The attached Code or Example is provided As Is.  It has not been tested or validated as a product, for use in a deployed application or system, or for use in hazardous environments.  You assume all risks for use of the Code or Example.

using System;
using MccDaq; //added to project references

namespace AInSinglePoint
{
    class Program
    {
        public const string DEVICE = "1608G";

  
        static void Main(string[] args)
        {

            MccDaq.ErrorInfo RetVal;

            int     BoardNum = 0;
            int     DeviceChannels = 0;
            short   DataVal;
            Single  EngrUnits;

            BoardNum = GetBoardNum(DEVICE);

            if (BoardNum == -1)
                Console.WriteLine("No USB-{0} detected!", DEVICE);
            else
            {
                MccDaq.MccBoard daq = new MccDaq.MccBoard(BoardNum);

                daq.BoardConfig.GetNumAdChans(out DeviceChannels);

                if (DeviceChannels > 8)
                    Console.WriteLine("Single-Ended Channels");
                else
                    Console.WriteLine("Differentially-Ended Channels");

                RetVal = daq.AIn(0, Range.Bip10Volts, out DataVal);

                if (RetVal.Value > 0)
                    Console.WriteLine(RetVal.Message);

                RetVal = daq.ToEngUnits(Range.Bip10Volts, DataVal, out EngrUnits);

                if (RetVal.Value > 0)
                    Console.WriteLine(RetVal.Message);

                //diy (do it yourself) conversion = ( 2 x range / 65536 ) * ( 32768 + A/D value )
                //Single res = 20.0f / 65536;
                //Single myVal = (32768 + DataVal) * res;

                Console.WriteLine("Channel 0 =\t{0} counts", DataVal.ToString());
                Console.WriteLine("Channel 0 =\t{0} enginnering units", EngrUnits.ToString("0.000"));

                WaitForKey();
            }
        }
        public static int GetBoardNum(string dev)
        {
            for (int BoardNum = 0; BoardNum < 99; BoardNum++)
            {
                MccDaq.MccBoard daq = new MccDaq.MccBoard(BoardNum);
                if (daq.BoardName.Contains(dev))
                {
                    Console.WriteLine("USB-{0} board number = {1}", dev, BoardNum.ToString());
                    daq.FlashLED();
                    return BoardNum;
                }
            }
            return -1;
        }
        public static void WaitForKey()
        {
            Console.WriteLine("\nPress <SpaceBar> to continue...");

            System.ConsoleKeyInfo cki;
            do
            {
                cki = Console.ReadKey();
            } while (cki.Key != ConsoleKey.Spacebar);
        }
    }
}




Posted 5/17/2013 5:14:37 AM by Administrator
https://kb.mccdaq.com/KnowledgebaseArticle50455.aspx