OPCExplorer
OPCExplorer
OPC Explorer
A simple OPC Client
Web: http://www.hilscher.com
List of revisions 2
Although this program has been developed with great care and intensively tested, Hilscher
Gesellschaft für Systemautomation mbH cannot guarantee the suitability of this program
for any purpose not confirmed by us in writing.
Guarantee claims shall be limited to the right to require rectification. Liability for any
damages which may have arisen from the use of this program or its documentation shall be
limited to cases of intent.
We reserve the right to modify our products and their specifications at any time in as far as
this contributes to technical progress. The version of the manual supplied with the program
applies.
1 INTRODUCTION ............................................................................................4
1.1 The OPC Standard ........................................................................................................... 4
1.2 Installation........................................................................................................................ 5
2.6 Loopbacks...................................................................................................................... 12
1 Introduction
The growing market of software based Automation Control Systems created the
demand for a common and uniform way for applications to access process data from
several sources, like fieldbus devices or database systems of different vendors. A
widely accepted solution to this problem is the OPC (OLE for Process Control)
Specification, a software interface based on Microsoft’s COM/DCOM (Component
Object Modell/Distributed Component Object Modell) technology. Hilscher’s fieldbus
system configurator SyCon includes OPC server functionality according to the OPC
Data Access Specification 2.0.
The OPC specification defines a client/server-system, where typical servers access
device data using appropriate manufacturer specific drivers and protocolls and serve
multiple OPC clients with this data. The OPC Data Access Interface Standard provides
the user with a set of functions to write or read process data synchronously or
asynchronously (polling) or to subscribe for data change notifications at a server
(„push/pull“ mechanism, event based).
S yC o n O P C S e r v e r fo r O P C - C lie n t
s ym b o lic d a ta a p p lica tio n
E x c h a n g e w ith O P C (e .g . V isu a liz a tio n )
D e v ic e D r iv e r fo r W in d o w s N T /9 5 P R O F I B U S -D P / F M S
In t er b u s
C AN open
H ilsc h e r d e v ice (e .g . C IF )
D e v i ceN et
F i el d b u s s y st e m s: C o n tr o lN et
Rather than beeing a simple device driver, an OPC server introduces a level of
abstraction for data access providing client access by means of symbolic names to
hide the details of the underlying transport protocoll, hardware or algorithm, which is
used to collect the real data. It does data type conversion, if requested by the client,
and allows grouping of client specific subsets of data with different requirements in
resolution and refresh time, if using the push mechanism, but it can not be used to
configure the underlying communication systems.
The usage of Microsofts COM technologie as implementation basis eases the
development of modular and interoperable middleware and applications, but via the
DCOM extensions also allows the user to distribute the components of a complex
Process Control and Visualizing System across different machines connected to a
standard network system.
The following chapters describe a simple OPC client, which may be used to check if
Hilscher’s system configurator/OPC server SyCon is properly installed and working, but
also to give interested developers an example about how to communicate with an OPC
Server using Microsofts Visual C++.
1.2 Installation
Windows 95/98:
To use the OPC Explorer, the following system components have to be installed:
• Service Pack 1.
• DCOM95/98 and DCM95CFG/ DCM98CFG.
DCOM95/98 and DCM95CFG/ DCM98CFG are extensions of the Windows 95/98
operating system which are not part of this software package. They can be obtained
free of charge at the Microsoft home page
http://www.microsoft.com/com/recources/downloads.asp
and are part of the Internet Explorer 4.x/5.x.
• Systemconfigurator SyCon (Version 2.1.4.0 or above) with installed OPC support or
any other OPC Data Access 2.0 Specification compliant OPC server.
• Various standard OPC files with OPC proxy dynamic link libraries, automation
interface wrapper dynamic link library and the OPC server browser component.
This files are available for downloaded at the OPC foundation web site
http://www.opcfoundation.org,
but for convenience are also packaged to a single installation binary called OPC
Data Access Components (OPC_DA20_Components.exe), which is made available
to the public free of charge by FactorySoft, Inc.
http://www.factorysoft.com/Downloads/OPC_components.htm
Windows NT 4.0:
To use the OPC explorer, the following system components must be installed:
• Service Pack 3
• Systemconfigurator SyCon (Version 2.1.4.0 or above) with installed OPC support or
any other OPC Data Access 2.0 Specification compliant OPC server
• Various standard OPC files (see the Note in the Win95/Win98 section)
The Explorer Application is provided as source code, which requires Microsoft Visual
C++ (6.0) for compilation. It uses the ATL (ActiveX Template Library) 3.0. An
executable Explorer.exe is also provided, but requires the ATL dynamic link library
(ATL.DLL) to be installed and registered on the system, which is done for example by
the Microsoft Visual Studio setup.
Appart from the applications source code the following files are required to build the
application:
• OPCComn.h – Definition of the OPC Common Interface in C/C++ syntax
• OPCComn_i.c – Definition of the Interface Identifiers (IIDs) for the Common COM
interface
This files are produced by Microsofts MIDL-Compiler from the IDL (Interface Definition
Language) file OPCDa.idl.
• OPCDa.h – Definition of the OPC Data Access Interface in C/C++ syntax
• OPCDa_i.c - Definition of the Interface Identifiers (IIDs) for the Data Access COM
interface
This files are produced by Microsofts MIDL-Compiler from the IDL file OPCComn.idl.
• opcenum_clsid.c – Definition of the Interface Identifiers (IIDs) for the Server
Browsers COM interface
• OPCError.h – OPC specific error codes
This files together with the IDL files are distributed from the OPC Foundation. Search
the Technical Info Section for Common Downloads for Programmes at
http://www.opcfoundation.com
The basic entities of the OPC data modell are OPC servers, groups within this server
and items within this groups.
2.1.1 Servers
From the clients view a server is a seperate process with its own configuration
regarding accessible data recording hardware and available data items, running on the
same machine as the client or on a remote machine, which supports DCOM.
A Server manages groups by providing methods to create or remove groups on behalf
of a client. Typically it provides some cache mechanism to speed up data access, if
various groups are in use by the client.
2.1.2 Groups
A server maintains sets of groups on a per client basis. A group may be used by a
single client to organize its data according to different group specific attributes, which
are used to parametrize the clients (multiple) subscription(s) to the server:
• Update Rate – Time between data updates through subscription callbacks triggered
by the server, if any data item inside the group has changed in the meantime. If the
data is scanned from a device, this is the scan rate.
• Dead Band – The percent change of an items value, which causes a callback. This
applies only to analogous values with a high and low limit, which means floating
point or integer data.
• Time Bias – The Time Zone Bias (in minutes) of timestamps associated with the
item.
• Locale ID – The current locale ID, if supported by the server.
• Enabled – A group may be enabled or disabled, i.e. will cause data change
notification callbacks, but may be disabled, if the client is not interested in the
groups data at the moment.
• Active – A groups Active Flag may be cleared by the client to signal, that it is not
interested in a group or item at the moment. The server will not return any
information on read or refresh requests about the group until it is set to be active
again.
A group has an Update Rate attribute, because OPC is change driven: Only changed
data is sent to the client. This is realized by callback functions which are subscribed at
the server. Regardless from the number of subscriptions established, each group has a
single client to notify for data changes; a client can never access an existing server or
group created by another client.
2.1.3 Items
Process data is made available to the OPC client by means of OPC items. They
represent data sources with the attributes
Server
OPC OPC
Server Server
OPC Client OPC Client
OPC Group
OPC
Data
OPC
Change
OPC Group Shutdown
Notification OPC Notification
Item
OPC
Shutdown OPC
Notification Item
OPC
Data Change
OPC Group
Notification
OPC
Data OPC
Change Item
Notification
notify
add/create
The first step to browse and display data provided by an OPC server is to start the
OPC server and make a connection to it. This can be achieved by selecting the menu
OPC – Connect, which will show a dialog box with all OPC servers available at the
local machine (connections via DCOM are not supported):
Select the desired server and click OK to start the server. You can also connect to a
running OPC server by the same procedure. It is possible to connect to multiple server
at the same time. If there are two connections, the left Explorer pane will show them in
the following manner:
Connecting to a server implies the creation of three groups, which hold data items with
different access rights:
• A group to insert items, which are read-only (initialy named „Read Only Items“).
• A group for items, which are write-only (initialy named „Write Only Items“).
• A group for items, which can be read or written (initialy named „Read/Write Items“).
To insert items into a group for display purpose, the user can browse the available data
items of the server by expanding the tree view node of the desired server in the left
Explorer pane. The right pane shows name and item ID of all available items for the
selected server in the servers Configuration subtree as shown below.
To display the value of a data item, just select one ore more items in the right Explorer
pane and select the menu OPC-Add Item(s). The items automatically are inserted to
one of the groups mentioned above according to their server provided access rights. It
is allowded to add an item more than once. In this case the item name of the second
and follwing insertion is modified in that the client adds a counter value enclosed in
braces to the native item name, which leads for example to the following situation:
First insertion: Input001
Second insertion: Input001 (1)
The item values are displayed if one of the groups in the Groups subtree is selected:
The right pane shows name and ID of the item, the data type, quality and substatus,
value and the number of updates, which are callbacks by the server, for each item. The
following data types are accepted and displayed by the client:
Each OPC server maintains some information about it‘s internal state, this is
• The time the server was activated (started).
• The current server time.
• The time the server sent the last data value update.
• The servers internal state.
• The number of groups created by a client.
• The bandwidth value, which is a server specific generated value regarding the
Bandwitdh currently in use by the client.
• Some information about version and build of the server.
• A vendor specific info string.
To view this information, select the State subtree of the desired server.
Once a connection to an OPC server is established, the user may wish to change
some attributes (refer to section 2.1.2) of the standard groups, especially the update
rate. Therefor select a group in the left Explorer pane and select a group in the Groups
subtree. Choose OPC-Group Parameter... and the dialog box shown in Figure 2-6 will
appear:
If the dialog is closed, all changes in the edit fields of the dialog are applied to the
connected server.
Access to item attributes is restricted to modifications of the actual value and is
applicable only to items with the canonical data types VT_I1, VT_I2, VT_I4, VT_UI1,
VT_UI4, VT_R4, VT_R8 and VT_BSTR.
To modify a value, select the desired item in the Group Display Mode and choose
Tools-Modify Item... . The follwoing dialog shows name and data type of the selected
item. Below you can enter a new value, which is applied, when the dialog is closed.
Note: Data value modifications are only applicable to Read/Write or Write Only items.
2.6 Loopbacks
For test purposes the OPC client provides a simple loopback mechanism, which
ensures permanent server activities. The user chooses a source item in the server and
assigns it to a destination item. If the value of both is equal, it is incremented and
written to the destination item, which therefor must be a Read/Write or Write Only item.
If source and destination value are coupled by a hardware or software loopback on the
servers side, the OPC client will trigger an endless counter loop.
Note: Loopbacks are only applicable to Read Only or Read/Write sources and
Read/Write or Write Only destinations
Select a source item from the left list box. The right list box shows assigned destination
items. Click the desired destination here. In the example shown above, item
Output003 is selected as destination for the source Input001.
It is possible to define an item as source of multiple destinations and vice versa, but in
the last case the result is undefined.
Note: Do not assign an item (Output001) and a copy of this item (Output001 (...)) as
destination the same time, because the result is undefined.
3.1 Overview
The OPC standard is build upon Microsofts COM (Component Object Modell)
technology, a language independent, open object modell. In the COM terminology a
server is a piece of code which allows the creation and usage of objects, which are
maintained by the server and may be used by one or more clients.
To access process data, an OPC clients creates a Server Object in an OPC server,
which may be an InProc Server (a dynamic link library), an OutProc Server (a separate
executable or Windows NT service) or a Remote Server, which is an OutProc Server
on a remote machine. Client and server interact via COM Interfaces, which are function
pointer tables similar to virtual function tables in object oriented programming
languages like C++. Refer to the COM specification (URL http://www.microsoft.com/
resources/specs.asp) for more details about this topic.
Although not mandatory by the OPC specification, a common object modell for both
OPC server and client consists of classes, which represent the basic concepts OPC
Server, OPC Group and OPC Item as described in section 2.1.2, 2.1.3 and 2.1.3. That
is, clients use server objects, which provide methods to activate and connect to an
OPC server. Server objects store a COM interface pointer to the OPC server object,
encapsulate the server status, keep a list of groups created in the server and provide
suitable code for server specific notifcations, which are according to the OPC Data
Access specification 2.0 server shutdown requests.
Group objects are used to organize items with the same update rate or which are made
active or inactive at the same time. A group object holds an interface pointer to its OPC
Group object, which is required to add or delete items to this group. It may also
implement functionality to process group specific server notifications.
OPC is change driven, so although explicit item polling is possible, for performance
reasons the prefered mode of operation is to buffer item values at each data change
notification. It is neccessary to store this value for example in an item object, which is
created for each item, that is inserted into a group.
Appart from several MFC classes which are used to build the user interface, the
classes of interest are CServer, CGroup and CItem. The relation between this
classes is shown in an UML like style in figure Figure 3-1:
CServer
m_pServer : IOPCServer*
m_hUpdateTimer : HANDLE
Connect()
Disconnect()
CreateGroup()
DeleteGroup()
OnShutdownRequest()
CGroup
m_ItemList : ITEMList
m_pGroup : IUnknown
m_hServersGroupHandle : OPCHANDLE
Create()
Remove()
Refresh()
OnDataChange()
CItem
m_sItemDef : OPCITEMDEF
m_vValue : VARIANT
m_wQuality : WORD
m_hServerHandle : OPCHANDLE
m_vtCanonicalType : VARTYPE
m_ulUpdateCounter : LARGE_INTEGER
m_ulLastUpdate : LARGE_INTEGER
m_pLoopbackDest : CItem*
m_pLoopbackSource : CItem*
SetValue()
WriteSync()
Inc()
return an IUnknown interface pointer to the client, which is the main interface to
access the servers functionality.
The OPC Explorer example creates a CServer object for each server it should be
connected to. See the Connect() method of this class about how to establish the
connection. This method also queries the server object for the interface pointers
• IOPCServer, which is used for group management and dealing with error and status
information, and
• IOPCBrowseServerAdressSpace, the interface used for item browsing as
described below.
The Connect() method creates three groups for the three categories of item access
rights: A group to hold Read-Only Items, one to hold Read/Write-Items and one for
Write-Only Items.
In addition this class is used to handle server specific notifications. Therefor it is
designed as COM class, which is subscribed at the server using the connection point
mechanism and provides a callback method named ShutDownRequest(). The task
of this method is to disconnect the client, if requested by the server.
In general, to access items an OPC client has to create at least one group, where items
of interest are inserted. The OPC Explorer sample associates one CGroup object with
each group created in the OPC server.
The Create() method of a CGroup object queries the server for an IOPCItemMgt
interface, which is required for item adding and removing as well as changing item
settings. A CGroup object provides an AddItem() method to insert items. It creates
a CItem object, which is inserted into a group internal list for display purposes and
adds the item to the server group. Items are removed using the RemoveItem()
method, which calls the IOPCItemMgt interface method RemoveItems() to delete
the item at the server and removes the items entry in the internal item list.
To insert items in a servers group, a unique identifier is needed, which is called the
Item ID. To get this Item ID, the client can ask an OPC server for available data items,
which is called item browsing. According to the OPC Specification 2.0 a server can
implement this feature in two Namespace Modes:
• Flat – All items of the server are returned in a single list.
• Hierarchical – Items are presented to the client in form of a tree with branches and
leafs. Via the browse interface the client can navigate through the tree to get item
IDs or validate items. In this case the items at a particular place in the tree are
returned via a standard COM string enumerator.
The OPC client example illustrates the usage of the browse interface. It displays the
tree branches in the left explorer pane. If a branch is selected by the user, it is
expanded by querying the subtree of this node. If available, leafs at the selected node
are displayed in the right explorer pane.
Browsing is done
after an Itemexpanding notification (refer to the
OnItemExpanding() method of the CLeftView class). Some additional code is
needed their to determine, which servers browser interface is required, because the
client supports multiple server at the same time.
The OPC Data Access Specification defines four group specific notifications from the
server:
• OnDataChange, which is sent if an item within the group has changed its value,
• OnReadComplete, which signals, that an asynchronous read operation was
completed,
• OnWriteComplete, which signals the completition of an asynchronous write
operation, and
• OnCancelComplete, which signals, that an asynchronous cancel operation was
completed.
To handle this notifications, the class CGroup publishes the COM interface
IOPCDataCallback with appropriate callback methods. Since asynchronous operations
are not supported, only the OnDataChange() method provides code, which is
responsible for a item data update.
A very useful feature of the OPC specification is an item handle, provided by the client
at item insertion and returned by the server on each notification concerning item values
(this also applies to a client defined group handle). Using this handle to deposit an
object pointer representing an item allows to update the items contained in a data
change notification in a convenient way.
The class CItem is used to represent data items in a group. It stores the item
definition, which is queried using the ValidateItems() method of the group
interface IOPCItemMgt.
Additionally there are data elements to store the loopback destination, if assigned, of
the item and a loopback source, if the item is assigned as loopback destination. This
elements are used by the items group to check the loopback condition on every data
change notification.
If a data change notification is received, the server delivers the client provided handles
of all items, which have changed. The OPC explorer uses CItem pointers as client
handles, so the returned handles can be used to access the corresponding item object
and set its new data value (method SetValue()) and update the items timestamp
regarding the last update (method SetUpdateTime()).
Modifying the value of an item invokes the method WriteSync(), which performs a
synchronous write operation. The Inc() method is used by the loopback feature.