Archived: Acquiring, Analyzing, and Presenting Data with Measurement Studio for Visual C++

Publish Date: Apr 30, 2018 | 21 Ratings | 2.62 out of 5 | Print | Submit your review

Overview

This document has been archived and is no longer updated by National Instruments.

This tutorial includes procedures for using the Measurement Studio for Visual C++ Full Development System to acquire, analyze, and present data.

You will need to download the Acquiring, Analyzing, and Presenting Data with Measurement Studio for Visual C++ example program.

When you are done building the Simple Measurement program, perform the following steps to use the program:

    • Select the number of data points you want to analyze and then click the Acquire button. The program simulates an acquisition of the specified number of points and presents the raw data on the Acquired Data graph.
    • Select the analysis function you want to perform on the raw data and click the Analyze button. The program analyzes the data and presents it on the Analyzed Data graph.
    • Click the Reset Graphs button to clear the graphs, reset the Analysis Functions slide, and clear the buffer.
Before you begin, make sure you have installed Microsoft Visual C++ version 6.0 and Measurement Studio for Visual C++ 6.0 Full Development System.

Table of Contents

  1. Use the Measurement Studio AppWizard to Generate the Shell
  2. Modify the Dialog Resource
  3. Include the Appropriate Header Files
  4. Add Static Variables
  5. Add Member Variables and Initialize Them when the Program Starts
  6. Set up DataSocket Connectivity
  7. Use the ClassWizard to Generate Source Code and Modify the Source Code to Implement Functionality
  8. Add the Code for the Help Window (Optional)
  9. Compile and Run the Demo

1. Use the Measurement Studio AppWizard to Generate the Shell

Open Visual C++ and create a new project as follows:

1. Click File»New»Projects and select NI Measurement Studio AppWizard.

2. Enter SimpleMeasurement in Project name, select a Location in which to save the project, and click OK.

3. Select Measurement MFC Application and click Next.

4. Deselect all components except Advanced Analysis, Common, DataSocket, and User Interface, and then click Next.

5. Select Dialog based and click Next.

6. Deselect About Box, ensure that 3D controls is selected, and ensure that ActiveX Controls is selected. Enter Simple Measurement Demo as the title for the dialog. Click Next.

7. Select MFC Standard as the type of project you want to create, select Yes to include source file comments, and select to use the MFC library As a shared DLL. Click Next.

8. Click Finish and then click OK.

Back to Top

2. Modify the Dialog Resource



Modify the dialog resource as described below so that it looks like Figure 1:


Figure 1. Simple Measurement Demo Dialog Resource



1. Delete the Cancel and OK buttons and delete the TODO: Place dialog controls here text box.

2. Resize the dialog resource and right click it, click Properties, and verify the following values for the specified properties:

ID IDD_SIMPLEMEASUREMENT_DIALOG
Caption Simple Measurement Demo


3. Close the Dialog Properties window.

Acquire

Complete the following steps to add the acquisition controls shown in Figure 1:


1. Add a Combo Box control. Right click the control, click Properties, and change the properties listed:

General»ID IDC_NUMPTS
Styles»Vertical scroll Enable
Styles»Sort Disable
Styles»Type Drop List


You programmatically add data to the Combo Box later in this tutorial.

2. Click the arrow on the Combo Box control, and then drag the bottom border of the control to resize it so that the list displays when the user clicks the arrow on the Combo Box during run time.

The following illustration shows the border resized to show the contents of the Control Box Properties dialog box.




3. Add a Static Text control to the left of the Combo Box control. Right click the Static Text control, click Properties, and change the properties listed:


General»Caption Number of Points to Acquire
Styles»Align text Left


4. Select the Combo Box control and the Static Text control, and then click Layout»Align»Top.

5. Add a Visual C++ Button control. Right click the control, click Properties, and change the properties listed:


ID IDC_ACQUIRE
Caption Acquire


6. Add a CWGraph to the dialog resource and resize it to match the correlating control in Figure 1. Right click the graph control, click Properties, and change the properties listed:


General»ID IDC_ACQUIREDDATA
General»Caption Acquired Data
Style»Plots Simple
Plots»Line style Solid
Plots»Line color Bright Green
Ticks»Axis caption»XAxis Time (seconds)
Ticks»Axis caption»YAxis-1 Signal




Analyze

Complete the following steps to add the analysis controls shown in Figure 1:

1. Add a CWSlide control. Right click the control, click Properties, and change the properties listed:


ID IDC_ANALYSISFUNCTIONS
Caption Analysis Functions
Style Value pairs only
Value Pairs 1: Power Spectrum
2: Filter
3: Power Spectrum with Filter

The numbers represent the Value Pair Index. Refer to the CNiSlide  topic in Measurement Studio Reference for more information about Value Pairs.


2. Add a Visual C++ button to the dialog resource. Right click the button, click Properties, and change the properties listed:


General IDC_ANALYZE
General Analyze



3. Add a CWGraph to the dialog resource and resize it to match the correlating control in Figure 1. Right click the graph, click Properties, and change the properties listed:


General»ID IDC_ANALYZEDDATA
General»Caption Analyzed Data
Style»Plots Simple
Plots»Line style Solid
Plots»Line color White
Ticks»Axis caption»XAxis Frequency (Hz)
Ticks»Axis caption»YAxis -1 Intensity



Remaining Controls

1. Add two Visual C++ Buttons at the bottom of the dialog resource as shown in Figure 1. Right click each of the controls, click Properties, and change the properties listed:


Button1 (left) Button2 (right)
ID IDC_RESET IDHELP
Caption Reset Graphs &Help


2. Add a Visual C++ Group Box control at the bottom of the dialog resource as shown in Figure 1.

3. Delete the caption for the Group Box control.

4. Inside the group box, add an Edit Box control. Right click the Edit Box control, click Properties, and change the properties listed:


General»ID IDC_STATUS
Styles Align text: Left
Multiline: Enabled
AutoVScroll: Enabled
Border: Enabled
Read-only: Enabled

Disable all other settings on this tab.


You cannot delete the caption inside the Edit Box control. You will change this value programmatically in the Use the ClassWizard to Generate Source Code and Modify the Source Code to Implement Functionality section.

5. Inside the group box, add a Static Text control to the left of the Edit Box. Right click the Static Text control, click Properties, and change the properties listed:


General»Caption The status of the DataSocket connection is
Styles»Align text Left


6. Select the Edit Box control and the Static Text control, and then click Layout»Align»Vert. Center.


Help Dialog (Optional)

1. Click the ResourceView tab, right click the Dialog folder, and select Insert Dialog to create a new dialog box for the pop-up Help window.

2. Delete the Cancel button and move the OK button to the bottom, center of the dialog resource.

3. Right click the dialog resource, click Properties, and change the properties listed:

ID IDD_HELP
Caption Help


4. Add an Edit Box control and center it in the dialog resource. Right click the control, click Properties, and change the properties listed:

General»ID IDC_HELPTEXT
Styles Align text: Left
Multiline: Enabled
AutoVScroll: Enabled
Border: Enabled
Read-only: Enabled

Disable all other settings on this tab.


You will programmatically add the text to the edit box later in this document.

5. Double click the OK button on the Help panel. A dialog box titled Adding a Class appears. Select Create a new class and click OK. Enter HelpWindow as the Name and click OK.

6. In the ClassWizard, click Member Variables. Select IDC_HELPTEXT and click Add Variable. Enter m_HelpText for the member variable name and click OK. You set the value of m_HelpText programmatically later in this tutorial.

7. Click OK again to exit the ClassWizard.

 

Back to Top

3. Include the Appropriate Header Files


1. Add the following include statement below the stdafx.h include statement at the top of the SimpleMeasurementDlg.cpp file:

#include "HelpWindow.h"

2 Add the following include statement after the #endif // _MSC_VER > 1000 statement in the HelpWindow.h file:

#include "resource.h"

This include statement links the HelpWindow class to the help dialog resource. Excluding this header file from the HelpWindow.h file causes a compile error because the HelpWindow class cannot find the definition of the IDD_HELP dialog.

Back to Top

4. Add Static Variables


Add the text below so that it appears above the CSimpleMeasurement dialog section.

//////////////////////////////////////
// Static member initialization

// Array of characters that holds
// the label for each
// number-of-points-to-acquire option.
char *m_NumberOfPointsToAcquire[]=

{"128","256","512","1024"};


// Static variable to hold the
// size of the character array.
// This program uses this variable
// in the for loop that inserts the
// values associated with the
// labels in the Combo Box.
#define NUMBEROFPOINTSTOACQUIRELIST

((sizeof(m_NumberOfPointsToAcquire))/
(sizeof(m_NumberOfPointsToAcquire[0])))     

 

Back to Top

5. Add Member Variables and Initialize Them when the Program Starts


1. Right click anywhere on the Simple Measurement dialog resource and select ClassWizard. Click the Member Variables tab and add variables for the control IDs as shown below:


Control ID Variable Type
IDC_ANALYSISFUNCTIONS m_AnalysisFunctions CNiSlide
IDC_ANALYZEDDATA m_AnalyzedData CNiGraph
IDC_NUMPTS m_NumPts CComboBox1
IDC_ACQUIREDDATA m_AcquiredData CNiGraph
IDC_STATUS m_Status CEdit2


1Change the category to Control and select CComboBox as the variable type.
2Change the category to Control and select CEdit as the variable type.

2. Click OK to exit the ClassWizard.

3. Modify the code of the CSimpleMeasurementDlg::OnInitDialog() function as follows:

BOOL CSimpleMeasurementDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog.  
// The framework does this
// automatically when the
// program's main window
// is not a dialog.

// Set big icon
SetIcon(m_hIcon, TRUE);
// Set small icon  
SetIcon(m_hIcon, FALSE);  

// Initialize the Combo Box control
// with the values listed.
   

for (int i = 0;
i < NUMBEROFPOINTSTOACQUIRELIST; i++)
{
// Declare a string value.
CNiString strValue;
strValue =
m_NumberOfPointsToAcquire[i];
// Declare an integer value.
int value;
// Convert the string value to
// an integer value so that it
// can be used in a mathematical
// operation later in the program.
    strValue >> value;
// Capture the index of the string.
int ndx = m_NumPts.AddString(strValue);
// Insert the string value at
// the appropriate index.
    m_NumPts.SetItemData(ndx, value);

    }

// Initialize the Number of Points
// to Acquire Combo Box display.
m_NumPts.SetCurSel(0);


return TRUE;
}

4. Add the following variable declarations and constant declarations, shown in bold font, to the appropriate section in SimpleMeasurementDlg.h:

// Construction
public:
// The variable to hold the number
// of data points the
// user wants to acquire.
int m_numPoints;
// The variable to hold the data
// from the National Instruments
// weather server.
CNiReal64Vector m_rawData;
// The variable to use in the
// simulated acquisition of a
// subset of the data held
// in m_rawData.
CNiReal64Vector m_acquiredData;

Back to Top

6. Set up DataSocket Connectivity


DataSocket Member Variable

Complete the following steps to add a CNiDataSocket variable. You use this variable to connect to the DataSocket server and acquire the data you want to analyze:

1. In the ClassView, right click CSimpleMeasurementDlg and click Add Member Variable.

2. Type CNiDataSocket in Variable Type and type m_dataSocket in Variable Name.

3. Click OK to create the member variable.

Event Handler Prototypes

Add the following code in the OnInitDialog function to set up event handler prototypes for the DataSocket status and data updates.

// Set up the event handlers to
// handle the DataSocket status
// updates and data updates.
m_dataSocket.InstallEventHandler

(*this, OnStatusUpdatedEventHandler
(CSimpleMeasurementDlg,
StatusUpdatedEventHandler));

m_dataSocket.InstallEventHandler
(*this, OnDataUpdatedEventHandler
(CSimpleMeasurementDlg,
DataUpdatedEventHandler));

// Set the URL to use for
// demonstration data on the
// National Instruments weather
// server.
CString url =

"dstp://weather.ni.com/msdemo";

// Connect to the DataSocket server.
m_dataSocket.Connect

(url, CNiDataSocket::Read);



Event Handler Implementation

Add the following code to the appropriate section in the SimpleMeasurementDlg.h file:
 
// Implementation
protected:
// This is the implementation of
// the function that handles the  
// DataSocket status updates. This  
// event handler is installed with  
// the
// CNiDataSocket::InstallEventHandler
// function, which is called in the
// OnInitDialog function  for the
// CSimpleMeasurementDlg class.
virtual void StatusUpdatedEventHandler
(long status, long error, const CString& Message);
// This is the implementation of
// the function that handles the
// DataSocket data updates. This event
// handler is installed with the
// CNiDataSocket::InstallEventHandler  
// function, which is called in the
// OnInitDialog function for the
// CSimpleMeasurementDlg class.
virtual void DataUpdatedEventHandler
(CNiDataSocketData& data);

Event Handler Calls

Add the following code to the bottom of the SimpleMeasurementDlg.cpp file. These are the calls to the event handlers that handle the status and data update events for the DataSocket connection.

void CSimpleMeasurementDlg::StatusUpdatedEventHandler

(long status, long error, const CString& Message)

{

// Set the DataSocket Status control
// to display the incoming message.


    m_Status.SetWindowText(Message);      
}

void CSimpleMeasurementDlg::DataUpdatedEventHandler

(CNiDataSocketData& data)

{

// Pass the update to the m_rawData
// variable.

    m_rawData = data.Value;
}

Back to Top

7. Use the ClassWizard to Generate Source Code and Modify the Source Code to Implement Functionality



An event is a notification that a user interface control generates in response to some particular occurrence in the program, such as a mouse click on a button or a change in the value of a knob. As a programmer, you define functions to respond to events. These functions are called event handlers. Double click a control to generate skeleton event handler code for the default event of the control. Follow the instructions below to create event handlers for the appropriate controls in the Simple Measurement program.

1. Double click the Acquire control and then click OK. Add the code shown in bold font below:

void CSimpleMeasurementDlg::OnAcquire()
{

// Clear the Analyzed Data graph
// to avoid mistakenly comparing
// the  newly acquired data with
// the previously analyzed data.

m_AnalyzedData.ClearData();

// Acquire points using DataSocket
// and plot the acquired data on a
// graph.

// Test the DataSocket connection and
// print an error if it isn't valid.
if (!m_dataSocket.IsConnected())
{
MessageBox
("The DataSocket connection is invalid. Close
 the program, research the connection
 problem, and restart.",
 "Error", MB_ICONERROR | MB_OK)
AfxThrowUserException();
}

 
// Acquire DataSocket data. This function
// call retrieves a block of data from
// the National Instruments weather  
// server. Later in this event handler,
// you simulate data acquisition by  
// getting a certain number of data
// points from this block of data. The
// update event handler moves the data
// received into the m_rawData vector.
m_dataSocket.Update();

// Use this integer to hold the index for
// the data in the m_NumPts Combo Box.
int selection;

// Get the index of the current cursor
// selection in the m_NumPts Combo Box.
selection = m_NumPts.GetCurSel();

// Set m_numPoints, which you use to
// determine the array subset you
// assign to m_AcquiredData, equal to
// the program value associated
// with the currently selected index
// value.  
m_numPoints =
m_NumPts.GetItemData
(selection);

// Set m_acquiredData equal to a
// subset of m_rawData. The subset
// is determined by the number of
// points the user selected.
m_acquiredData =
m_rawData.Subset
(0,m_numPoints);

// Plot the points held in m_acquiredData
// starting at zero and incrementing by
// one.
m_AcquiredData.PlotY
(m_acquiredData, 0, 1);

}

2. Double click the Analyze control and then click OK. Add the following code shown in bold font below:

void CSimpleMeasurementDlg::OnAnalyze()
{

// Test the DataSocket connection
// and print an error if it isn't
// valid.
if (!m_dataSocket.IsConnected())
{
MessageBox
("The DataSocket connection is invalid. Close
 the program, research the connection
 problem, and restart.",
 "Error", MB_ICONERROR | MB_OK)
AfxThrowUserException();
}

// Use this integer in the
// switch statement below.
long function = 0;
   
double dt = (double)1/1024;
// df holds 1 if a Power Spectrum
// is not calculated.
double df = 1;  

// Create two vectors to hold
// temporary input and output
// values in the analysis
// functions.
CNiReal64Vector temp, result;

// Set the value of function
// to the current
// m_AnalysisFunctions.ValuePairIndex.    
function =
m_AnalysisFunctions.ValuePairIndex;

// Based on the value of the
// function variable, perform
// the appropriate analysis.

switch (function)
{
case 1:
// Power Spectrum
// m_acquiredData is the data
// subset you acquired from
// m_rawData, result is the
// output of this function, df is
// the frequency interval, and dt
// is the sampling period.
CNiMath::AutoPowerSpectrum
(m_acquiredData, result,
 df, dt);
break;
case 2:
// Filter
// m_acquiredData is the
// data subset you acquired from
// m_rawData, temp is the output,
// 1024 is the sampling frequency,
// 25 is the sampling cutoff,
// and 5 is the filter order.
CNiMath::ButterworthLowPass
(m_acquiredData, result,
 1024, 25, 5);
break;
case 3:
// Power Spectrum with Filter
// m_acquiredData is the data
// subset you acquired from
// m_rawData, temp is the
// output, 1024 is the sampling
// frequency, 25 is the sampling
// cutoff, and 5 is the filter
// order. For the
// AutoPowerSpectrum function,
// temp is the input from
// the ButterworthLowPass,  
// function result is the output,
// df is the  frequency interval,  
// and dt is the sampling period.
CNiMath::ButterworthLowPass
(m_acquiredData, temp, 1024,
 25, 5);
CNiMath::AutoPowerSpectrum
(temp, result, df, dt);
break;
default:
// Assert false so that if
// no analysis function is
// selected, nothing is
// plotted on the
// Analyzed Data graph.
ASSERT(false);
break;
}

// Plot the analyzed data
// on the Analyzed Data  
// graph. Plot the points
// held in result starting
// at zero and incrementing
// by the value of df.
m_AnalyzedData.PlotY
(result, 0, df);

}

3. Double click the Reset Graphs control and then click OK. Add the code shown in bold font below:

void CSimpleMeasurementDlg::OnReset()
{

// Clear the Acquired Data graph.
m_AcquiredData.ClearData();

// Clear the Analyzed Data graph.
m_AnalyzedData.ClearData();

// Set the pointer back to the first
// analysis operation
// on the slide control.
m_AnalysisFunctions.ValuePairIndex = 1;

// Clear the buffer.
m_acquiredData.Resize(0);

}

4. Compile and run the project. Click the Acquire, Analyze, and Reset buttons to test them.

Back to Top

8. Add the Code for the Help Window (Optional)


1. Double click the Help button control and then click OK. Add the following code to display the help window when the user clicks the Help button:

void CSimpleMeasurementDlg::OnHelp()
{

HelpWindow help;
help.DoModal();

}
2. Open the Help Dialog (IDD_HELP) and double click the panel (outside of any controls). Add help text as appropriate. Use \r\n\r\n to insert line breaks.

HelpWindow::HelpWindow(CWnd* pParent /*=NULL*/)
: CDialog(HelpWindow::IDD, pParent)
{

//{{AFX_DATA_INIT(HelpWindow)
m_HelpText = _T("");
//}}AFX_DATA_INIT
m_HelpText.Insert(0, "INSERT HELP TEXT HERE");

}

3. Add the code shown in bold font to the OK event handler of the Help dialog box:

void HelpWindow::OnOK()
{

EndDialog(0);
CDialog::OnOK();

}

Back to Top

9. Compile and Run the Demo


1. Compile and run your Simple Measurement program.

2. Select the number of points to acquire, click Acquire. When you click Acquire, the program acquires the number of points you selected and plots the data on the Acquired Data graph.

3. Use the slide control to select the type of analysis to perform, and click the Analyze button. When you click Analyze, the program performs the analysis function you selected on the data you acquired and plots the result on the Analyzed Data graph.

4. Click the Reset button. The program clears both graphs and resets the Analysis Functions slide to the first option.

5. Experiment with different options in Number of Points to Acquire and different analysis functions to see how the program works.

6. Close the program when you are finished.

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit