Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Sep 6, 2006


Feedback


Yes No

Related Categories

Related Links - Developer Zone

Related Links - Products and Services

Connecting Measurement Studio User Interface ActiveX Controls to Remote Data

5 ratings | 3.80 out of 5
Print

Overview

This application note explains how to create Visual Basic programs that connect to remote data sources over the Internet and read from or write to them. Data sources might be data items on OPC, DataSocket, HTTP, FTP, or file servers. Consider an engine test scenario taking place on the factory floor where data is collected during the test. You work in a different city and want to monitor the remote measurements from your office. You can create a simple but powerful Visual Basic program using the Measurement Studio User Interface (UI) ActiveX controls -- buttons, graphs, knobs, numeric edit boxes, and slides -- to visualize the temperature measurements or publish them from your desk, and you can do it without writing any code. Measurement Studio uses integrated DataSocket connectivity to create the connection between UI controls and remote data sources. This connection is called a binding.

Note: Measurement Studio includes tools to build measurement applications in ANSI C, Visual C++, and Visual Basic. The Measurement Studio Visual Basic tools were formerly known as ComponentWorks.

Binding Overview

A binding connects a property on a UI control to a remote data source (readable data item) or target (writable data item). Remote data sources and targets might be data items on OPC, DataSocket, HTTP, FTP, or file servers located anywhere on the Internet. For example, you can bind the data in a plot, the color of a plot, or even the color of a caption name to a data item on an OPC server or waveform data on a DataSocket or HTTP (Web) server.

You can create bindings through the CWBinding object. The CWBinding object uses National Instruments DataSocket technology to connect to data sources and share live measurements over the Internet. With DataSocket, you can share data between applications and/or different data sources without writing code to support different data formats and networking protocols.


    Note: Refer to the Measurement Studio Reference, which is available from your Windows Start menu if you installed Measurement Studio, or the DataSocket Web site at ni.com/datasocket for more information about DataSocket technology, DSTP, and sharing live data over the Internet.

    Locating Data Sources

With integrated DataSocket connectivity, the UI controls can connect to many different data sources on your local machine or on a remote server. No matter where the data item is, you access it the same way -- with a URL. URLs provide a standard mechanism for referring to data anywhere on the Internet. You already know how to use URLs to locate Web pages from your browser, and you can locate data items with DataSocket the same way.

Like URLs you use in a Web browser, DataSocket URLs can point to different types of data sources depending on the prefix of the URL, or the URL scheme. DataSocket recognizes several existing schemes:

  • opc -- OLE for Process Control. OPC is an open and interoperable interface that standardizes communication between automation and control applications, field systems and devices, and business and office applications. Many devices, such as PLCs, RTUs, and National Instruments FieldPoint modules, communicate with OPC client applications through OPC servers. With integrated DataSocket connectivity, you can create an OPC client application that can communicate with OPC servers. You can access data items on OPC servers with URLs of the following form:

      opc://machine_name/server_name/item_name

    The following URLs show how to specify different data items on OPC servers:


      opc:/National Instruments.OPCDemo/sine

      opc:/National Instruments.OPCDemo/sine?Accesspath=sine

      opc://machine/National Instruments.OPCModbus/Modbus Demo Box.4:0

      opc://machine/National Instruments.OPCModbus/Modbus Demo Box.4:0?updaterate=100&deadband=0.7


  • dstp -- DataSocket Transfer Protocol. DSTP is a protocol for sharing live data. When you use dstp for live data transfer, you’re actually sending data to a DataSocket server, which handles the networking tasks for you. The DataSocket server is a small, stand-alone executable that enables data exchange between multiple applications, each of which uses a DataSocket client. The server accepts and stores information from data sources and targets and relays it to other data sources and targets. When you run the server on your computer, you make data easily accessible to other DataSocket applications on the same computer or other computers connected though a TCP network, such as the Internet. You can access data items on OPC servers with URLs of the following form:
      dstp://machine/item

    The following URL accesses the wave data item on a local DataSocket server:

      dstp://localhost/wave

  • http -- HyperText Transfer Protocol. HTTP is a protocol based on TCP/IP, which is used to download Web pages from an HTTP server to a Web browser. The following URL accesses a chirp wave file on the National Instruments Web server:


  • ftp -- File Transfer Protocol. FTP is a protocol based on TCP/IP to exchange files between computers. The following URL accesses a DataSocket data file on the National Instruments FTP server:
    ftp://user:password@ftp.natinst.com/support/compworks/datasocket/tone.dsd

  • file -- The file protocol points to files on your computer or a networked computer. The following URL accesses a tab-delimited text file that gets installed with Measurement Studio and DataSocket:
    file:c:\MeasurementStudio\Vb\Samples\DataSocket\datafiles\tabtext.txt


    Tip Often OPC and DSTP URLs can be difficult to create. With integrated DataSocket connectivity, you can interactively find and select data URLs, rather than constructing the URLs yourself. See Binding to an OPC Data Item to learn more about interactively browsing for URLs.

    Connecting to Data Sources and Targets


You can connect a binding interactively in the UI property pages or programmatically in your Visual Basic code. If you want to create a program without writing any code, enable the Auto connect property on the Bindings property page. If you want to programmatically control when the binding connects to the data source or target, disable the Auto connect property on the Bindings property page and call the Connect or ConnectTo method in your code.

When you connect to a data source or target, you have to specify one of the following access modes:
  • Read (cwdsRead) -- Reads data once as soon as the binding is connected. Call the Update method to trigger another read, or, if you want to specify timed intervals at which to automatically update the data, set the Interval property in the property pages or the TimerInterval property programmatically.
  • ReadAutoUpdate (cwdsReadAutoUpdate) -- Reads as soon as the binding is connected and automatically reads again if the data at the data source is updated. You can connect and automatically read data only from OPC and DataSocket servers. If you are connecting to HTTP, FTP, or file data items, use the Read access mode.
  • Write (cwdsWrite) -- Writes the current data to the target once as soon as the binding is connected. Call the Update method to trigger another write, or, if you want to specify timed intervals at which to automatically update the data, set the Interval property in the property pages or the TimerInterval property programmatically.
  • WriteAutoUpdate (cwdsWriteAutoUpdate) -- Writes the current data to the target when the binding is connected. Data is rewritten automatically when the binding property changes.

    Note: Refer to the Measurement Studio Reference for complete reference information about the access modes, connect methods, interval properties, or other CWBinding features.

    Examples -- Creating Bindings


As you are planning your binding application, first decide which UI control best represents your data – a plot on a graph or perhaps the value of a knob or slide. You can bind almost any property on a UI control. Next, configure the binding and test the connection. The following examples show how easy it is to connect to data items on a DataSocket server and an OPC server both interactively and programmatically.

Binding to a DSTP Data Item

The easiest way to create and test a binding is interactively on the Bindings property page of the UI control. Place a CWGraph control on your Visual Basic form, and use its Bindings property page to configure the binding:

1. Click Add to create a new binding.

2. Select the property you want to bind to from the Bindable properties list. For this example, select YDataAppend to chart new data from the server.
    Tip If you want to plot data from a data source, use the YData, XYData, YDataAppend, or XYDataAppend property. Setting the YData and XYData properties are equivalent to calling the PlotY and PlotXY methods, and setting the YDataAppend and XYDataAppend properties are equivalent to calling the ChartY and ChartXY methods. Refer to the Measurement Studio Reference for information about the CWBinding object, these bindable properties, and the Plot and Chart methods they represent.
3. Verify that the ReadAutoUpdate access mode is selected so that new data is automatically displayed as soon as it is available.

4. Specify the data source location with a URL. For this example, enter the following URL, which binds the plot on the graph to the current wind speed value on the National Instruments DataSocket server in Austin, Texas:

      dstp://weather.ni.com/weather/austin/current/windspeed
    Note: To access data items on HTTP, FTP, or remote DataSocket servers, you must know the exact URL of the data item. However, you can interactively browse for items on local or networked OPC and DataSocket servers. Press the Browse for URL (...) button to interactively find and select the item.
5. Press Test and then Connect on the Bindings property page to test the connection. If the connection is active, the preview window displays data from the specified data item, as shown in Figure 1.

[+] Enlarge Image
Figure 1. Testing the Binding from the Property Page

6. Click OK to close the property pages.

7. Run your program. The CWBinding object immediately connects to the data item and displays the windspeeds on the graph. Data continues to be displayed whenever new data is available (that is, the windspeed changes).
    Note The binding connection is automatically made because the Auto connect property was selected in the property page. The next example shows you how to connect the CWBinding object to the data source sometime during runtime, rather than at the start of program execution.

Binding to an OPC Data Item

You can easily modify the existing example to communicate with an OPC server rather than a DataSocket server if your test or control system communicates with devices such as PLCs, RTUs, or National Instruments FieldPoint devices – just update the URL on the Bindings property page. Because OPC data items often have complex URLs, you can use the CWBinding browsing feature to interactively find and select the appropriate data item.
    Note This example connects to the National Instruments OPC Demo server, which was installed with Measurement Studio and DataSocket. Not all OPC servers support browsing. If you can’t browse your OPC server, check its documentation to determine how you can access data items. Refer to Application Note 139, Developing an OPC Client Application Using Visual Basic, for information about OPC and connecting to OPC servers.
This example also explains how to connect the binding during runtime, rather than automatically connecting as soon as the program begins.

1. Click the Browse for URL (...) button and select an item on the National Instruments OPC Demo Server. Figure 2 demonstrates browsing to an OPC data item.
    Tip: The Browse for URL (...) button offers different browsing options for different data sources. If you want to connect to an OPC data item, select Browse for OPC Item to open an OPC server browser. If you want to select an item on a DataSocket server, select Browse for a DSTP item to open a DataSocket server browser. If you want to browse for a local file, type file: in the URL field and then select the Browse from URL option. If you don’t enter anything in the URL field and you select the Browse from URL option, a simple text box is opened in which you can type the URL.

Figure 2. Browsing to an OPC Server Data Item

2. Test the connection. Press Test and then Connect on the Bindings property page. As soon as the connection is active, you will see data displayed in the preview window.

[+] Enlarge Image
Figure 3. Testing an OPC Connection from the Bindings Property Page

3. To make the binding connection during runtime rather than when the program first executes, unselect the Auto connect option. Click OK to close the property pages.

4. Add a button to the user interface named cmdConnectBinding that triggers the binding connection and then add the following code to your program:
    Private Sub cmdConnectBinding_Click()
        CWGraph1.CWBindings(1).Connect
    End Sub
    The CWBinding object uses the properties, including the URL and access mode, already specified in the property pages. If you want to change the URL or access mode during program execution, you can modify them programmatically. The following section shows you how to create this entire OPC example programmatically.

Programmatically Creating a Binding

The Measurement Studio ActiveX controls offer properties, methods, and events to help you programmatically create, control, and respond to data sources and targets. This example implements the previous OPC example entirely from Visual Basic code, so you don’t need to set any binding properties in the property pages. The finished user interface might look like Figure 4.
    Tip: To test this example, you should either delete the existing CWBinding object from the Bindings property page or open a new project.

Figure 4. User Interface for Binding a Graph Property to a Data Source

1. Add the CWBinding object to the CWGraph control. In the following code, the Visual Basic Set function creates a reference named Binding that points to the new CWBinding object on the CWGraph control. When you set properties later in the code, you can just use Binding rather than specifying CWGraph1.CWBindings(1):

     Private Sub cmdConnectBinding_Click()
          Dim Binding As CWBinding
          Set Binding = CWGraph1.CWBindings.Add
          'Connect the binding
          Binding.Connect

2. Define which object and property on the control you are using to bind the remote data source:

     Private Sub cmdConnectBinding_Click()
          Dim Binding As CWBinding
          Set Binding = CWGraph1.CWBindings.Add
          'Specify the object and property for the Binding
          Binding.SetBindObject CWGraph1
          Binding.BindProperty = "YDataAppend"
          'Connect the binding
          Binding.Connect

3. Set the access mode and URL on the CWBinding object:

     Private Sub cmdConnectBinding_Click()
          Dim Binding As CWBinding
          Set Binding = CWGraph1.CWBindings.Add
          'Specify the object and property for the CWBinding
          Binding.SetBindObject CWGraph1
          Binding.BindProperty = "YDataAppend"
          Binding.AccessMode = cwdsReadAutoUpdate
          Binding.URL = "opc:/National Instruments.OPCDemo/sine"
          'Connect the binding
          Binding.Connect
     End Sub
    Tip Use the ConnectTo method to specify the access mode and URL as you connect the data source or target to reduce three lines of code to one:

          Binding.ConnectTo "opc:/National Instruments.OPCDemo/sine", _
               cwdsReadAutoUpdate

4. Run and test the example. In the next two steps, you add finishing touches to make the program more user friendly.

5. Improve the program interface if you plan to distribute the program to others. For example, you might create a text box on the user interface in which users can enter the URL (as shown in Figure 4) so that they don’t have to edit the code to change the data source. If you add a text box named Text1, then modify the URL statements as follows:

          Binding.URL = Text1.Text
    Or use the SelectURL method to enable users to interactively browse to a URL. If users will be connecting to OPC servers, DataSocket servers, or local files, the SelectURL method enables interactive browsing and selection of data items, rather than requiring users to construct often complex URLs themselves. Call the SelectURL method as follows rather than setting the URL property:

          'Browse OPC servers and data items
          Binding.SelectURL "opc:"

          'Browse DataSocket servers and data items
          Binding.SelectURL "dstp:"

          'Browse local files
          Binding.SelectURL "file:"

If you specify the "http:" or "ftp:" protocol with the SelectURL method, the method displays a simple text box in which users can enter HTTP and FTP URLs.
    Note Refer to the Measurement Studio Reference for more information about the SelectURL method, its optional parameters, and other CWBinding features.
6. Add a status indicator so that users can determine if the binding is connecting, active, idle, unconnected or if an error occurred. Place a Visual Basic Label on the form and define the CWBindingStatusUpdated event as follows:

          Private Sub CWGraph1_CWBindingStatusUpdated(ByVal _
            Index As Integer, ByVal Status As Long, ByVal _
            Error As Long, ByVal Message As String)

                'Display status message
                Label1.Caption = Message
          End Sub

Tip: You can query the Status, LastError, and LastMessage properties at any time. Use the StatusUpdated property to determine if the status has changed since the last time you checked it. Check the Measurement Studio Reference for information about these properties.

Processing Data from a Binding



Use a CWBindingDataUpdated event procedure to process data values as they are sent or received. For example, you might want to scale values or ignore them if they don't meet your criteria. The following procedure scales all positive values by 10, while ignoring all negative values:

Private Sub CWGraph1_CWBindingDataUpdated(ByVal Index As _
  Integer, ByVal Data As CWUIControlsLib.CWData, Ignore _
  As Boolean)

       If (Data.Value < 0) Then
              'Don't plot negative values--ignore them
              Ignore = True
       Else
              'Scale each value by a factor of 10
              Data.Value = Data.Value * 10
       End If
End Sub

If you want the UI controls to generate a CWBindingDataUpdated event when data is ready to be sent (in Write mode) or has just been received (in Read mode), enable the DataUpdated event option on the Bindings property page or programmatically set the DataUpdatedEnabled property to True.

Conclusion



Measurement Studio integrated DataSocket connectivity is a powerful feature for communicating with remote data sources from your Visual Basic programs. Use the Measurement Studio UI ActiveX control and property that make the most sense for your application – buttons, knobs, graphs or charts, slides, or numeric edit boxes. You can create and customize bindings interactively through the Bindings property page or programmatically with the simple API. With CWBindings, you can quickly and easily create a Visual Basic program that visualizes data from remote data sources or publishes data to remote targets without code.
5 ratings | 3.80 out of 5
Print

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