Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Article

OPCWare DA Automation Wrapper - VB.NET

Rate me:
Please Sign up or sign in to vote.
2.55/5 (9 votes)
23 Apr 2007CPOL3 min read 101.5K   39   12
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
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionReadData() Pin
Member 1161482417-Jun-15 23:13
Member 1161482417-Jun-15 23:13 
QuestionOPCAutomation group assign Error Pin
shoaibmqureshi10-Jun-15 0:59
shoaibmqureshi10-Jun-15 0:59 
Questionsetup opc Pin
Member 1161052429-Apr-15 7:27
Member 1161052429-Apr-15 7:27 
Answerhow to setup form to connect to plc Pin
Member 1161052415-Apr-15 9:44
Member 1161052415-Apr-15 9:44 
Questiongood example but it does n't work with Visual Basic 2008 Pin
BERTEAUX4-Aug-11 6:53
BERTEAUX4-Aug-11 6:53 
GeneralSample in C sharp Pin
Majid Ahrar23-Mar-10 8:54
Majid Ahrar23-Mar-10 8:54 
Generalnice article , please send me ur e-mail id Pin
ManasAddy7-Jan-10 12:34
ManasAddy7-Jan-10 12:34 
GeneralRe: nice article , please send me ur e-mail id Pin
ZbynekZ23-Mar-14 23:19
ZbynekZ23-Mar-14 23:19 
QuestionHow can I do get online data on the server without user's event Pin
Yekbunn30-Jul-08 4:22
Yekbunn30-Jul-08 4:22 
QuestionTypesafe? Pin
Tom2Tom26-Jun-07 4:46
Tom2Tom26-Jun-07 4:46 
GeneralBetter format needed! Get rid of the spaces! Pin
irrdev23-Apr-07 14:57
irrdev23-Apr-07 14:57 
GeneralRe: Better format needed! Get rid of the spaces! Pin
lost in transition 23-Apr-07 16:08
lost in transition 23-Apr-07 16:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.