Extracting Data from Acquisition using CWDAQ in Microsoft Visual C++ 6.0
Primary Software: Measurement Studio>>Visual C++ Support
Primary Software Version: 6.0
Primary Software Fixed Version: N/A
Secondary Software: Measurement Studio>>Visual Basic Support
Problem: I'm using the CWDAQ controls in Microsoft Visual Studio 6.0 with the Classwizard generated wrappers. When doing analog input, I'm trying to extract the array of data from the
VARIANT FAR* pointer returned by one of the events. How should I do this?
Solution: The
VARIANT pointer returned contains a
SafeArray of floating point values obtained during the acquisition. These are ActiveX datatypes and there are a whole set of functions associated with extracting values from them. In this case, if you want to get the array of floats from the
SafeArray, here is how you would do it for the
CWAI::OnAcquiredDataCwai1 event:
void CAcquireNScansDlg::OnAcquiredDataCwai1(VARIANT FAR* ScaledData, VARIANT FAR* BinaryCodes)
{
SAFEARRAY FAR* psa = NULL;
HRESULT hr;
LONG cElements, lLBound, lUBound;
float HUGEP *pbstr;
//Check to see if ScaledData contains an array of 4 byte real numbers. Otherwise, return.
if (V_VT(ScaledData) != ( VT_ARRAY | VT_R4 ))
{
TRACE("Not the correct data type\n");return;
}
//Get a pointer to the SafeArray stored in the variant
psa = V_ARRAY(ScaledData);
//Check the number of dimensions in the safearray
TRACE("Number of channels: %d\n",SafeArrayGetDim(psa));
// Get array bounds.Assuming 1D array
//Get Low index of array. Safearrays always dont start with a 0 index
hr = SafeArrayGetLBound(psa, 1, &lLBound);
if (FAILED(hr)) return;
//Get upper bound
hr = SafeArrayGetUBound(psa, 1, &lUBound);
if (FAILED(hr)) return;
// Get a pointer to the elements of the array.
// and lock the memory
hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pbstr);
if (FAILED(hr))return;
//Get total number of elements
cElements = lUBound-lLBound+1;
//temp will be the array of floats
//that will store the array of data we extract from the safe array
//Remember to free it after you're done.
temp = (float*)calloc(cElements,sizeof(float) );
//Copy over each datapoint
for (int i = 0; i < cElements-1; i++)
{
temp[i] = pbstr[i];
}
//free the buffer after you finish with it
free(temp);
//Unlock the safearray data space
SafeArrayUnaccessData(psa);
}
This can be extended for obtaining arrays of other data types as well.
For more information about the ActiveX functions used, refer to the Microsoft Developer network link below.
Related Links: Conversion and Manipulation Functions
Attachments:
Report Date: 02/10/2004
Last Updated: 04/07/2006
Document ID: 369EO65B