USB-1208LS Continuous Background Scanning Example

The example below demonstrates how to read a buffer that is being filled continuously with live data. It uses ALoadQueue(..), AInScan(…) and GetStatus(…) to read a group of analog input channels. Data is written to an ASCII text file that can be viewed and verified using Excel, DASYLab, NI LabVIEW, MatLab® and other programs capable of text import.

The functionality is as follows, the acquisition puts data into a continuously looping buffer and the program retrieves it one half buffer at a time. The continuous mode is set using the Scan Options enumeration Background and Continuous. Data is collected by ping-pongs between the lower half then the upper looping fast enough to keep up.

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.

If starting a new project using Visual Studio, you must first 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.

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.

       public const int NUMOFBLKS      = 32;
        public const int PACKETSIZE     = 32;
        public const int CHANCOUNT      = 4;
        public const int FIRSTCHANNEL   = 0;
        public const int LASTCHANNEL    = 3;
        public const int FREQ           = 100;

  
        //This program works with several devices, enable the Device string for your product
 
        public const string DEVICE = "USB-1208LS";

        public static StreamWriter fStream;

        public static MccBoard daq;

        static void Main(string[] args)
        {


            int     BoardNum        = 0;
            int     Rate            = FREQ;
            bool    ReadLower       = true;

            Console.WriteLine("\nContinuous Background Scanning Example\n");

            Console.WriteLine("\nLocating board...\n");

            BoardNum = GetBoardNum(DEVICE);


            if (BoardNum == -1)
            {
                Console.WriteLine("No {0} detected!", DEVICE);
                WaitForKey();
                return;
            }


            int BUFFERSIZE = PACKETSIZE * CHANCOUNT * NUMOFBLKS; 
            int HALFBUFFSIZE = BUFFERSIZE / 2;

            daq = new MccDaq.MccBoard( BoardNum );
           
            IntPtr buffer = MccService.WinBufAllocEx( BUFFERSIZE );

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

            short[] chArray = new short[CHANCOUNT]; //configuration array for channel numbers
            Range[] chRange = new Range[CHANCOUNT]; //configuration array for input ranges

            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;

            IsError(daq.ALoadQueue(chArray, chRange, CHANCOUNT));

            ScanOptions options  = ScanOptions.Background|ScanOptions.Continuous|ScanOptions.ConvertData;

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


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

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

            ushort[] ushortArray = new ushort[BUFFERSIZE];

            //Loop until key press
            do{
                IsError(daq.GetStatus( out daqStatus, out Count, out Index, FunctionType.AiFunction ));
                Console.Write("Count={0}, Index={1}\r", Count, Index);

                if ((Index >= HALFBUFFSIZE) & ReadLower) //check for 50% more data
                {
                    //get lower half of buffer
                    IsError(MccService.WinBufToArray(buffer, ushortArray, 0, HALFBUFFSIZE));

                    DisplayData(ushortArray, HALFBUFFSIZE/CHANCOUNT);
                    ReadLower = false; //flag that controls the next read
                }
                else if ((Index < HALFBUFFSIZE) & !ReadLower)
                {
                    //get the upper half
                    IsError( MccService.WinBufToArray(buffer, ushortArray, HALFBUFFSIZE, HALFBUFFSIZE));

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


            } while ( !Console.KeyAvailable );
            ConsoleKeyInfo cki = Console.ReadKey();

            

            //flush any buffered data out to disk
            fStream.Close();

            //stop the  acquisition
            IsError(daq.StopBackground(FunctionType.AiFunction));

            //free up memory
            MccService.WinBufFreeEx(buffer);

            WaitForKey();
            
        }

Attachments
AinScanBackgroundContinuousScan.zip