Acquiring, Analyzing, and Presenting Data with LabWindows™/CVI™

Updated Sep 25, 2020

Environment

Software

  • LabWindows/CVI

This document describes the step-by-step procedure for using LabWindows/CVI and DataSocket to acquire, analyze, and present data.

This document describes how to create a program that includes the following functionality:
  • Acquires a specified number of points and presents them as raw data on the Original Data graph.
  • Analyzes the data and presents it on the Analyzed Data graph.
  • Provides a separate help panel with step-by-step instructions for using the demo program.

Getting Started

  1. Open LabWindows/CVI and select File»New»Project (*.prj) to create a new project.
  2. Save the project as CVIDemo.prj.
 

Creating the Graphical User Interface (GUI)

  1.  Select File»New»User Interface (*.uir) to create a new graphical user interface.
  2. Double-click the panel and add the following information in the Edit Panel dialog box:
    Constant NameMAINPANEL
    Panel TitleLabWindows/CVI Demo
  3. Select Create»Decoration»Raised Frame and add three raised frame decoration boxes to the panel as shown in Figure 1. The first box will contain the Acquire section, the second box will contain the Analyze section, and the third box will contain the Present section.
  4. Add controls as described in the following sections. When you finish, your user interface (UI) will look similar to the one in Figure 1.


Figure 1. Acquire, Analyze, and Present UI
 

Acquire

Complete the following steps to set up the controls in the Acquire section:
  1. Select Create»Ring»Ring to add a ring control. Double-click the control and add the following information in the Edit Ring dialog box:
Constant NameNUMBERPOINTS
Control ModeHot
LabelNumber of Points to Acquire
  1. Click Label/Value Pairs in the Edit Ring dialog box and enter the following values in the Label and Value fields.
LabelValue
128128
256256
512512
10241024
20482048
  1. Select Create»Command Button»Square Command Button to add a command button. Double-click the control and add the following information in the Edit Command Button dialog box:
Constant NameACQUIRE
Callback FunctionAcquireData
Control ModeHot
LabelAcquire
  1. Select Create»Text»Text Box to add a text box. Double-click the control and add the following information in the Edit Text Box dialog box:
Constant NameSTATUS
Control ModeHot
LabelStatus of DataSocket Server
  1. Select Create»Text»Text Message (from Classic Style Controls) to add a text message control. Double-click the control and add the following information in the Edit Text Message dialog box:
Constant NameACQUIREMSG
Default ValueAcquire

The font used in the demo is bold 14-point NIDialog. To customize the font, click Text Style. Move the text message control to the top left corner of the decoration control.
 

Analyze

Complete the following steps to set up the controls in the Analyze section:
  1. Select Create»Ring»Vertical Pointer Slide to add a vertical pointer slide control. Double-click the control and add the following information in the Edit Ring Slide dialog box:
Constant NameANALYSIS
Control ModeHot
LabelAnalysis Function
  1. Select Label/Value Pairs in the Edit Ring Slide dialog box and add the following information to the Label and Value fields:
LabelValue
Power Spectrum0
Filter1
Power Spectrum with Filter2
  1. Select Create»Command Button»Square Command Button to add a command button. Double-click the control and add the following information in the Edit Command Button dialog box:
Constant NameANALYZE
Callback FunctionAnalyzeData
Control ModeHot
LabelAnalyze
  1. Select Create»Text»Text Message (from Classic Style Controls) to add a text message. Double-click the control and add the following information in the Edit Text Message dialog box:
Constant NameANALYZEMSG
Default ValueAnalyze

The font used in the demo is bold 14 point NIDialog. To customize the font, click Text Style. Move the text message control to the top left corner of the decoration control.
 

Present

Complete the following steps to set up the controls in the Present section:
  1. Select Create»Graph»Graph and add two graphs to the panel. Double-click each graph control and add the following information in the Edit Graph dialog box:
GRAPHGRAPH_2
Constant NameORIGINALGRAPHANALYZEDGRAPH
Control ModeIndicatorIndicator
Data ModeRetainRetain
LabelOriginal DataAnalyzed Data
  1. For each graph control, click the X-axis and Left Y-axis buttons and select Auto Scale.
  2. Click Create»Text»Text Message (from Classic Style Controls) to add a text message control. Double-click the control and add the following information in the Edit Text Message dialog box:
Constant NamePRESENTMSG
Default ValuePresent

The font used in the demo is bold 14 point NIDialog. To customize the font, click Text Style. Move the text message control to the top left corner of the decoration control.
 

Help Panel and Save UIR

  1. Select Create»Command Button»Square Command Button to add two command buttons to the main panel below the Present section. Double-click each button and add the following information in the Edit Command Button dialog box:
    COMMANDBUTTONCOMMANDBUTTON_2
    Constant NameQUITHELP
    Callback FunctionQuitHelp
    Control ModeHotHot
    Label__Quit__Help
  2. Right-click the window outside the main panel and select Panel to create a new panel titled Help. Double-click the new panel and fill in the following fields in the Edit Panel dialog box:
    Constant NameHELP
    Panel TitleHelp
  3. Select Create»Text»Text Box to add a text box. Double-click the control and add the following information in the Edit Text Box dialog box:
    Constant NameTEXTBOX
    Default ValueNote To add a new line in the text box control, press <Ctrl-Enter>. You must add new lines to wrap the text as shown.

    1. Select the number of points you want
    to acquire and click Acquire.

    2. Select the type of analysis you want
    to perform on your data. You can select
    only one analysis function at a time.

    3. To exit, click Quit.

    Control ModeHot
    LabelInstructions for Operating Demo
  4. Select Create»Command Button»Square Command Button to add a command button. Double-click the control and add the following information in the Edit Command Button dialog box:
    Constant NameOK
    Callback FunctionOK
    Control ModeHot
    LabelOK
  5. Place this new help panel so that half of the panel overlaps the main panel as shown in Figure 2. Overlapping the panels ensures that you can access either panel easily.
Figure 2. Help Panel Overlapping Main Panel
  1. Select File»Save As and save the UI as CVIDemo.uir. When you save this file, LabWindows/CVI automatically creates a header file (.h), which is the link between the .uir file and the .c file.
  2. Select File»Add File to Project.
  3. Return to the Project window and select Edit»Add Files to Project»Include (*.h).
  4. Select CVIDemo.h and click Add.
  5. Click OK.


Generating the Source Code

Complete the following steps to generate the source code:

  1. Return to the User Interface Editor and select Code»Generate»All Code. This command opens the Generate All Code dialog box.
  2. In the Generate All Code dialog box, select the Add to Current Project option in the Target Files section.
  3. Select MAINPANEL as the panel to load and display at startup. Then, enter panelHandle in Panel Variable Name.
  4. Select Quit and OK as the Select QuitUserInterface Callbacks.
  5. Click OK. LabWindows/CVI generates skeleton code, names the file CVIDemo.c, and adds the .c file to your project. You are now ready to modify the source code.
 

Modifying the Source Code

Complete the following steps to modify the source code required for the demo:

  1. Add the following code to the top of the file (under the include files already listed):
#include <analysis.h>
#include <ansi_c.h>
#include <cviauto.h>
#include "dataskt.h"

/************************************************************************
The following constants have been defined for use throughout the program.
MAX_POINTS represents the maximum number of points the user can acquire.
************************************************************************/

#define TRUE 1
#define FALSE 0
#define MAX_POINTS 2048
 
  1. Replace the existing panelHandle declaration with the following code.
/************************************************************************
numpoints holds the number of points the user requests. array stores
either the acquired data or the analyzed data. oldarray stores the
originally acquired data.
************************************************************************/

static int panelHandle, handle, numpoints=16;
static double array[MAX_POINTS], oldarray[MAX_POINTS];
static DSHandle dsData;
 
  1. In the main function, add the following bold code. This code opens a session to DataSocket and discards the session after use.
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "CVIDemo.uir", MAINPANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
// Open connection to DataSocket site.
DS_Open ("dstp://weather.ni.com/msdemo", DSConst_Read, dsDataCallback, NULL, &dsData);
RunUserInterface ();
DS_DiscardObjHandle (dsData); 
DiscardPanel (panelHandle);
return 0;
}
 
  1. Insert the following code after the DataSocket handle declaration:
/****************************************************************************
This is the callback function for the DataSocket handle. For more information
on callback functions and DataSocket, refer to the DS_Open function panel help.
******************************************************************************/

void dsDataCallback (DSHandle dsHandle, int event, void *callbackData)
{
if (event == DS_EVENT_STATUSUPDATED)
{
HRESULT hr = NOERROR;
char message[1000];
hr = DS_GetLastMessage (dsHandle, message, 1000);
if (SUCCEEDED(hr))
ResetTextBox (panelHandle, MAINPANEL_STATUS, message);
}
 }

This code is the DataSocket handle callback. For more information, refer to the DS_Open function panel help by right-clicking the DS_Open function and selecting Recall Function Panel.
 
  1. Replace the existing AcquireData callback function with the following code:
/*****************************************************************
This callback is called when an event occurs onthe Acquire button.
If the button is clicked, the following happens:          
1. Check to see how many points the user wants to acquire.        
2. Acquire the specified number of points.      
3. Output the resulting graph on the Original Data graph.
*****************************************************************/

int CVICALLBACK AcquireData (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
int size;
double *value;
switch (event)
{
case EVENT_COMMIT:
//Delete old graphs from the Original Graph display.
DeleteGraphPlot (panelHandle, MAINPANEL_ORIGINALGRAPH, -1, VAL_IMMEDIATE_DRAW);
//Get the number of points the user has requested.
GetCtrlVal (panelHandle, MAINPANEL_NUMBERPOINTS, &numpoints);
   
//Copy the data into oldarray[] as a backup.
DS_Update (dsData);
DS_GetDataType (dsData, NULL, &size, NULL);
value = malloc (size* (sizeof(double)));
DS_GetDataValue (dsData, CAVT_DOUBLE | CAVT_ARRAY, value, size*sizeof(double), 0, 0);
           
Copy1D (value, numpoints, array);
Copy1D (array, numpoints, oldarray);
//Plot the data onto the Original Graph.
PlotY (panelHandle, MAINPANEL_ORIGINALGRAPH, array, numpoints, VAL_DOUBLE, VAL_THIN_LINE, VAL_SOLID_SQUARE, VAL_SOLID, 1, VAL_GREEN);
free (value);
break;
 }
return 0;
}
 
  1. To determine which analysis function the user wants to perform, replace the existing AnalyzeData callback function with the following. This code determines which analysis function the user wants to perform.
int CVICALLBACK AnalyzeData (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
int boolean, function, PointsToPlot=numpoints;
double *tempArray, df, *staticArray, dt=1/2048;
switch (event)
{
case EVENT_COMMIT:
/************************************************************************
The Copy1D function restores array with the original data acquired. This
functionality is important because every time you perform an analysis on
array, its original values are overwritten. If a user calls the Analysis
function multiple times without re-acquiring data, the program analyzes
existing data.
************************************************************************/
Copy1D (oldarray, numpoints, array);
tempArray = malloc (numpoints*sizeof(double));
staticArray = malloc (numpoints*sizeof(double));    
staticArray = tempArray = array;
GetCtrlAttribute (panelHandle, MAINPANEL_ANALYSIS, ATTR_CTRL_VAL, &function);
 
/**********************************************************************
The switch statement determines which function to perform. The program
performs the appropriate analysis function and then plots the analyzed
data on the Analyzed Data graph.
**********************************************************************/
switch (function)
{
case 0:
// Power Spectrum
AutoPowerSpectrum (staticArray, numpoints, dt, tempArray, &df);
PointsToPlot=numpoints/2;
break;
case 1:
// Filter
Bw_LPF (staticArray, numpoints, 2048, 25, 5, tempArray);
break;
case 2:
// Power Spectrum with Filter
Bw_LPF (tempArray, numpoints, 2048, 25, 5, staticArray);
AutoPowerSpectrum (staticArray, numpoints, dt, tempArray, &df);
PointsToPlot=100;
break;
}
DeleteGraphPlot (panelHandle, MAINPANEL_ANALYZEDGRAPH, -1, VAL_IMMEDIATE_DRAW);
PlotY (panelHandle, MAINPANEL_ANALYZEDGRAPH, tempArray, PointsToPlot, VAL_DOUBLE, VAL_THIN_LINE, VAL_SOLID_SQUARE, VAL_SOLID, 1, VAL_WHITE);
break;
}
return 0;
}
 
  1. Add the following code to the Help callback below the case EVENT_COMMIT: statement:
/*This code displays the Help panel.*/

int CVICALLBACK Help (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
 case EVENT_COMMIT:
if ((handle = LoadPanel (0, "CVIDemo.uir", HELP)) < 0)
return -1;
DisplayPanel (handle);
RunUserInterface ();
DiscardPanel (handle);
 
  1. Save CVIDemo.c.
  2. Save the project.
  3. Select Build»Create Debuggable Executable to compile the project.
  4. Select Run»Debug CVIDemo_dbg.exe to run the application.