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

USB-1608G CSharp (C#) External Trigger Input

Expand / Collapse

USB-1608G CSharp (C#) External Trigger Input

The following example demonstrates how to trigger using the External TTL input. Once triggered, the program continuously acquires data to buffer. 

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. 

Although not shown below, a few convenience functions were added such as IsError, GetBoardNum, DisplayData, CreateFileHeader and WaitForKey. To view all the functions download the complete Visual Studio 2008 project by extracting the zip file at the bottom. 

The IsError function checks the error number in the ErrorInfo object and if not zero displays the error message.  The GetBoardNum function searches for a device matching the identifying string and when found exits returning the board number assigned by InstaCal. The board number is used to get the controlling device object that has AInScan function. The CreateFileHeader writes acquisition information to the beginning of the output file. The Display data writes the data to the file and console screen. And WaitForKey does just that - waits for someone to press the spacebar.

The functionality is fairly simple, the acquisition waits for a rising edge TTL signal to appear on the External Trigger input and when received it continuously puts data into the buffer. To command the API to look at the External Trigger input use the Scan Options enumeration ExtTrigger. Once detected, the program proceeds to retrieve one half buffer at a time ping-ponging between reading the lower half and the upper in attempt to keep up with the incoming data. When sampling at high rates, care should be taken so that unread data is not overwritten.  

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;
using System.IO;
namespace AnalogInDigitalTrigger
    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";

        public static StreamWriter fStream;

        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);
                MccBoard daq = new MccDaq.MccBoard(BoardNum);

                daq.BoardConfig.GetNumAdChans(out DeviceChannels);

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

                IntPtr buffer = MccService.ScaledWinBufAllocEx(BUFFERSIZE);

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

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

                //specify ScanOptions.ExtTrigger to use the TRIG input 
                ScanOptions MyScanOptions = ScanOptions.Background  |
                                            ScanOptions.ScaleData   |
                                            ScanOptions.ExtTrigger  |

                //setup the acquisiton
                RetVal = daq.AInScan(   FIRSTCHANNEL,
                                        ref Rate,

                int Count = 0;
                int Index = 0;
                short daqStatus;
                bool ReadLower = true;

                double[] theArray = new double[BUFFERSIZE];

                fStream = new StreamWriter(@"C:\Users\Public\Documents\DataFile1608G.asc");
                CreateFileHeaders(chArray); //writes basic info to the beginning of the file

                Console.WriteLine("Waiting for External TTL trigger...Press any key to exit loop.");

                //Loop until key press
                    RetVal = daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.AiFunction);
                    if ((Index >= HALFBUFFSIZE) & ReadLower) //check for 50% more data
                        //get lower half of buffer - ScaledWinBufToArray returns engineering units
                        RetVal = MccService.ScaledWinBufToArray(buffer, theArray, 0, HALFBUFFSIZE);

                        DisplayData(theArray, HALFBUFFSIZE / CHANCOUNT);
                        ReadLower = false; //flag that controls the next read
                    else if ((Index < HALFBUFFSIZE) & !ReadLower)
                        //get the upper half  - ScaledWinBufToArray returns engineering units
                        RetVal = MccService.ScaledWinBufToArray(buffer, theArray, HALFBUFFSIZE, HALFBUFFSIZE);

                        DisplayData(theArray, HALFBUFFSIZE / CHANCOUNT);
                        ReadLower = true;//flag that controls the next read

                } while (!Console.KeyAvailable);

                RetVal = daq.StopBackground(FunctionType.AiFunction);




Rate this Article:

Attachments (29.74 KB, 720 views)

Add Your Comments

For comments email

Article ID: 50468

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

Article has been viewed 5,589 times.