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

USB-1608G CSharp (C#) External Trigger Auto Retrigger

Expand / Collapse
 

USB-1608G CSharp (C#) External Trigger Auto Retrigger


The following example demonstrates how to set the API to automatically re-arm the trigger after an acquisition has completed. This program uses the External Trigger input and once received, a fixed amount of data is collected, placed into the buffer and re-armed. 

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.

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 AnalogInAutoRetrigger
{
    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 int HALFBUFFSIZE = BUFFERSIZE / 2;
        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;
                }

                short[] chArray = new short[CHANCOUNT];
                Range[] chRange = new Range[CHANCOUNT];

                chArray[0] = 0;
                chArray[1] = 1;
                chArray[2] = 2;
                chArray[3] = 3;

                chRange[0] = Range.Bip10Volts;
                chRange[1] = Range.Bip10Volts;
                chRange[2] = Range.Bip10Volts;
                chRange[3] = Range.Bip10Volts;

                RetVal = daq.ALoadQueue(chArray, chRange, CHANCOUNT);

                ScanOptions MyScanOptions = ScanOptions.Background  |
                                            ScanOptions.ScaleData   |
                                            ScanOptions.ExtTrigger  |
                                            ScanOptions.Continuous  |
                                            ScanOptions.RetrigMode;

                //setup the acquisiton
                RetVal = daq.AInScan(   FIRSTCHANNEL,
                                        LASTCHANNEL,
                                        BUFFERSIZE,
                                        ref Rate,
                                        Range.Bip10Volts,
                                        buffer,
                                        MyScanOptions
                                    );
                IsError(RetVal);

                RetVal = daq.BoardConfig.SetAdRetrigCount( BUFFERSIZE );
                IsError(RetVal);

                RetVal = daq.SetTrigger(TriggerType.TrigPosEdge, 0, 0);
                IsError(RetVal);


                int Count = 0;
                int Index = 0;
                int LastCount = 0;
                short daqStatus;

                double[] theArray = new double[BUFFERSIZE];

                System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo();

                do{
                    Console.WriteLine("Waiting for External rising edge trigger.");
                    Console.WriteLine("Total Count = {0}",LastCount);

                    do
                    {   //spin here until the buffer is half full
                        System.Threading.Thread.Sleep(100);
                        RetVal = daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.AiFunction);
                        IsError(RetVal);

                        if (Console.KeyAvailable)
                        {
                            RetVal = daq.StopBackground(FunctionType.AiFunction);
                            MccService.WinBufFreeEx(buffer);
                            return;
                        }

                    } while( Count < BUFFERSIZE + LastCount );
                    LastCount += BUFFERSIZE;
                    RetVal = MccService.ScaledWinBufToArray(buffer, theArray, 0, BUFFERSIZE);
                    IsError(RetVal);

                    DisplayData(theArray, BUFFERSIZE);
                    

                } while (!Console.KeyAvailable);

                cki = Console.ReadKey();

                RetVal = daq.StopBackground(FunctionType.AiFunction);

                MccService.WinBufFreeEx(buffer);

                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 int IsError(ErrorInfo e)
        {
            if (e.Value != 0)
            {
                Console.WriteLine(e.Message);
                return 1;
            }
            return 0;
        }
        public static void WaitForKey()
        {
            Console.WriteLine("\nPress <SpaceBar> to continue...");

            System.ConsoleKeyInfo cki;
            do
            {
                cki = Console.ReadKey();
            } while (cki.Key != ConsoleKey.Spacebar);
        }
        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


AnalogInAutoRetrigger.zip AnalogInAutoRetrigger.zip (27.63 KB, 798 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50469

Last Modified:2/6/2014 11:48:21 AM

Article has been viewed 5,096 times.

Options