Using Visual Basicwith OPC
Using Visual Basicwith OPC
An OPC Client
John Weber
President & Founder
Software Toolbox, Inc.
[email protected]
Presentation Updated 3/2001
website: http://softwaretoolbox.com
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Agenda
• OPC - Review of the Basics
• OPC - Terminology
• OPC & VB 101 - Automation Wrapper & Object Model
• Building Your VB Client - Read data - 7 easy steps with
code from start to finish
• Housekeeping
• Handles Handles Everywhere - how the server and client
correlate the data each is managing in your program
• Writing Data
• Future ideas to consider
• New developments since original presentation in 10/99
• Resources for future learning
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC OPC - Review of the Basics
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC OPC and VB 101
Object model for the Automation Wrapper - the wrapper lets the VB user connect to
an OPC server using standard Object.property and Object.method syntaxes
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC OPC and VB 101
OPC Server Object
Your VB Program OPC Groups Collection OPC Browse Object
with the Automation OPC Group Objects
Wrapper object
OPC Items Collection
included in it
OPC Items Objects
The Automation Wrapper
connects to the OPC server
and creates the groups and COM/DCOM
items in the server and gives
you references to them in your
VB program in an Object OPC Server Object
model that mirrors that of the
server
OPC Groups Collection
Activate De-Activate
Groups & Items Groups & Items
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB Tools You’ll Need To Build Your
OPC
Application
• If you want to build an OPC client in VB and test
it, you’ll need the following tools:
– Visual Basic 5 or 6 running on Windows 95, 98, or NT
any VB edition will do the job
– An OPC Server
– The OPC Automation Wrapper
• You can get the last two items including a sample
test OPC server at softwaretoolbox.com (see last
slide for exact link). There is no charge for the
Automation Wrapper and sample OPC
demonstration servers are free also.
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 1
• Install the OPC
Automation Wrapper DLL
on your PC
• Start a new VB project
• In VB, click on Project -->
References on the VB
menu bar
• The OPC Automation
Wrapper appears on the
dialog as “OPC
Automation 2.0” - select it
as shown here.
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 2
• First you need to declare some variables in the VB
code window in the General Declarations area as
shown here
Option Explicit
Option Base 1 ‘Makes all arrays start with an index of 1
Dim WithEvents AnOPCServer As OPCServer These lines create objects you will
Dim WithEvents ConnectedOPCServer As OPCServer use to manage your OPC server
Dim ConnectedServerGroup As OPCGroups connection and a group - you could
Dim WithEvents ConnectedGroup As OPCGroup add more than one group if you
wanted to
Dim OPCItemCollection As OPCItems
Dim ItemCount As Long These lines create objects you
Dim OPCItemIDs(10) As String will use to manage your OPC
Dim ItemServerHandles() As Long Items - we are setting up our
Dim ItemServerErrors() As Long sample to read 10 items - you
Dim ClientHandles(10) As Long setup as many as you need
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 3
• If you want your VB project to connect to your server at
startup, use the name of your server (ask your server vendor)
and enter the following code in the Form Load subroutine for
your project - our server name is “KepServer”
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 4
• Next, you’ll go ahead and add a group right after you get your
connection to the OPC server. Enter this code right after your code for
connecting to the server in the Form Load subroutine of your project
'Prepare to add a group to the current OPC Server
' Get the group interface from the server object
Set ConnectedServerGroup = ConnectedOPCServer.OPCGroups
'Set the desired percent deadband - enter an integer from 0 to 100 for the deadband
ConnectedServerGroup.DefaultGroupDeadband = 0
' Add the group and set its update rate - enter whatever group name you want in place of “DataGroup1”
Set ConnectedGroup = ConnectedServerGroup.Add(“DataGroup1”)
' Set the update rate for the group - enter an long integer value representing the millisecond group update rate
ConnectedGroup.UpdateRate = 500
‘ The following line is crucial -- without it you won’t be subscribed to the server and DataChange events will not fire!
ConnectedGroup.IsSubscribed = True
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 5
• Next you’ll go ahead and add some items. This code follows right
after the group add code in the Form Load subroutine
• For the item names, enter valid item names for your OPC server.
Refer to your OPC server’s documentation for valid item naming
conventions. We are going to be reading some data from a GE PLC
here so we will use their memory conventions. The OPC server we
are using uses the syntax “itemname@updaterate”
ItemCount = 4
Dim i As Integer
For i = 0 To 3
‘This line builds a string like “GE9030.R1@10” - a valid item name for the OPC server we are using
OPCItemIDs(i + 1) = “GE9030.R” & (I + 1) & “@10”
ClientHandles(i + 1) = I ‘ Sets a reference pointer number for this point
OPCItemActiveState(i).Value = 1 ‘ Tells the server we want this item to be active
Next I
Set OPCItemCollection = ConnectedGroup.OPCItems ‘Gets an items collection from the current Group
OPCItemCollection.DefaultIsActive = True ‘Sets the items collection to active
‘This line adds the items we’ve chosen to the items collection and in turn to the group in the OPC Server
OPCItemCollection.AddItems ItemCount, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 6
• Now, assuming you have all valid item names and your
OPC server is ready to go, all you need to do is add code to
react to the DataChange events that your OPC server will
fire back to the Automation DLL, which in turn will fire an
event in VB for you
• In your program, you created an object variable called
“ConnectedGroup” in step 2 -- this object will fire an event
called “DataChange” for anytime one of the items in your
group has new data for you
• The DataChange event tells you how many items changed,
gives you back the client handles so you know which ones
changed, and the data, quality, and timestamp information
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 6
• Go ahead and build the objects on your form to display your
data.
• For our form, we built one with 3 arrays of text boxes -- one
each for the data, quality, and timestamp information. We
named them txtData, txtQuality, txtTimeStamp respectively.
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 6
• Now in the VB Code window, use the combo boxes at the top to go to the
ConnectedGroup object’s DataChange event subroutine -- enter this code to
update your text boxes.
• Note this code assumes you have named your text boxes as we have in our
example and built an array of text boxes as we have in our form. If you named
your text boxes differently, then you will need to adjust this code accordingly.
• The variables ItemValues, Qualities, ClientHandles, and TimeStamps are all
variables passed to you by the Automation Wrapper DLL and your Connected
Group object when the DataChange event fires
Dim i As Integer
The Qualities data array can
For i = 1 To NumItems
contain a variety of very
txtData(ClientHandles(i)).Text = ItemValues(i)
hardware specific data about
If Qualities(i) And &HC0 Then
why communications failed - a
txtQuality(ClientHandles(i)).Text = "Quality Good" value of Hex C0 is returned
Else when everything is OK
txtQuality(ClientHandles(i)).Text = "Quality Bad"
End If
Timestamps are returned in
txtTimeStamp(ClientHandles(i)).Text = TimeStamps(i)
GMT or Universal Time
Next i
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Building Your VB Client - Step 7
• Now if you have not done so, save your project and form
in VB
• Run your project. If you have specified a valid OPC server
and Item names and your OPC server is running and ready,
then when you run your project, it should immediately start
updating the text boxes on your form
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC If things aren’t working . . .
• If you do not have communications, check
the following
– Do you have the OPC server name specified
correctly in the form load routine ? Check with
your OPC server vendor for the right name to
use
– Are you using valid item naming syntaxes for
your pariticular OPC server?
– Do you have the cabling and hardware setup
right between your OPC server and your
hardware?
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Housekeeping
• This program was a very basic one -- in reality you need to add at least
one more section of code in order to make sure you clean up your
connections to the OPC server before you exit your program.
• Otherwise the OPC server will still think you are connected and hold
open memory to service you that it could otherwise release.
• Enter the code on the following slide in the Click ( ) event on the
“Disconnect and Exit” command button you put on your form
• This code makes sure you remove the item, items collection, group,
and groups collections from the server then disconnect from the server
upon exit.
1 0 38145856
2 1 38145968
3 2 38146080
4 3 38146192
Assigned by your program and passed to the Assigned by the OPC Server and
OPC Server when you call the .AddItems passed back to your program by the
method. Should be numbers that are useful Automation Wrapper DLL when the
to you in your code in working with and AddItems method completes and
manipulating the data returns
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Writing a Value - Step 1
• Of course now that you are reading data, you want to write some data.
Add a text boxes and an array of command buttons to your form as
shown below. Name your text boxe txtValueToWrite and the
command button array cmdWriteValue. For simplicity, we will stick to
just writing items that you already subscribed to when we loaded the
form.
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Writing a Value - Step 2
• When you click on the write command buttons, the event handler in VB
hands you an index from 0 to 3 which matches up to the items that we
added when the form loaded. The following code performs an OPC
Synchronous Write to the chosen item - place this code in the
cmdWriteClick event in your VB program
' Write only 1 item this time
ItemCount = 1
' Create some local scope variables to hold the value to be sent.
' These arrays could just as easily contain all of the item we have added.
Dim SyncItemValues(1) As Variant
Dim SyncItemServerHandles(1) As Long
Dim SyncItemServerErrors() As Long
' Get the Servers handle for the desired item. The server handles
' were returned when we loaded the form. We use the index of the command button clicked to tell us which item to change
SyncItemServerHandles(1) = ItemServerHandles(Index + 1)
' Invoke the SyncWrite operation. Remember this call will wait until completion
ConnectedGroup.SyncWrite ItemCount, SyncItemServerHandles, SyncItemValues, SyncItemServerErrors
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC Writing a Value - Step 3
• Run your program. Assuming of course that your PLC or
device will let you write the points you are reading, you
should now be able to enter a value in the text box and
write a data point and see it change in the display where
you were reading data.
• Words of wisdom on writing
– You can’t write to an item that you haven’t added to the items
collection.
– You don’t have to be reading an item in order to write it -- but it
must be already added to the current group by your client program
– Synchronous writes will take control and execute with high
priority
– Asynchronous writes are available for lower priority writes -- see
the OPC Automation Specification for syntax for Asynch Writes
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC From Here . . .
• From here you would want to build in some error
handling. This sample assumes everything works
and does not trap errors
• Download more samples (see last slide for links)
for sample code including error handling
• Once you have the data back into your VB
program (Data Change event), you can pass the
values to any graphical ActiveX display object on
the same form or a different form
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC From Here . . .
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB New developments since original
OPC
presentation created . . .
• www.OPCActiveX.com -
– ActiveX control that encapsulates all the details for you
– Read data into VB with no code needed
– Write data with one line of code
– Built in tool to browse remote servers and tags
– Robust DCOM support
– Tested with 40+ commercial OPC servers
• You can build yourself for free, or license existing
technology at low prices - your choice
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC The Possibilities . . . .
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.
VB
OPC OPC Resources
• OPC Foundation - www.opcfoundation.org
• Samples - softwaretoolbox.com/isaexpo99
– The program created by this document
– A more advanced program
– Sample OPC Servers
– Automation Wrapper DLL install set
– This document in Powerpoint (*.ppt) format
– OPC Automation 2.0 specification document
• Commercially supported tool to save code writing -
www.OPCActivex.com
• Contact the presenter - email to
[email protected]
Copyright Software Toolbox, 1999,2000,2001 All Rights Reserved Worldwide. Software Toolbox is a registered trademark of Software Toolbox, Inc.