Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
What is Developer Zone?
United States

Document TypeTutorial
NI Supported: Yes
Publish Date: Sep 06, 2006


Feedback


Yes No

Related Links - Developer Zone

Related Links -Products and Services

Acquiring, Analyzing, and Presenting Data with Measurement Studio for Visual Basic

29 Ratings | 3.79 out of 5
 Print |  PDF

Overview

          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:
          • 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.

          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

  1. How Does This Application Work?
  2. Create the User Interface
  3. Create the Source Code

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:

OperationDescriptionInputOutput
FilterApplies a digital lowpass Butterworth filterx = One-dimensional array of input data
fs = Sampling frequency
fc = Cutoff frequency
Order = Filter order
Power SpectrumComputes 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 FilterComputes the auto power spectrum of the time domain signal and applies a digital lowpass Butterworth filterPower 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


  1. 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.
  2. Click OK.
  3. In the Properties Window, set Name to FMainForm. Set Caption to Measurement Studio for Visual Basic Demo.
  4. 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.
Note: Additional Measurement Studio project templates are available in the Visual Basic Toolbox. For information, refer to the Measurement Studio.



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:
  1. Place a Measurement Studio Numeric Edit (CWNumEdit) control on the form. By default, the control is named CWNumEdit1.
  2. 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.)
  3. Place a Visual Basic Label on the form. In the Properties Window, set Caption to Number of Points to Acquire.
  4. Place a Visual Basic CommandButton on the form. Set Name to cmdAcquire and Caption to Acquire.
    Analyze

    Use a Slide control to select which analysis operation you want applied to the data. Complete the following steps to create the Analyze section:
    1. 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.
    2. 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.
    3. Place a Measurement Studio Slide (CWSlide) control on the form. By default, the control is named CWSlide1.
    4. Right-click the control and select Properties. On the Style tab, select Value pairs only.
    5. 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).
    6. On the Numeric tab, set Caption to Function. Click OK.
      Present

      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:
      1. Place a Measurement Studio Graph (CWGraph) control on the form. In the Properties Window, set Name to grphOriginal.
      2. Right-click the control and select Properties. On the Graph tab, set Caption to Original Data. Click OK.
      3. Place another CWGraph control on the form. In the Properties Window, set Name grphAnalyze.
      4. In the property pages, go to the Graph tab and set Caption to Analyzed Data.
      5. On the Plots tab, set the line color to white. Click OK.
      6. 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.
      7. Place a Visual Basic Textbox control on the form. The textbox displays the status of the DataSocket connection.
      8. In the Properties Window, set Name to txtStatus and delete the text from Text. Set Enabled to False.
        Additional Functionality

        Add help and quit buttons to the form:
        1. Place a CommandButton below the Present section, as shown in Figure 3. In the Properties Window, set Name to cmdQuit and Caption to Quit.
        2. Place another CommandButton below the Present section. In the Properties Window, set Name to cmdHelp and Caption to Help.
          Create a Help Form

          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:
          1. Select Project»Add Form. Select Form from the Add Form dialog box.
          2. In the Properties Window, set Name to FHelp, Caption to Help, and Border Style to Fixed Dialog.
          3. Add a TextBox to the form.
          4. In the Properties Window, set Name to txtHelp and leave Text blank. Set Multiline to True and Enabled to False.
          5. Add a CommandButton to the form.
          6. In the Properties Window, set Name to cmdClose and Caption to Close.
          At run time, your completed Help form should look similar to Figure 4.


                Figure 4. Help Form

            Note: Select File»Save Project As and save the project as VBDemo.

            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.
                Private mydata As Variant
                '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
               End Sub

            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
               End Sub

            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 If
              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
               End Sub

            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
            29 Ratings | 3.79 out of 5
             Print |  PDF

            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/).