USB-1608G CSharp (C#) Analog Output Scan

The following example demonstrates how to output an array of data to the analog output pin. 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 Visual Studio 9.0 project is attached to this article.

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.

The functionality is fairly simple, one channel is setup for input for the analog output signal. Connecting a wire from AOUT 0 to AIN 0 will allow the program to measure the output sinewave. The program calculates one complete cycle of a sinewave storing each point in an array. Immediately after the data is sent to the AOUT pin, the program reads AIN0 in an effort to capture the waveform.

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 AnalogOutputScan
{
    class Program
    {
        public const int BLOCKSIZE      = 50;
        public const int CHANCOUNT      = 1;
        public const int FIRSTCHANNEL   = 0;
        public const int LASTCHANNEL    = 0;
        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 Rate = FREQ;

            int BoardNum = GetBoardNum(DEVICE);

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

                IntPtr OutBuf = MccService.ScaledWinBufAllocEx(BUFFERSIZE);

                IntPtr buffer = MccService.ScaledWinBufAllocEx(BUFFERSIZE);

                if( (OutBuf == IntPtr.Zero) || (buffer == IntPtr.Zero) )
                {
                    Console.WriteLine("Insufficient Memory");
                    return;
                }


                double angle = 6.28318 / BUFFERSIZE;
                double value;
                int index = 0;
                ushort[] dataArray = new ushort[BUFFERSIZE];

                for (int j = 0; j < CHANCOUNT; j++)
                {
                    for (int i = 0; i < BUFFERSIZE; i++)
                    {
                        value = (System.Math.Sin(angle * i) +1 ) * 32767;
                        dataArray[index++] = System.Convert.ToUInt16(value);
                    }
                }

                MccService.WinArrayToBuf(dataArray, OutBuf, 0, BUFFERSIZE);

                Console.WriteLine("\nConnect analog output 0 to analog input 0...");

                WaitForKey();



                RetVal = daq.AOutScan(  0, 
                                        0, 
                                        BLOCKSIZE, 
                                        ref Rate, 
                                        Range.Bip10Volts, 
                                        OutBuf, 
                                        ScanOptions.Background
                                      );
                IsError(RetVal);

                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);

                RetVal = daq.StopBackground( FunctionType.AiFunction );
                IsError(RetVal);

                RetVal = daq.StopBackground( FunctionType.AoFunction );
                IsError(RetVal);


                MccService.WinBufFreeEx(buffer);
                MccService.WinBufFreeEx(OutBuf);

                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");

            }
        }
    }
}

Attachments
AnalogOutputScan.zip