65.9K
CodeProject is changing. Read more.
Home

OPCWare DA Automation Wrapper - VB.NET

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.55/5 (9 votes)

Apr 23, 2007

CPOL

3 min read

viewsIcon

103555

This paper provides the basic technical overview of OPC (OLE for Process and Control) and a sample VB.NET OPC Client to access the data using OPCware DA Automation Wrapper component. It describes how VB.NET client can communicate with OPC Server.

OPCWare DA Automation Wrapper - VB.NET

Summary

This paper provides the basic technical overview of OPC (OLE for Process and Control) and a sample VB.NET OPC Client to access the data using OPCware DA Automation Wrapper component. It describes how VB.NET client can communicate with OPC Server. The intended audiences of this paper are software developers. (This paper assumes that the reader has the fundamental knowledge of OPC and VB.NET)

Introduction

OPC (OLE for Process and Control) is an industrial standard to access data from process control devices. It is a standard to exchange data between systems. This has set of specifications to communicate different multi vendor devices and control applications. OPC specifications are maintained by OPC foundation. The following next section will explain the need of OPC and the goal of OPC foundation.

Why OPC and OPC Foundation

In the past, manufacturers have many different data sources such as PLCs, DCSs, databases and other devices. Vendors would capture this data in their application using the own device interfaces. The data can be accessed only through the vendor specific propriety interfaces. So, to access the data, the manufacturers should always forced to depend upon the vendors. So, the manufacturers looked to forward an enabling technology that helps the manufacturers to move beyond propriety solutions. OPC has introduced to address this by providing an industrial communication standard the enables exchange of data between multimedia devices without any proprietary restrictions.

OPC provides open standard for the communication between multi vendors for the communication between multi vendor devices. These open standards are formed and maintained by a group "OPC foundation"

The major goal of OPC Foundation is to eliminate vendor specific restrictions. So the hardware manufacturer need only provide a single OPC Server for their devices to communicate with any OPC Client. Software vendors simply include OPC client capabilities in their products and they become instantly compatible with other thousands of hardware devices.

The following section will explain the basic overview of OPC and the client communication.

How OPC works

Screenshot - image002.jpg

At high-level, OPC consists of several objects like OPC Server, OPC Groups and OPC Items. OPC Server acts as a container for OPC Groups. OPC Group contains logically organized OPC Items. There are two types of OPC groups namely public and private. Public groups can be accessed by multiple clients and the private group is for a single client. OPC items represents connections to the data sources. OPC item will not have any custom interface, so OPC Client can access OPC Items by OPC Group. Each OPC Item is a Value, Quality and Timestamp. OPC Items are not data sources, they are just connection to the data sources. When the OPC Client wants to communicate with OPC Server, it need to instantiate an OPC Server Object/OPC Groups. Once the connection is established, the OPC Item can be accessed by OPC Group and OPC Item objects. The following section will provide a code snippet for a basic OPC Client application.

Sample OPC Client program

This section contains a sample code snippet, which act as a OPC Client program which will read and write data to OPC Server. This code uses "Matrikon.OPC.Simulation" as the OPC Server. (The simulation server can be downloaded from Internet)

Screenshot - image004.jpg
Fig. Matrikon.OPC.Simulation Server Sample OPCClient Items

The following sample code snippet is developed in VB.net
Option Strict Off
Option Explicit On
'########## Include the reference "OPCWare DA Automation Wrapper"
Imports OPCAutomation
Imports System.Runtime.InteropServices
Public Class Form1
Dim WithEvents ObjOPCServer As OPCAutomation.OPCServer
Dim WithEvents ObjOPCGroups As OPCAutomation.OPCGroups
Dim WithEvents ObjOPCGroup As OPCAutomation.OPCGroup
Dim ObjOPCItems As OPCAutomation.OPCItems
Dim ObjOPCItem As OPCAutomation.OPCItem
Const NoOfItems = 2
Dim Array_Items(NoOfItems) As String
Dim Array_ClientHanlers(NoOfItems) As Integer
Dim Array_ServerHandlers(NoOfItems) As Integer
Dim Array_Values(NoOfItems) As Object
'########## Form Load Event  ###################################
Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
 Call StartOPCClient("Matrikon.OPC.Simulation.1")
 Call ReadData()
 Call WriteData()
End Sub
'################################################################
 '########## Sub procedure to read OPC Item values ##############
Private Sub ReadData()
Try
Dim ObjOPCItem As OPCAutomation.OPCItem
For Each ObjOPCItem In ObjOPCGroup.OPCItems
    ObjOPCItem.Read(OPCAutomation.OPCDataSource.OPCDevice)
    Array_Values(ObjOPCItem.ClientHandle) = ObjOPCItem.Value
    MsgBox(ObjOPCItem.ItemID & ":-> " & vbCrLf & "Value:" & ObjOPCItem.Value &_
            vbCrLf & "TimeStamp:" & ObjOPCItem.TimeStamp & vbCrLf &_
            "Quality:" & ObjOPCItem.Quality)
Next
ObjOPCItem = Nothing
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "OPC Client")
End Try
End Sub
'################ Sub procedure to initialize OPC Server ######
Private Sub StartOPCClient(ByVal OPCServerName As String)
Dim liCounter As Integer
Dim gpServerHandlers As Array
Dim gsErrors As Array
Try
            ObjOPCServer = New OPCAutomation.OPCServer
'##### Initialize OPC Server ################################
            ObjOPCServer.Connect(OPCServerName, "")
            ObjOPCGroups = ObjOPCServer.OPCGroups
            ObjOPCGroup = ObjOPCGroups.Add("OPCGroup1")
'Add OPCGroup
            ObjOPCGroup.UpdateRate = 1000
            ObjOPCGroup.IsActive = False
            ObjOPCGroup.IsSubscribed = ObjOPCGroup.IsActive
            ObjOPCItems = ObjOPCGroup.OPCItems
            'Build OPCItems Array
            For liCounter = 1 To NoOfItems
                Array_Items(liCounter) = "OPCClient.Device" & liCounter
                Array_ClientHanlers(liCounter) = liCounter
            Next
            'Add OPCItems
            ObjOPCItems.AddItems(NoOfItems, Array_Items,Array_ClientHanlers, _
                                 gpServerHandlers, gsErrors)
            'Get the server handlers
            For liCounter = 1 To NoOfItems
                If gsErrors(liCounter) = 0 Then
                    Array_ServerHandlers(liCounter) = gpServerHandlers(liCounter)
                Else
                    MsgBox("Item" & liCounter & "has problem", _
                           MsgBoxStyle.Critical, "OPC Client")
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "OPC Client")
        End Try
    End Sub
'#################### Sub procedure to write OPC Item data 
    Private Sub WriteData()
        Try
            Dim liCounter As Integer
            Dim Errors As Array
            '##### Assign new values
            For liCounter = 1 To NoOfItems
                Array_Values(liCounter) = CStr(liCounter * Rnd() * 100)
            Next
            'Write back to OPC Server
            ObjOPCGroup.SyncWrite(NoOfItems, Array_ServerHandlers, _
                                 Array_Values, Errors)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "OPC Client")
        End Try
    End Sub
End
Class

Conclusion

With OPC the industrial automation has improved in integration and scalabilities. Vendors of different automation system will not spent money on developing software components compatible with other vendor products.

OPC makes it possible to have plug-and-play software and hardware across the spectrum of vendors, devices, software and systems that manufacturing can easily integrate into corporate wide automation and business systems.