How Do I Dynamically Choose the Size of an Array to be Passed to a NI-DAQmx C Function?Primary Software: LabWindows/CVI Development Systems>>Full Development SystemPrimary Software Version: 8.0 Primary Software Fixed Version: N/A Secondary Software: LabWindows/CVI Development Systems>>Base Package, Measurement Studio>>Visual Basic Support, Measurement Studio>>Visual C++ Support, Measurement Studio>>Professional Edition (Base Package), Measurement Studio>>Enterprise Edition (Full Development System), Measurement Studio>>Standard Edition (Starter Kit), Measurement Studio>>.NET Support, Measurement Studio>>MS for Measurement Computing
Problem: Several NI-DAQmx C functions return data into an array passed by reference as one of the function parameters. I want to use the most efficient size of array, but I don't know how many elements the function will return. How do I find out how big of an array NI-DAQmx will return? Solution: Passing a NULL and a 0 as the array and number of samples, respectively, causes the function to return the number of samples used in the array instead of returning an error. An example function call would be: DAQmxErrChk (numberOfCoefficients=DAQmxGetAIDevScalingCoeff(taskHandle, channels[0], NULL, 0)); Then you can allocate memory for the coefficient array: // Allocate Memory for coefficient array coefficients=(float64*)(malloc(sizeof(float64)*numberOfCoefficients*numberOfChannels)); // Check for allocation error if (coefficients==NULL) goto Error; You can now retrieve the coefficients: // Get Coefficients for each channel for (uInt32 i=0; i < numberOfChannels; i++) { DAQmxErrChk (DAQmxGetAIDevScalingCoeff(taskHandle, channels[i], coefficients+(numberOfCoefficients*i), numberOfCoefficients)); } Now that you know the size of the array, you can call the function again using the appropriately-sized array to get the data. Note: To find a list of functions that use this type of array, open the C Reference Help that is installed with NI-DAQmx (typically as C:\Program Files\National Instruments\NI-DAQ\docs\cdaqmx.chm) and search for "arraySizeInSamples". Related Links: NI LabWindows/CVI Discussion forum post with detailed answer (code posted on 5/2/2008) Attachments:
Report Date: 09/24/2008 Last Updated: 09/29/2008 Document ID: 4PNAOTD4 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
