USB-1208LS Foreground (finite) Scanning Example

The following example demonstrates how to acquire a fixed number of samples. It uses 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.

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. To view the complete program project download the attachment below.

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      = 8;
        public const int PACKETSIZE     = 32;
        public const int CHANCOUNT      = 1;
        public const int FIRSTCHANNEL   = 0;
        public const int LASTCHANNEL    = 0;
        public const int FREQ           = 1000;


   
        public const string DEVICE = "1208LS";
 
        public static StreamWriter fStream; 

        public static MccBoard daq;


        static void Main(string[] args)
        {

            int BoardNum = 0;
            int Rate = FREQ;
            Console.WriteLine("\nAnalog Input Foreground 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 * NUMOFBLKS * CHANCOUNT;
 
            daq = new MccDaq.MccBoard(BoardNum);
 
            IntPtr buffer = MccService.WinBufAllocEx(BUFFERSIZE);

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

            //create text file to hold data
            fStream = new StreamWriter(@"C:\Users\Public\Documents\DataFile.asc");

            //writes Acquisition info to file header
            CreateFileHeaders();


            ScanOptions options = ScanOptions.ConvertData;

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


            //declare ushort array to hold data returned by WinBufToArray
            ushort[] ushortArray = new ushort[BUFFERSIZE];

            //fetch data from driver
            IsError( MccService.WinBufToArray( buffer, ushortArray, 0, BUFFERSIZE ) );

            //Write data to console and to file
            DisplayData(ushortArray, BUFFERSIZE / CHANCOUNT);

            IsError(MccService.WinBufFreeEx(buffer));

            WaitForKey();
            
        }

Attachments
AInScanForeground.zip