Acquiring, Analyzing, and Presenting Data with LabWindows/CVI
Overview
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.
Table of Contents
Getting Started
- Open LabWindows/CVI and select File»New»Project (*.prj) to create a new project.
- Save the project as CVIDemo.prj.
See Also:
Acquiring, Analyzing, and Presenting Data using LabWindows/CVICreating the Graphical User Interface (GUI)
- Select File»New»User Interface (*.uir) to create a new graphical user interface.
- Double-click the panel and add the following information in the Edit Panel dialog box:
Constant Name MAINPANEL Panel Title LabWindows/CVI Demo - 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.
- Add controls as described in the following sections. When you finish, your user interface (UI) will look similar to the one in Figure 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:

COMMANDBUTTON COMMANDBUTTON_2 Constant Name QUIT HELP Callback Function Quit Help Control Mode Hot Hot Label __Quit __Help - 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 Name HELP Panel Title Help - 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 Name TEXTBOX Default Value Note 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 Mode Hot Label Instructions for Operating Demo - 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 Name OK Callback Function OK Control Mode Hot Label OK - 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 - 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.
- Select File»Add File to Project.
- Return to the Project window and select Edit»Add Files to Project»Include (*.h).
- Select CVIDemo.h and click Add.
- Click OK.
Generating the Source Code
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 Name | NUMBERPOINTS |
| Control Mode | Hot |
| Label | Number of Points to Acquire |
| Label | Value |
| 128 | 128 |
| 256 | 256 |
| 512 | 512 |
| 1024 | 1024 |
| 2048 | 2048 |
| Constant Name | ACQUIRE |
| Callback Function | AcquireData |
| Control Mode | Hot |
| Label | Acquire |
| Constant Name | STATUS |
| Control Mode | Hot |
| Label | Status of DataSocket Server |
| Constant Name | ACQUIREMSG |
| Default Value | Acquire |
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 Name | ANALYSIS |
| Control Mode | Hot |
| Label | Analysis Function |
| Label | Value |
| Power Spectrum | 0 |
| Filter | 1 |
| Power Spectrum with Filter | 2 |
| Constant Name | ANALYZE |
| Callback Function | AnalyzeData |
| Control Mode | Hot |
| Label | Analyze |
| Constant Name | ANALYZEMSG |
| Default Value | Analyze |
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:
| GRAPH | GRAPH_2 | |
| Constant Name | ORIGINALGRAPH | ANALYZEDGRAPH |
| Control Mode | Indicator | Indicator |
| Data Mode | Retain | Retain |
| Label | Original Data | Analyzed Data |
3. 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 Name | PRESENTMSG |
| Default Value | Present |
Help Panel and Save UIR
- Return to the User Interface Editor and select Code»Generate»All Code. This command opens the Generate All Code dialog box.
- In the Generate All Code dialog box, select the Add to Current Project option in the Target Files section.
- Select MAINPANEL as the panel to load and display at startup. Then, enter panelHandle in Panel Variable Name.
- Select Quit and OK as the Select QuitUserInterface Callbacks.
- 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.
See Also:
Acquiring, Analyzing, and Presenting Data using LabWindows/CVIModifying the Source Code
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
2. 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;
3. 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 */
- return -1;
// 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;
- /****************************************************************************
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.
5. 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;
6. 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;
case 1:
- // Filter
Bw_LPF (staticArray, numpoints, 2048, 25, 5, tempArray);
case 2:
- // Power Spectrum with Filter
Bw_LPF (tempArray, numpoints, 2048, 25, 5, staticArray);
AutoPowerSpectrum (staticArray, numpoints, dt, tempArray, &df);
PointsToPlot=100;
}
PlotY (panelHandle, MAINPANEL_ANALYZEDGRAPH, tempArray, PointsToPlot, VAL_DOUBLE, VAL_THIN_LINE, VAL_SOLID_SQUARE, VAL_SOLID, 1, VAL_WHITE);
}
7. 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)
{
- if ((handle = LoadPanel (0, "CVIDemo.uir", HELP)) < 0)
- return -1;
RunUserInterface ();
DiscardPanel (handle);
9. Save the project.
10. Select Build»Create Debuggable Executable to compile the project.
11. Select Run»Debug CVIDemo_dbg.exe to run the application.
Reader Comments | Submit a comment »
In order to make this compile, I had to
manually load the dataskt.fp instrument file,
as suggested by Mert A. at:
http://forums.ni.com/ni/board/message?board.id=180&message.id=32002
(" Those link errors indicate that the
project uses the DataSocket library, but it
was not found to link against. To fix this,
you should explicitly add the DataSocket FP
(CVI81\toolslib\datasock\dataskt.fp) to the
project. The program will also build if you
just load the FP from the Library menu, but
it is better to explicitly associate it with
the project that needs it.")
The DataSocket site listed in the code
(weather.ni.com/msdemo") is down. According
to this forum page, it's down permanently,
but there are workarounds using local files,
which I haven't gotten working yet.
http://forums.ni.com/ni/board/message?board.id=230&message.id=1756
- daniel allen, none. dada.da@gmail.com - Oct 23, 2007
Code for the beginner, lacks detail
I'm new to Labwindows/CVI and in trying out
this example, my code yields several
errors/warnings and after trying a few
things, it still doesn't compile. The
example lacsk the complete info to compile
properly. -- Sal
- Sal Aguinaga. sa_jr@yahoo.com - Jul 25, 2005
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).

