Ai 432 Opcda
Ai 432 Opcda
Ai 432 Opcda
4 Access to devices
4.3 OLE for Process Control (OPC) 4.3.2 Data Access Specification
2007 May, HK
field bus
field devices
Industrial Automation
OPC application
ReadItem ("OPC:Reactor1: Program2.MotorSpeed")
controller programming
Reactor_1.Program2 MW%1003 MotorSpeed Temperature .
Value: 112
MW%1004
OPC server
Get (192.162.0.2), MW%1003)
symbols
code
Network
Reactor_1
Marker: MW%1003
Oven controller
Industrial Automation
OPC DA: Objects as viewed by the OPC server An OPC server is structured as a directory with root, branches and leaves (items)
Tag Name
Level_1 Level_2 Ramp4
the hierarchical position may differ from the fully qualified ItemID this will be detailed under browsing
Branches may contain other branches and items The structure may also be flat instead of hierarchical This structure is defined during engineering of the attached devices and sensor/actors. (Intelligent servers could configure themselves by reading the attached devices)
Industrial Automation
OPC DA: Browsing - methods An OPC DA server presents an interface that allows the client to explore its structure, with the methods: MoveDown MoveUp MoveToRoot
showBranches showLeafes * GetItemID: retrieves the fully qualified item ID (see later)
Industrial Automation
OPC DA: Browsing: Fully Qualified ItemID and hierarchy A server has internally two ways to access the items: 1) the path shown when exploring the tree, and 2) the fully qualified ItemID, which is the internal path name used by the server.
Root.SimulatedItems.UserDefined.Ramp.Ramp1 UserDefined!Ramp.Ramp1
Clients usually search for an item though the hierarchical way. They position the browser on the corresponding branch and retrieve the fully qualified item ID, which is the name of the item as the server understands it.
The fully qualified name is only used at configuration time, afterwards, objects are accessed over client handles and server handles (see later)
Industrial Automation
quality:
and two optional static property: description: a text string describing the use and of the variable (optional)
engineering unit: the unit in which the variable is expressed (optional) (when writing, only the value is used)
ID
Industrial Automation
U
4.3.2 OPC DA - 9/48
Boolean, Character, Byte, (1 byte) Word, (2 bytes) Double Word, (4 bytes) Short Integer (2 bytes) Integer (4 bytes) Long Integer: Long Unsigned Integer Single Float (4 bytes) Double Float (8 bytes) Currency, Date, String, Array of "the above"
When accessing an item, the client may request that it is returned with a specific type, which could be different from the server's type. (The server's type is returned by browsing) Type conversion is left to the server, there are no rules whether and how a server does the conversion. (use with caution) Care must be taken that the data types in the programming language or in the database match those of the OPC Server. Items also may have engineering units, but this option is not often used.
Industrial Automation
OPC DA: Objects as viewed by the OPC client A client builds its own hierarchy, using the servers hierarchical view.
Industrial Automation
OPC DA: Mapping items to groups Each client structures its items by groups, independently from the server. Initially, the client browses the server structure to check if the items it is interested in exist.
GroupX
clients Client1 Client2
GroupZ
A client registers its groups and items at the server. The server keeps the structure of all its clients.
Server root Area 1 Oven_1 TAG TAG Temperature Heat_On
server
Tank_1
Area 2 Area 51
Level
Empty_Valve Fill_Valve
Industrial Automation
OPC DA: Client Handle and Server handle client 1 group1 1, 76584 2, 87689 3, 23443 group2 3, 53664 4, 43222 client 2 group1 1, 1 2, 2 3, 3
client1 The fully qualified item is not sufficient to identify an item, a client may subscribe the same item in different groups
3, 54
2, 1201 3, 1202 1,1 2, 2
client2
x 2, 2
Industrial Automation
client
myGroup.SynchWrite()
server
Call myGroup.AsyncWrite()
client
Call
server
Reply
myGroup_AsyncWriteComplete()
Reply
The OPC interface accesses only groups, not individual items. However, the "automation interface" allows to access individual items, but this does not give rise to a communication
Industrial Automation
asynchronous server
Call myGroup.AsyncRead()
client
Call
server
Reply
myGroup_AsyncReadComplete()
Reply
client
myGroup.IsSubscribed
server
Subscribe Notify
on change ("subscription-based")
Industrial Automation
server
Subscribe
Notify
myGroup_DataChange() Notify myGroup_DataChange() myGroup.Refresh() Notify myGroup_DataChange()
The server notifies the client if an item changed - in a particular group (myGroup_DataChange) or - in any of the groups (myGroups_GlobalDataChange) In the second case, only the group in which the item changed will be sent.
Industrial Automation 4.3.2 OPC DA - 17/48
OPC DA: "Cache" or "Device" ? "SynchRead" reads the data either from cache (local to the PC) or reads synchronous from the device. "Write" is always to device (DA 3.0 allows write to cache)
client application (OPC client) OPC server cache synchronous call over the field bus to the end device (takes a while)
server samples items (at the RequestedUpdateRate) and puts them into cache
FB manager
fieldbus
fieldbus connection fieldbus fieldbus proprietary protocol
device
no need for device access when fieldbus operates cyclically FB agent
device
FB agent
Industrial Automation
OPC DA: When are subscribed data transmitted ? A group has two properties to control when a data change is to be transmitted: myGroup.Refreshrate: (called UpdateRate in DA 3.0) rate at which the server samples the process values, expressed in seconds ! (1/rate) earliest interval between changes of value are communicated to the client, but minimum rate at which the cache should be updated. (throttles changes, but may miss some) The server never sends data to a client at a rate faster than the client requests. myGroup.Deadband applied only to analog values: deadband = % the range (in Engineering Units). value is transmitted if the difference since last transmission exceeds deadband.
Applies to all items of a group, DA 3.0 allows settings per group and per item. = refresh rate max range deadband min time Industrial Automation 4.3.2 OPC DA - 19/48
OPC DA: communication paradigm OPC DA works according to the shared memory paradigm. This means that a newer value overwrites the older one, no queues or history are kept.
The server does not guarantee that different clients see the same snapshot of the plant.
The server does not guarantee that all changes to variables are registered, changes may be missed if the polling period is too low.
OPC DA Client OPC DA Client
OPC DA Server
Industrial Automation
OPC DA Part 2
Part 1 explains the concepts Part 2 shows how they are implemented and programmed
Industrial Automation
An instance of an OPC Server. You must create an OPCServer object before you can get references to other objects. It contains the OPCGroups Collection and creates OPCBrowser objects. A collection containing all of the OPCGroup objects this client has created within the scope of the OPCServer that the Automation Application has connected to via OPCServer.Connect() An instance of an OPCGroup object. this object maintains state information and provides the mechanism to access data for the OPCItems Collection object that the OPCGroup object references. A collection containing all of the OPCItem objects this client has created within the scope of the OPCServer, and corresponding OPCGroup object that the Automation Application has created. An automation object that maintains the items definition, current value, status information, last update time. Note the Custom Interface does not provide a separate Item Object. An object that browses item names in the servers configuration. There exists only one instance of an OPCBrowser object per instance of an OPC Server object.
Industrial Automation
OPC DA: Program - initialising a connection find out existing OPC servers create an OPCServer object
myDummyServer.GetOPCServers myServer = new OPCServer
myServer.Connect
Set myBrowser = myServer.Browser Set myGroups = myServer.Groups Set myGroup1 = myGroups.Add Set MyItems = MyGroup1.OPCItems FQItems1[1] = "Device1.Temp1" ClientHandle1[1] = 101 ReDim ServerHandle1(nrItems) ReDim ServerErrors1(nrItems) ReDim Value1(nrItems) myGroup1.AddItems myGroup1.IsActive myGroup1.IsSubscribed
next group
Industrial Automation
As OPCAutomation.OPCGroups As OPCAutomation.OPCGroup
Integer As OPCAutomation.OPCItems 'OPC Item Collection As OPCAutomation.OPCItem 'OPC Item Object As String 'fully qualified items (see later) As Long As Long ' must be a dynamic array As Long ' must be a dynamic array
Reference: "OPC Automation 2.0" must be included into Visual Basic project
(if missing: copy opcdaauto.dll to C:\WINNT\System32\opddaauto) and register it: C:\>regsvr32 C:\WINNT\System32\opddaauto.
A simple way to do it: install Software Toolbox's TopServer (freeware)
Industrial Automation 4.3.2 OPC DA - 26/48
The GetOPCServers function applied to a dummy Server object allow to list the existing servers on this node or on another node (over DCOM - security must be set correctly). The information about which OPC servers exist is taken from the registry, where it has been put by each server at its installation time
Private Sub ShowServers(netNodeName As String) Dim dummyServer As OPCAutomation.OPCServer Dim Servers As Variant Dim cntServers As Integer Set dummyServer = New OPCAutomation. OPCServer Servers = dummyServer.GetOPCServers(netNodeName) For cntServers = LBound(Servers) To UBound(Servers) MsgBox Servers(cntServers) Next cntServers Set dummyServer = Nothing Exit Sub End Sub
' create a dummy server object ' returns all available servers ' display the names
Industrial Automation
Before connecting, it is safe to check the name of the server from the server's list. Also, it is preferable to include the connection in a separate routine since it can fail:
Function ServerGetCare(Name As String, ServerNode As String) As OPCAutomation.OPCServer On Error GoTo ServerGetCareErr Dim MyOPCServer As New OPCAutomation.OPCServer MyOPCServer.Connect ServerName, ServerNode ' connect risky Set ServerGetCare = MyOPCServer Exit Function
ServerGetCare_Err: Err.Clear MsgBox "Could not connect" Set MyServer = Nothing Exit Function
Industrial Automation
OPC DA: Program - Browsing the server The object OPCBrowser (of type "collection") acts as a pointer to the server's tree:
Dim MyServer As OPCAutomation.OPCServer Dim MyBrowser As OPCAutomation.OPCBrowser Dim vName As Variant MyServer.Connect "Matrikon.OPC.Simulation", "Orion" Set MyBrowser = MyServer.CreateBrowser MyBrowser.ShowBranches For Each vName In MyBrowser MsgBox "Branch: " & vName Next vName MyBrowser.ShowLeafs For Each vName In MyBrowser MsgBox "Leaf: " & vName Next vName 'server and node name (DCOM) ' create an OPC browser ' show the branches ' display the branch name
Industrial Automation
OPC DA: Navigating There may be leaves at every branch, since a branch may have properties
MyBrowser.MoveDown (strBranch) MyBrowser.MoveUp
Server root Site A Tank TAG Machine Room Wash Room TAG Grinder Climate Store TAG
' go down the selected branch tree ' go up the selected branch tree
server
TAG site properties
Industrial Automation
Access path
The Access Path is an optional information that the client may provide regarding how to get to the data, where several possibilities exist. Its use is highly server specific. Do not confound with hierarchical path.
The optional function GetAccessPath retrieves the access path for items that can be accessed over different ways.
Industrial Automation
To get the "fully qualified itemID", one positions the browser at the place where the leaf is attached to the branch and calls GetItemID
myOPCBrowser.MoveDown("TankArea") myOPCBrowser.MoveDown("Tank1")
e.g. FQI could be "Controller1;Tanks!WaterLevel" Of course, one can write an Item ID directly when defining a group, but it is safer to browse the server and get the FQI from there, since the delimiter depends on the server.
Industrial Automation
adds the items to collection input parameter input fully qualified ID input ClientHandles return parameter ServerHandles return parameter ServerErrors
' handle of the group (no s) ! ' now ready to send and receive ' and to generate events
OPC DA: Data structures at the client The client prepares data structures for its items and gives the server the corresponding pointers so the server can update them. Items to be written and read can be mixed in the same group. The type of the item (Boolean, Float,) is implicit, but known at the server
communicated to server by registering group
FullyQualifiedItemID
"Channel1.Device1.Temp1"
"Channel1.Device1.Speed1" "Channel1.PLC2.Door" "Channel1.PLC2.Valve3"
100
102 203 204
34543
22532 534676 787234
0
0 0 0
123.4
999.8 0 1
OK
OK OK OK
12:09.234
12:02.214 12:03.002 12:02.345
"Channel1.PLC2.CloseDoor"
..
205
58432
..
0
..
0
..
BAD
..
12:02.345
..
client
myGroup.SynchRead()
server
Call
serverHandles(1) = serverHandles(2) = source = OPCcache thisGroup.SyncRead source, nrItems, serverHandles, values, errors
Reply ServerHandle(11) ' copy from global variables ServerHandle(14) ' could also be OPCDevice
' identifies the items to be read ! ' returns be a dynamic array ' returns a dynamic array
For cntItems = LBound(serverHandles) To UBound(serverHandles) ' 1..n MsgBox CStr(cntItems) & " : " & values(cntItems) Next cntItems
Industrial Automation
client
AsyncRead()
Call
server
Reply
AsyncReadComplete()
Private Sub Mygroup_AsyncReadComplete ( ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, Asynchronous read separates Call and Reply. TimeStamps() As Date, Errors() As Long) MsgBox ("Async Read Complete") End Sub
Industrial Automation
Although the AsynchReadComplete carries the ClientHandle of each item, it does not tell which AsynchRead caused the AsynchReadComplete event to fire. Call and Reply are linked by the TransactionID: this ID is returned in AsynchReadComplete It can also be used to cancel the operation
Industrial Automation 4.3.2 OPC DA - 37/48
To UBound(ClientHandles) ' index 1..n = ItemValues(cntItems) ' display = DateAdd("h", 9, TimeStamps(cntItems)) = Qualities(cntItems)
End Sub
This function is called each time an item in the group changes The ClientHandles (here: 5 and 6) identifies the variables, not the fully qualified itemID The values are displayed in the TextValue, TextTimeStamp and TextQuality fields. The refresh rate is given in the group definition.
Industrial Automation 4.3.2 OPC DA - 38/48
Although transmission by groups is more efficient than AsyncRead, it can be improved by using Groups Events (Global Data Change)
Industrial Automation
The GlobalDataChange event is fired when any item in a group changed. (if Groups is also with events, the corresponding Group_DataChange will also be called)
Industrial Automation
This event signals to the client that the server shut down. The client must declare its server WithEvents and provide the corresponding event Subroutine This should stop all actions, otherwise exceptions will occur.
Industrial Automation
OPC DA: Do not forget cleanup ! To speed up connection/disconnection, an OPC server remembers its groups and clients when a client disconnects. To do this, an OPC server initialises its structures with a client counter of 2, instead of 1. Therefore, it is imperative to shut down explicitly the server, otherwise links will subside (and you will have kill the server to clear them).
Private Sub ServerShutdown Dim dummyServer As OPCAutomation.OPCServer Dim Servers As Variant Dim cntServers As Integer Set myGroup1 = Nothing Set myGroups = Nothing MyServer.Remove MyServer.RemoveAllGroups MyServer.Disconnect Set MyServer = Nothing
' create a dummy server object ' returns all available servers
Industrial Automation
OPC DA: Libraries The OPC DA specification is not formal, conformance can hardly be checked against this document. To ensure that the standard is observed, the OPC foundation distributes on its website the DLLs (opcdaauto.dll, opccomn_ps,) that contain the type libraries to access the OPC server.
The vendors are not compelled to implement all features. For instance, the description of the variables is seldom used. Calling unimplemented functions causes exceptions that must be caught in Visual Basic with "On Error " statements.
There exist three versions of DA, 1.0, 2.0 and 3.0, that behave differently, however, older servers do not have a property indicating which version they support.
Industrial Automation
OPC DA: Custom Interface While the Automation Interface is easy to use and quite powerful, some OPC functions are missing and special operations can only be done in Visual C++ using the custom COM interface. This is only recommended for experienced programmers.
IOPCCommon IOPCServer IConnectionPointContainer IOPCItemProperties [IOPCServerPublicGroups] IConnectionPointContainer [IOPCBrowseServerAddressSpace] IPersistFile IOPCDataCallback IOPCServerShutdown
OPC client
OPC server
IOPCItemMgt IOPCGroupStateMgt [IOPCPublicGroupSteatMgt] IOPCSyncIO IOPCAsyncIO2 IConnectionPointContainer IEnumOPCItemAttributes IOPCSyncIO IDataObject
OPC group
Industrial Automation
Can a change of an OPC variable be notified as an event, or shall the client poll ?
Why is browsing necessary, even when one knows the variable's location in the server ?
Industrial Automation
To probe further. OPC Foundation: Specifications http://www.opcfoundation.org SoftwareToolbox Examples in Visual Basic
http://www.softwaretoolbox.com/Tech_Support/TechExpertiseCenter/OPC/opc.html
Industrial Automation
Industrial Automation
IConnectionPointContainer
IOPCAsyncIO IDataObject
N/A
Required Required
Required
Optional Optional
Required
N/A N/A
Industrial Automation
IOPCBrowseServerAddressSpace
IOPCItemIO
Optional
N/A
Optional
N/A
N/A
Required
Industrial Automation