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

Developing Industrial Automation Applications using Visual Basic .NET and OPC

11 ratings | 3.91 out of 5
Print

Overview

.NET is quickly becoming a standard for enterprise level applications because of the range of inter-department connectivity provided. This connectivity enables programmers to quickly link information from one department to another. For example, you can link online orders to parts databases to manufacturing lines to shipping databases. This technical application note explains how Microsoft® .NET and National Instruments industrial automation tools connect enterprise applications directly to manufacturing. This application note uses OLE for Process Control, (OPC) - an industry standard industrial communication protocol, to connect your manufacturing hardware to the rest of your enterprise.

.NET: An Enterprise Level Solution

Today, 56 percent of software decision-makers in North American enterprises say they will use .NET for the majority of development (Nicholas, Wilkofff. “The State Of Technology Adoption.” Forrester. May 2004). As .NET continues to expand throughout the enterprise into manufacturing, we need to continually look at ways to connect the enterprise to machines and hardware actually manufacturing products. Consider creating an on-demand personal-computer manufacturing system where customers have the option to fully customize their new PC order online. Immediately after a customer places an order, your application must access parts and human resources databases to verify that all the inputs required for producing a finished computer are available. If a particular part is unavailable, your application automatically places an order for that part. When all parts and workers have arrived, production begins on the custom computer order. Each phase of production communicates with the orders database, which constantly updates the status of this product for the end customer. The powerful web connectivity tools of ASP .NET and XML web services allow order information to be available from anywhere in the world.

You can also integrate the test and control aspects of your manufacturing process such that .NET connects your entire enterprise. In this example, customers have the option to customize their computers, thus each computer has a unique manufacturing process. Producing a computer case requires plastic injection molding which involves setting and monitoring the appropriate temperature for the specific case type. From your .NET application, it would be nice to directly monitor and control this temperature using .NET. This is typically accomplished by connecting to programmable logic controller (PLCs1) or to the next generation of PLCs – programmable automation controllers (PACs). PACs combine PLC ruggedness with PC functionality under open and flexible software architecture. National Instruments Measurement Studio provides powerful and easy-to-use tools to communicate to PLCs and PACs through any OPC server available on your computer using Visual Studio .NET. As shown in Figure 1, .NET allows you to seamlessly connect all the disjoint parts of your business together, including distributed I/O on the manufacturing line through an open communication.

1PLC (Programmable logic controller) is a highly reliable special-purpose computer used in industrial monitoring and control applications. PLCs typically have special-purpose digital and analog I/O ports for data acquisition.


Figure 1. .NET Connects the Enterprise
 

OLE for Process Control

A common way of connecting to PLCs and PACs is via OLE (Object Linking and Embedding) for Process Control (OPC). Before OPC, automation suppliers and systems integrators developed proprietary interfaces to their hardware devices located on the manufacturing floor. The problem with developing these proprietary interfaces is that users had to rewrite code for communicating with different devices because each one used a different API. Years ago, Microsoft introduced OLE, COM, and distributed COM (DCOM) technologies; with them, application software can interoperate and communicate between modules distributed across a computer network. Automation hardware and software vendors, including National Instruments, formed an organization to define the standard for using COM in manufacturing applications. The result was OPC. The OPC Foundation, which was chartered to develop the standard, now consists of more than 300 companies. The foundation's goal is to develop a common interface for connectivity -- an open and interoperable interface standard, based on the functional requirements of OLE and COM technologies.

See Also:
OPC Foundation

Why Do We Need OPC Standards?


With hundreds of major hardware and software vendors, you have the difficult task of making sure that your client application can communicate with any device and driver. Before OPC, you had to write separate client application code to communicate with each device because each device driver used a different API. OPC provides an industry-standard interface so client applications can retrieve process variables, such as temperature, pressure, flow rate, or position, and set control variables, such as velocity and rotation.

OPC offers the following benefits:

  • Single API for all OPC servers so that you can reuse the code for your client application with each device.
  • Opportunity to develop client applications in development environments that take advantage of COM and ActiveX, such as Microsoft Visual Basic, Visual C++, Excel, and Internet Explorer; and Borland Delphi. This application note explains how to implement an OPC client in Visual Basic .NET.
  • Available OPC items available can be browsed. An OPC item is a channel or variable in a real-world device (normally an I/O point) that a device server monitors or controls.
  • Distributed and remote access through DCOM. You can access devices connected to other computers on the network.

 

What Is DataSocket?

DataSocket is a technology for sharing OPC data between applications or data sources. DataSocket provides a simple interface through which to interact with OPC devices from Visual Basic .NET. You can connect to an OPC server with DataSocket using a universal resource locater (URL), which is similar to the URLs used in a Web browser. URLs provide a standard mechanism for referring to inputs and outputs. The standard format for an OPC address is:
opc://machine_name/server_name/item_name

Building an OPC client application in Visual Basic .NET


In this example, you will build a client application that communicates with an OPC server using Visual Basic .NET, Measurement Studio, and DataSocket. You will learn how to browse available OPC items, in addition to binding OPC data to user interface controls in your client application without writing any code. In regards to the temperature monitoring, you build a Visual Basic .NET application that monitors temperature data from a plastic molding chamber. This requires writing a temperature set-point value and then continuously reading the instantaneous values from a sensor placed in the chamber. To achieve this task, you can assign a channel on your PLC or PAC hardware to write your set-point to and another channel to read the instantaneous values of the sensor from. As explained above, you also are connected to these two channels from your .NET application through the OPC server. In order to control and monitor the above two temperature values through your .NET application, this application note explains how to set and read from OPC items. The first section of the example illustrates the use of text code to monitor and display data on the user controls used. The later half of this example describes how data-binding can be used to simply bind OPC data to the user controls and accomplish the tasks interactively without writing any code. To download a free evaluation version of Measurement Studio, please visit the Visual Basic .NET Home Page.

Note: To test the OPC client example, you need an OPC server. You can use the National Instruments OPC Demo server installed with Measurement Studio or you can use any other OPC server that you have installed on your computer. Some PLCs come with their own OPC servers, or you can purchase OPC servers from third-party vendors.

1. Launch Microsoft Visual Studio .NET 2003.

2. Create a new project. Select File New Project. Choose Windows Application from Measurement Studio Projects Visual Basic Projects.


Figure 2. Measurement Studio Project Type

3. Add Measurement Studio class libraries and click Finish.

4. Add the controls as shown in Figure 3.


Figure 3: OPC Client User Interface

Note: To place the controls on the form, select the icon from the Visual Basic Toolbox and click and drag to draw the control. After you place the control, you can use your mouse to move or resize the object. Right click on the object and select Properties to customize its appearance or behavior using the property pages.

a. Place a Measurement Studio .NET WaveformGraph on the form.

b. Place a TextBox on the form. Right click on the TextBox and select Properties. Change the Name property to URLRead and the Text property to opc:/National Instruments.OPCDemo/sine. Add a Label to identify the TextBox control on the user interface. Change its Text property to URL (Read).

Note: The URL points to a demo OPC server installed with Measurement Studio. Later in the example, you will learn how to browse for OPC servers so that you can connect to any installed on your computer.


c. Place a Label on the form in which to display the status of the connection. In the Properties window, change the Name property to StatusRead and Border Style to Fixed Single. Delete the default text for the Text property. Add another Label to identify this control on the user interface. Change its Text property to Status (Read).

d. Place a CommandButton control on the form. In the Properties window, change the Name property to ConnectBtn and the Text to Connect to OPC Server.

e. Place another CommandButton on the form and change Name to DisconnectBtn and Text to Disconnect from OPC Server.

f. Place a Measurement Studio .NET DataSocket control on the form.


5. Add Visual Basic .NET code to make the application respond to interactions from users and the OPC server.

Tip: In Visual Basic .NET, you can double click on a control to generate an event handler routine, which includes the control name, default event, and any parameters that are passed to the event handler routine. For example, to generate an event handler to connect to the OPC server, double click on the Connect to OPC Server button on the form.

a. Users will connect to an OPC server by clicking on the Connect to OPC Server button. To generate an event handler, double click on the Connect to OPC Server button and add the following code:

Private Sub ConnectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectBtn.Click
DataSocket1.Connect(URLRead.Text, DataSocket1.AccessMode.ReadAutoUpdate)
End Sub

The Connect method connects to the URL specified in the URLRead text box and automatically sends data to the client application as soon as it is available.

Tip Use the Measurement Studio Online Reference to find information about the Measurement Studio ActiveX controls and their properties, methods, and events. You can launch the help from the Windows Start menu, the Measurement Studio custom property pages, or the Visual Basic .NET Code window (highlight a property, method, or event and press <F1>).

b. DataSocket automatically returns new data from the OPC server as soon as it is available. To chart the data as DataSocket returns it to the application, double- click on the DataSocket control to generate the DataUpdated event and add the following code:
Private Sub DataSocket1_DataUpdated(ByVal sender As System.Object, ByVal e As NationalInstruments.Net.DataUpdatedEventArgs) Handles DataSocket1.DataUpdated
WaveformGraph1.PlotYAppend(e.Data.Value)
End Sub


c. To display the OPC server connection status (for example, whether DataSocket is connected or unconnected) in the StatusRead label, double-click on the DataSocket control again, which will show the above generated DataUpdated event. Select the ConnectionStatusUpdated event from the Method Name drop-down list and add the following code:

Private Sub DataSocket1_ConnectionStatusUpdated(ByVal sender As Object, ByVal e As NationalInstruments.Net.ConnectionStatusUpdatedEventArgs) Handles DataSocket1.ConnectionStatusUpdated
StatusRead.Text = e.Message
End Sub
d. Users will press the Disconnect from OPC Server button to disconnect. To generate an event handler, double click on the Disconnect from OPC Server button and add the following code:
Private Sub DisconnectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectBtn.Click
DataSocket1.Disconnect()
End Sub

6. Save the project and form as OPC Client.

7. Select Debug»Start to run your program

8. Press Connect to OPC Server to connect to the default OPC server, National Instruments.OPCDemo. DataSocket charts each data point as it is received. Notice that the status message confirms that DataSocket is currently connected to the OPC server.

9. Press Disconnect from OPC Server to disconnect DataSocket. Notice that the status message confirms that DataSocket is unconnected.

10. Select Debug»Stop Debugging to close the OPC client application.


Writing Data to OPC Servers

To effectively communicate with a device, you need to write to it as well as read from it. To write to the OPC server, you need another DataSocket control to connect in write mode, a status message to display the connection status, and a numeric edit box to specify the write value. You also should allow users to browse for the OPC item that they want to write to with the existing Browse for OPC Item button and disconnect from the OPC server with the existing Disconnect from OPC Server button. Figure 4 shows the OPC client with the write feature.


Figure 4. OPC Client Extended to Write to an OPC Item

1. Add the controls as shown in Figure 4.

a. Place a TextBox on the form. Right click on the TextBox and select Properties. Change the Name property to URLWrite, and the Text property to opc:/National Instruments.OPCDemo/MotorRPMSetpoint. Add a label to identify this control. Change its Text property to URL (Write).

b. Place a Label on the form in which to display the status of the connection. In the Properties window, change the Name property to StatusWrite and Border Style to Fixed Single. Delete the default text for the Text property. Add a label to identify this control. Change its Text property to Status(Write).

c. Place a NumericEdit control on the form and change its name to WriteValue. Add a label to identify this control as Write Value.

d. Place another DataSocket control on the form.


2. Add Visual Basic .NET code to make the application respond to interactions from users and the OPC server.

a. To connect both DataSocket controls when users press the Connect to OPC Server button, add the bolded code to the ConnectBtn_Click subroutine:

Private Sub ConnectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectBtn.Click
DataSocket1.Connect(URLRead.Text, DataSocket1.AccessMode.ReadAutoUpdate)
DataSocket2.Connect(URLWrite.Text, DataSocket2.AccessMode.WriteAutoUpdate)
End Sub

b. To display the OPC server connection status, double click on DataSocket2 and add the following code to the ConnectionStatusUpdated event:
Private Sub DataSocket2_ConnectionStatusUpdated(ByVal sender As Object, ByVal e As NationalInstruments.Net.ConnectionStatusUpdatedEventArgs) Handles DataSocket2.ConnectionStatusUpdated
StatusWrite.Text = e.Message
End Sub

c. DataSocket should automatically write the value to the OPC server as soon as the user enters it. To write the value, double click on the NumericEdit control and add the following code:
Private Sub WriteValue_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WriteValue.ValueChanged
DataSocket2.Data.Value = WriteValue.Value
End Sub
d. To chart the write value, you have to add a second plot to the graph through the property pages and one line of code to the program. Right click on the graph control and select Properties. Under the Graph>>Plots property, click the Add button to add a second plot and change the plot color to pink. Click OK.
Add the bolded line of code to the DataSocket1_DataUpdated event:

Private Sub DataSocket1_DataUpdated(ByVal sender As System.Object, ByVal e As NationalInstruments.Net.DataUpdatedEventArgs) Handles DataSocket1.DataUpdated
WaveformGraph1.PlotYAppend(e.Data.Value)
WaveformGraph1.Plots(1).PlotYAppend(DataSocket2.Data.Value)
End Sub

e. Users will expect to browse for both URLs if they press the Browse for OPC Item button. To allow them to browse for both, add the bolded code to the BrowseBtn_Click subroutine:

Private Sub BrowseBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseBtn.Click
URLRead.Text = DataSocket1.Url
DataSocket1.Connect(URLRead.Text, DataSocket1.AccessMode.ReadAutoUpdate)

URLWrite.Text = DataSocket2.Url
DataSocket2.Connect(URLWrite.Text, DataSocket2.AccessMode.WriteAutoUpdate)
End Sub

f. When users press the Disconnect from OPC Server button, they will expect both servers to be disconnected. To disconnect the second OPC server, add the bolded code to the DisconnectBtn_Click subroutine:
Private Sub DisconnectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectBtn.Click
DataSocket1.Disconnect()
DataSocket2.Disconnect()
End Sub
3. Select Debug»Start to run the OPC client.

4. Change the URL (Read) to opc:/National Instruments.OPCDemo/MotorRPMSensor.

5. Press the Connect to OPC Server button.

6. Try writing different values to the OPC server. Notice how the chart data adjusts to approach the setpoint you specify. When the value reaches the setpoint, no more data is charted because no more events are being fired, but you can continue to adjust the write value and see the adjustment reflected in the chart. Figure 5 shows the finished OPC client application.


Figure 5. Finished OPC Client Application

7. Save the project.


Data-binding approach:

In the above example, you saw how Measurement Studio can be used with Visual Basic .NET to interface with the data from your manufacturing system. You wrote simple code to execute different callback functions for different user events. To simplify this even further, Measurement Studio provides data-binding options, using which you can easily connect a particular control or an indicator to a data-source. This eliminates the need to write callback functions. This is illustrated further in the example developed in the following section, where you will control and monitor the data using data-binding options.

Follow the first 3 steps given under the section: “Building an OPC client application in Visual Basic .NET” to launch a new Measurement Studio project.

1. Place a Measurement Studio .NET WaveformGraph on the form. Keep its default properties.

[+] Enlarge Image
Figure 6. Measurement Studio Graph and DataSocket

2. Place a DataSocketSource control on your form.

3. Right click on the DataSocketSource that you just created and click on Edit Bindings.

4. Select Add to add a new binding. To browse all available OPC servers and to specify the item you want to read and write, put your cursor on the Url property and select the ellipsis. In this case, select the simulated MotorRPMSensor from the NI OPC Demo Server. After you press OK, the OPC URL is
opc://localhost/National Instruments.OPCDemo/MotorRPMSensor


Figure 7. Select the OPC Server

You can select your actual temperature channel from PLC or PAC hardware here to read from.

5. Repeat steps 1 through 4 above to add another WaveformGraph and DataSocketSource control, except in this case, choose the MotorRPMSetPoint with the following Url from the Url binding property:
opc://localhost/National Instruments.OPCDemo/MotorRPMSetPoint

6. Place a NumericEdit control and another DataSocketSource control on the form. Choose the MotorRPMSetPoint item from the Url binding property and change the AccessMode to ReadWriteAutoUpdate.

7. Right-click on the first WaveformGraph1 and select Properties. Change the BindingData property under DataBindings to DataSocketSource2>>Binding1 as shown in Figure 8. Change the BindingMethod to PlotYAppend.


Figure 8. Data binding

8. Select DataSocketSource1>>Binding1 for WaveformGraph2 and DataSocketSource3>>Binding1 for NumericEdit. Change the BindingMethod for WaveformGraph2 to PlotYAppend.

9. To run this application and display the live data from your OPC server, press F5. As shown in Figure 9, you can change the set-point and monitor the sensor values.


[+] Enlarge Image
Figure 9. OPC Client application with Data-binding
 

Improved OPC Communication with Network Variables

The network variable library introduced in Measurement Studio 8.1 provides a simplified communications API optimized for data exchange between applications on the same system, or across the network.  Network variables provide premier performance when communicating with OPC clients that have a large number of channels or transfer large amounts of data.  You can use network variables to communicate with OPC clients if you have the LabVIEW Datalogging and Supervisory Control (DSC) Module or the DSC Run-Time Engine installed on the machine that will host the Network Variable Engine and the OPC variables.

In addition, the OPC Foundation is developing a .NET API (the Universal Architecture, or UA) that will be available to all OPC Foundation members.  The UA will also provide you with a way to communicate with OPC clients from Visual Basic .NET.

Conclusion

In this application note, you learned how to connect your manufacturing data to the rest of your enterprise using Microsoft Visual Basic .NET and National Instruments Measurement Studio. You developed two different applications for this task. In the first example, you wrote callback functions, whereas in the second example, you used data binding options to connect the OPC data to your controls and indicators.

Related Links:
Measurement Studio Home Page
Evaluate Measurement Studio for Visual Basic .NET

11 ratings | 3.91 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/).