Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

USB-1608G CSharp (C#) Finite One Shot Acquisition

Expand / Collapse
 

USB-1608G CSharp (C#) Finite One Shot Acquisition


The following example demonstrates how to acquire a fixed number of samples. 

To access the API, add a reference to the MccDaq object. Adding the reference is usually accomplished by right clicking the Project [under the Project Explorer] and selecting Add Reference. 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. 

To make the code more readable a few convenience functions were added such as IsError, GetBoardNum, DisplayData and WaitForKey. The IsError function checks the error number in the ErrorInfo object and if not zero displays the error message.  The GetBoardNum function begins reading the Device strings from each number location and as soon as it finds one that contains the identifying string it exits. The Display data merely displays the data in column form and WaitForKey does just that - waits for someone to press the spacebar.

This program does not use the ScanOptions.Background enumeration and because of this the acquisition will shut  down after the number of samples have been acquired. Generally, this type of acquisition sizes the buffer to hold all the samples. However, if the acquisition size is large enough that it makes one buffer impractical then it may be better to adapt the background example. 

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;

namespace AInScanForeground
{
    class Program
    {
        public const int BLOCKSIZE      = 50;
        public const int CHANCOUNT      = 4;
        public const int FIRSTCHANNEL   = 0;
        public const int LASTCHANNEL    = 3;
        public const int FREQ           = 100;
        public const int BUFFERSIZE     = BLOCKSIZE * CHANCOUNT;
        public const string DEVICE      = "1608G";

 
      
        static void Main(string[] args)
        {

            MccDaq.ErrorInfo RetVal;

            int BoardNum        = 0;
            int DeviceChannels  = 0;
            int Rate            = FREQ;

    
            BoardNum = GetBoardNum(DEVICE);

            if( BoardNum == -1 )
                Console.WriteLine("No USB-{0} detected!", DEVICE);
            else
            {
                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" );


                IntPtr buffer = MccService.ScaledWinBufAllocEx( BUFFERSIZE );

                if( buffer == IntPtr.Zero )
                {
                    Console.WriteLine( "Bad Handle" );
                    return;
                }

                RetVal = daq.AInScan(   FIRSTCHANNEL, 
                                        LASTCHANNEL, 
                                        BUFFERSIZE, 
                                        ref Rate, 
                                        Range.Bip10Volts, 
                                        buffer, 
                                        ScanOptions.ScaleData
                                    );
                IsError(RetVal);

                double[] theArray = new double[ BUFFERSIZE ];

                RetVal = MccService.ScaledWinBufToArray( buffer, theArray, 0, BUFFERSIZE );
                IsError(RetVal);

                MccService.WinBufFreeEx( buffer );

                DisplayData(theArray, BUFFERSIZE);

                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);
        }
        public static int IsError(ErrorInfo e)
        {
            if (e.Value != 0)
            {
                Console.WriteLine(e.Message);
                return 1;
            }
            return 0;
        }
        public static void DisplayData(double[] datArray, int Count)
        {

            int i = 0;

            for (int row = 0; row < Count / CHANCOUNT; row++)
            {
                for (int c = 0; c < CHANCOUNT; c++)
                    Console.Write("{0}\t", datArray[i++].ToString("0.0000").PadLeft(10));
                Console.Write("\r\n");

            }
        }
    }
}


Rate this Article:

Attachments


AInScanForeground.zip AInScanForeground.zip (26.07 KB, 630 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50454

Last Modified:2/6/2014 11:47:44 AM

Article has been viewed 3,760 times.

Options