Overview
- Acquires a specified number of points and presents them as raw data on a graph.
- Analyzes the data with a selected analysis operation and presents the data on a graph.
- Provides step-by-step instructions on how to run the application.
- This document provides step-by-step instructions for using National Instruments Measurement Studio for Visual Basic and National Instruments DataSocket to acquire, analyze, and present data.
You will create an application that includes the following functionality:
To complete this application, you must have Microsoft Visual Basic and National Instruments Measurement Studio installed.
Note: Measurement Studio includes tools to build measurement applications in Visual Studio (VisualC++ and Visual Basic). Note that the Measurement Studio for Visual Basic tools were formerly known as ComponentWorks.
Table of Contents
How Does This Application Work?
This application uses a Measurement Studio DataSocket control to read data from the DataSocket server. DataSocket is a programming tool that enables you to share data between data sources and data targets. The DataSocket server is an executable that communicates and exchanges data between two applications using the dstp (DataSocket transfer protocol) scheme. In this application, use the DataSocket server to read data from a URL you specify programmatically. This URL connects to an NI demo server. After the requested data points are read, they are plotted on a Measurement Studio Graph.This application also uses the Measurement Studio Array and DSP controls to perform analysis operations on the data acquired from DataSocket. In this application, you can perform the following analysis operations on the data:
| Operation | Description | Input | Output |
| Filter | Applies a digital lowpass Butterworth filter | x = One-dimensional array of input data fs = Sampling frequency fc = Cutoff frequency Order = Filter order | |
| Power Spectrum | Computes the auto power spectrum of the time domain signal | x = time domain signal dt = sample period of time domain signal | Autospectrum = amplitude spectrum magnitude df = frequency interval |
| Power Spectrum with Filter | Computes the auto power spectrum of the time domain signal and applies a digital lowpass Butterworth filter | Power Spectrum: x = time domain signal dt = sample period of time domain signal Filter: x = One-dimensional array of input data fs = Sampling frequency fc = Cutoff frequency Order = Filter order | Power Spectrum: Autospectrum = amplitude spectrum magnitude df = frequency interval |
The analyzed data is plotted on a Measurement Studio Graph.
Note: For information about Measurement Studio controls and functionality, refer to the Measurement Studio Reference (Start»Programs»National Instruments»Measurement Studio»Help»Measurement Studio Reference).
Create the User Interface
- Launch Visual Basic and select NI Measurements Exe from the New Project dialog box. This project template automatically loads the Measurement Studio User Interface, 3D Graph, Analysis, DataSocket, and measurement (DAQ) controls into the Visual Basic Toolbox.
- Click OK.
- In the Properties Window, set Name to FMainForm. Set Caption to Measurement Studio for Visual Basic Demo.
- Add Visual Basic Frame controls to divide the form into three sections—Acquire, Analyze, and Present. Figure 3 shows these sections, as well as the controls that will be added to them.

Figure 3. Measurement Studio Demo User Interface
Acquire
Use a Numeric Edit control to specify the number of points to acquire and a command button to begin the acquisition and analysis. Complete the following steps to create the Acquire section of the user interface:
- Place a Measurement Studio Numeric Edit (CWNumEdit) control on the form. By default, the control is named CWNumEdit1.
- Configure the control through its interactive property pages. Right-click the control and select Properties. On the Numeric tab, set Minimum to 50, and Maximum to 2048. Enable Do range checking. At run time, the control ensures that all values you specify are between 50 and 2048. (The DataSocket control reads an array of 2048 points from the server.)
- Place a Visual Basic Label on the form. In the Properties Window, set Caption to Number of Points to Acquire.
- Place a Visual Basic CommandButton on the form. Set Name to cmdAcquire and Caption to Acquire.
Use a Slide control to select which analysis operation you want applied to the data. Complete the following steps to create the Analyze section:
- Place a Measurement Studio DSP (CWDSP) control on the form. Because the control is invisible at run time, you can place it anywhere on the form. By default, the control is named CWDSP1.
- Place a Measurement Studio Array (CWArray) control on the form. Because the control is invisible at run time, you can place it anywhere on the form. By default, the control is named CWArray1.
- Place a Measurement Studio Slide (CWSlide) control on the form. By default, the control is named CWSlide1.
- Right-click the control and select Properties. On the Style tab, select Value pairs only.
- On the Value Pairs tab, select the fourth value pair and delete it. Rename the three remaining value pairs by selecting each value pair and setting Name to Power Spectrum with Filter (Index 1), Power Spectrum (Index 2), and Filter (Index 3).
- On the Numeric tab, set Caption to Function. Click OK.
Use two graphs to display the data read from the DataSocket server and the data analyzed by the selected analysis operation. Use a textbox to display the connection status of the DataSocket control. Complete the following steps to create the Present section:
- Place a Measurement Studio Graph (CWGraph) control on the form. In the Properties Window, set Name to grphOriginal.
- Right-click the control and select Properties. On the Graph tab, set Caption to Original Data. Click OK.
- Place another CWGraph control on the form. In the Properties Window, set Name grphAnalyze.
- In the property pages, go to the Graph tab and set Caption to Analyzed Data.
- On the Plots tab, set the line color to white. Click OK.
- Place a DataSocket (CWDataSocket) control on the form. Because the control is invisible at run time, you can place it anywhere on the form. By default, the control is named CWDataSocket1.
- Place a Visual Basic Textbox control on the form. The textbox displays the status of the DataSocket connection.
- In the Properties Window, set Name to txtStatus and delete the text from Text. Set Enabled to False.
Add help and quit buttons to the form:
- Place a CommandButton below the Present section, as shown in Figure 3. In the Properties Window, set Name to cmdQuit and Caption to Quit.
- Place another CommandButton below the Present section. In the Properties Window, set Name to cmdHelp and Caption to Help.
You can add help to your applications to provide your users with additional information about using your application. Add another form to the same project to create a help window that pops up when you click the Help button on the main form:
- Select Project»Add Form. Select Form from the Add Form dialog box.
- In the Properties Window, set Name to FHelp, Caption to Help, and Border Style to Fixed Dialog.
- Add a TextBox to the form.
- In the Properties Window, set Name to txtHelp and leave Text blank. Set Multiline to True and Enabled to False.
- Add a CommandButton to the form.
- In the Properties Window, set Name to cmdClose and Caption to Close.

Figure 4. Help Form
Create the Source Code
Complete the following steps to create the code for FMainForm:
1. Add the following code to the Declarations section of the form to declare variables that are accessible by different events throughout the application:
- 'Requested data read from the DataSocket server.
'Change in time
Const dt As Double = 1 / 2048
2. When you click the Acquire button, the DataSocket control connects to the DataSocket server and performs a read operation. Double-click the Acquire button and add the following code to the event procedure:
- Private Sub cmdAcquire_Click()
'Connect to the DataSocket server. Read data once.
CWDataSocket1.ConnectTo "dstp://weather.ni.com/msdemo", cwdsRead
3. Double-click on the DataSocket control to create the OnDataUpdated event procedure. (This event is the default event for the control.)
4. When the data is read from the server, the number of points you specify in the CWNumEdit control is plotted on the Original Data graph. Add the following code to the event:
- Private Sub CWDataSocket1_OnDataUpdated(ByVal Data As CWDSLib.CWData)
'Plot the requested number of points, which is a subset of the data array
'of data read from DataSocket. Because OnDataUpdated returns an object,
'you must use Data.Value to access the data.
mydata = CWArray1.Subset1D(Data.Value, 0, CWNumEdit1.Value)
'Plot the subset of original data
grphOriginal.PlotY mydata, 0, dt
5. Double-click on the form to create the Form_Load event procedure. Add the following code to the event to initialize mydata:
- Private Sub Form_Load()
'Initialize the Variant
mydata = Null
End Sub
6. Display the status of the DataSocket connection on the form. In the code window, select CWDataSocket1 from the object listbox and OnStatusUpdated from the procedure listbox to create the OnStatusUpdated event procedure.
7. Add the following code to the event:
- Private Sub CWDataSocket1_OnStatusUpdated(ByVal Status As Long, ByVal Error As Long, ByVal Message As String)
txtStatus.Text = Message
End Sub
8. Use the CWSlide control to select which analysis operation you want to apply to the data read from the DataSocket server. Create a new event named AnalyzeData.
Tip: To create an event procedure, select Tools»Add Procedure. Enter the name of the procedure and click OK.
9. The index of the slide indicates which analysis operation is performed. The analyzed data is plotted on the Analyzed Data graph. Add the following code to the AnalyzedData event:
- Public Sub AnalyzeData()
'Result of the AutoPowerSpectrum
Dim result As Variant
'Frequency interval
Dim df As Variant
'Use the ValuePairIndex property to evaluate which
'value pair of the collection is selected.
If Not IsNull(mydata) Then
Select Case (CWSlide1.Axis.ValuePairs(CWSlide1.ValuePairIndex).Name)
Case "Power Spectrum with Filter"
CWDSP1.AutoPowerSpectrum CWDSP1.BwLPF(mydata, 2048, 25, 5), dt, result, df
grphAnalyze.Axes(1).Caption = "Frequency"
grphAnalyze.Axes(1).AutoScale = False
grphAnalyze.Axes(1).Maximum = 100
grphAnalyze.PlotY result, 0, df
Case "Power Spectrum"
CWDSP1.AutoPowerSpectrum mydata, dt, result, df
grphAnalyze.Axes(1).Caption = "Frequency"
grphAnalyze.Axes(1).AutoScale = False
grphAnalyze.Axes(1).Maximum = 100
grphAnalyze.PlotY result, 0, df
Case "Filter"
grphAnalyze.Axes(1).Caption = "Time"
grphAnalyze.Axes(1).AutoScale = True
grphAnalyze.PlotY CWDSP1.BwLPF(mydata, 2048, 25, 5), 0, dt
End Select
Else
- 'Display a message box if the user does not click Acquire before selecting an analysis operation.
MsgBox "You must acquire data first."
End Sub
10. If you select a different analysis option on the slide, the Analyzed Data graph updates to display the new plot. Double-click the slide to create the PointerValueChanged event. Add the following code:
- Private Sub CWSlide1_PointerValueChanged(ByVal Pointer As Long, Value As Variant)
AnalyzeData
End Sub
11. Call AnalyzeData when you click the Acquire button. When you click the Acquire button, data is read from the DataSocket server, and the OnDataUpdated event is generated. Add the bold line of code to the OnDataUpdated event:
- Private Sub CWDataSocket1_OnDataUpdated(ByVal Data As CWDSLib.CWData)
'Plot the requested number of points, which is a subset of the data array
'of data read from DataSocket. Because OnDataUpdated returns an object,
'you must use Data.Value to access the data.
mydata = CWArray1.Subset1D(Data.Value, 0, CWNumEdit1.Value)
'Plot the subset of original data
grphOriginal.PlotY mydata, 0, dt
- AnalyzeData
12. If you click the Acquire button again, the graphs must be cleared before new data is plotted. Add the bold code to the cmdAcquire_Click event:
- Private Sub cmdAcquire_Click()
'Connect to the DataSocket server. Read data when it's ready.
CWDataSocket1.ConnectTo "dstp://weather.ni.com/msdemo", cwdsRead
'Clear old data
grphOriginal.ClearData
grphAnalyze.ClearData
End Sub
13. Double-click the Quit button and add the following code to the cmdQuit_Click event:
- Private Sub cmdQuit_Click()
CWDataSocket1.Disconnect
End
End Sub
The DataSocket connection terminates and the application ends.
14. Double-click the Help button and add the following code to the cmdHelp_Click event:
- Private Sub cmdHelp_Click()
FHelp.Show vbModal
End Sub
This event opens the Help form when the user clicks on the Help button. It also ensures that the user cannot control the main form until he has clicked Close on the help form.
15. Open the Help form that you created earlier and double-click on the form. Add the following code to the Form_Load event:
- Private Sub Form_Load()
txtHelp.Text = "1. Enter the number of points you want to acquire." _
& vbCrLf & "2. Select the analysis you want to perform." _
& vbCrLf & "3. Click Acquire to display the original and analyzed data." _
& vbCrLf & "4. Move the slide pointer to perform different analysis operations."
End Sub
16. Double-click the Close button on the Help form, and add the following code to the cmdClose_Click event:
- Private Sub cmdClose_Click()
Unload FHelp
End Sub
17. Save and run the application.
Test the functionality of your application. When you run your application, select different analysis operations with the slide. Notice that the plot of the Analyzed Data graph dynamically updates to present the results of the selected operation. Enter new values in the numeric edit and click Acquire to change the number of points displayed on the Original Data graph. Test the Help button. When you are finished testing, click the Close button to disconnect from the DataSocket server and end the application.
Related Links:
Acquiring, Analyzing and Presenting Data with Measurement Studio for Visual Basic
Getting Started with Measurement Studio for Visual Basic
Reader Comments | Submit a comment »
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/).
