USB-1408FS Step Response CSharp Example

The example below demonstrates a voltage step response. It uses VOut(...), AInScan(...) and GetStatus(...) to read a group of analog input channels after updating the voltage on AOUT0. The program reads acquisition setup details and voltage output setups from a comma delimited file (Setup.csv).  It then reads a number of samples (PacketSize * NumOfBlocks) and averages them down to one value which is written to an ASCII text file. This file can be viewed and verified using NotePad, 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. The attachment also contains a sample step.txt file that defines some acquisition parameters and the voltage steps. The maximum number of steps that can be defined is 90.

For proper execution, place the executable file in a folder with the Setup.csv file. 

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 string DEVICE = "USB-1408FS";

        public static int CHANCOUNT = 2;

        public static int FIRSTCHANNEL = 0;

        public static int LASTCHANNEL = 1;

        public static int NUMOFBLKS = 20;   //number packets to get

        public static int BEGINSTEPS = 9;   //the first ten lines in the input file are setup instructions

        public static int FREQ = 1000;

        public static int DELAY = 100;      //settling time delay in milliseconds after each voltage step befor voltage read

        public static StreamWriter fStream; //output file stream object
        public static MccBoard daq;

        public static int PACKETSIZE;

        public static float zero    = 0.0f;

        public static int Steps    = 0;

        public static int RANGE;

        public static string infile = "Setup.csv";

        public static string outfile = "DataFile.asc";

        static void Main(string[] args)

            int BoardNum = 0;
            int Rate = FREQ;
            int i;
            string[] steps = new string[100];

            Console.WriteLine("\nExample Stimulus Response Program");

            if (File.Exists(infile) == false)
                Console.WriteLine("\nMissing Setup.csv file!");

            Console.WriteLine("\nReading voltage steps from Setup.csv");

            StreamReader ReadObj = new StreamReader(@infile);
            //string reads are split in to two
            string[] temp = new string[2];
            string tempx;
            while (!ReadObj.EndOfStream)
                tempx = ReadObj.ReadLine();
                temp = tempx.Split(',');
                steps[i++] = temp[1];
            Steps = i;
            Console.WriteLine("\nNumber of voltage steps = {0}", Steps-BEGINSTEPS);

            FREQ = Convert.ToInt16(steps[0]); //input file line 1 == sample rate

            NUMOFBLKS = Convert.ToInt16(steps[1]);//input file line 2 == number of packets to get

            DELAY = Convert.ToInt16(steps[2]);//input file line 3 == delay between voltage out and voltage read

            RANGE = Convert.ToInt16(steps[3]); //input file line 4 == input voltage range

            FIRSTCHANNEL = Convert.ToInt16(steps[4]); //input file line 5 == first channel in scan list

            LASTCHANNEL = Convert.ToInt16(steps[5]); //input file line 6 == last channel in scan list

            CHANCOUNT = LASTCHANNEL - FIRSTCHANNEL + 1; //channel count

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

            //get the InstaCal board number containing DEVICE string
            BoardNum = GetBoardNum(DEVICE);

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

            //calculate buffer size

            //create device object
            daq = new MccDaq.MccBoard(BoardNum);

            //allocate buffer
            IntPtr buffer = MccService.WinBufAllocEx(BUFFERSIZE);

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

            //create text file to hold data
            fStream = new StreamWriter(@outfile);

            //writes Acquisition info to file header
            Console.WriteLine("\nPress any key to exit...\n");
            for (i = BEGINSTEPS; i < Steps; i++)
                IsError(daq.VOut(0, Range.Uni4Volts, System.Convert.ToSingle(steps[i]), VOutOptions.Default));

                //call command to execute acquisition
                IsError(daq.AInScan(    FIRSTCHANNEL,
                                        ref Rate,
                                        ScanOptions.BlockIo | ScanOptions.ConvertData

                //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(daq.VOut(0, Range.Uni4Volts, zero, VOutOptions.Default));


Posted 7/25/2014 10:44:27 AM by Administrator