《OPC接口技术的介绍ppt课件.ppt》由会员分享,可在线阅读,更多相关《OPC接口技术的介绍ppt课件.ppt(103页珍藏版)》请在三一办公上搜索。
1、OPC接口技术,谈英姿东南大学自控系工业自动化教研室,内容提要,概述OPC基本原理OPC DA 接口标准OPC AE 接口标准OPC HDA 接口标准OPC XML接口标准,OPC基本原理,通用OPC结构及组成对象及接口概述必需的接口定义可选的接口定义服务器地址空间及配置时间同步与顺序发送机制EnumOPC对象属性条目,通用OPC结构及组成,OPC数据访问规范OPC报警和事件规范OPC数据交换规范OPC历史数据访问规范OPC批处理规范OPC安全性规范OPC XML规范,对象及接口概述,OPC对象OPC对象有三个:服务器(OPC Server)、组(OPC Group)和项(OPC Item)O
2、PC接口,服务器,OPC Server即OPC启动服务器,通过它获得其他对象和服务的起始类,并用于返回OPC Group类对象。OPC Server级别有多种属性,其中包含一个OPC服务器对象的状态和版本等信息。这种级别中的对象由客户应用创建。IOPCServer接口包含管理OPC Group级别中的对象的方法。如将组加入服务器或从服务器中删除组的方法(AddGroup,RemoveGroup)。IOPCBrowseServerAddressSpace接口包含查找服务器地址空间的方法。IOPCCommon接口方法用于通知服务器语言的设置和客户机的名称。,标准OPC服务器对象,必需的接口定义,O
3、PC服务器必需的接口有IunknownIOPCServer IOPCCommerIConnectionPointContainer,组(OPC Group),OPC Group存储由若干OPC Item组成的Group信息,并用于返回OPC Item类对象。OPC Group级别管理被称为OPC Item的各个过程变量。IOPCItemMgt接口提供将项加入组或从组中删除项的方法(“AddItem”,“RemoveItem”)。IOPCGroupStateMgt接口的方法用于处理组专用的参数或复制组。,标准OPC组对象,项(OPC Item),OPC Item存储具体Item 的定义、数据值、
4、状态值等信息。OPC Item级别的一个对象代表与一个过程变量的连接。该对象的唯一接口是OPCItemDisp。关于OPC Item的信息可以在属性表中找到,例如数值(“Value”)属性或存取路径(“AccessPath”)属性。,OPC项对象,OPC 的过程数据构造,OPC项代表了与服务器中的数据的连接。客户程序对OPC项的操作都是通过包容此项的OPC组来进行的,而不是直接把OPC项作为一个对象来操作。每个OPC项都有值(Value)。品质(Quality)和时间戳(Time Stamp)三个属性。,EnumOPC对象属性条目,OPC客户与OPC服务器,自定义接口与自动化接口,OPC Au
5、tomationwrapper,Automation client(Visual Basic, Excel,),custom interface,server,Custom client(C+,.),custom interface,server,自定义接口与自动化接口,自定义接口效率高,通过该接口,客户能够发挥OPC服务器的最佳性能,采用C语言的客户一般采用定制接口方案;自动化接口使解释性语言和宏语言访问OPC服务器成为可能,采用VB等语言的客户一般采用自动化接口,OPC DA 接口标准,数据访问方式DA功能实现过程DA的对象与接口,数据访问方式,DA 的同步读取DA的异步读取DA的数据更新
6、DA的数据订阅DA的同步写入DA的异步写入,DA 的同步读取,DA的异步读取,DA的数据订阅,数据订阅传输模式,数据源:cache/device,client application(OPC client),fieldbus,OPC server,cache,fieldbus,device,fieldbus,device,fieldbus connection,server samples items(at the RequestedUpdateRate)and puts them into cache,synchronous call over the field bus to the en
7、d device (takes a while),no need for “device access” when fieldbus operates cyclically,FB manager,FB agent,FB agent,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),DA功能实现过程,OPC DA客户连接到DA服务器,建立O
8、PC Group和OPC Item;通过Group 和Item进行数据访问;处理完毕,关闭连接。,OPC DA对象的层次模型,OPCItem,OPCBrowser,OPCServer,OPCGroups (collection),OPCGroup,OPCItems (collection),OPCItem,OPCItem,OPCGroup,OPC DA 初始化连接,find out existing OPC servers,connect to that OPC server,create an OPCBrowser objectcreate an OPCGroups object,creat
9、e an OPCServer object,create an OPCGroup object,build array of itemschecking with the browser that these items exist in this server,add item array to the group,activate and subscribe group,nextgroup,初始化,myDummyServer.GetOPCServers,myServer.Connect,Set myBrowser = myServer.BrowserSet myGroups = mySer
10、ver.Groups,myServer = new OPCServer,Set myGroup1 = myGroups.AddSet MyItems = MyGroup1.OPCItems,FQItems11 = Device1.Temp1ClientHandle11 = 101ReDim ServerHandle1(nrItems)ReDim ServerErrors1(nrItems)ReDim Value1(nrItems),myGroup1.AddItems,myGroup1.IsActivemyGroup1.IsSubscribed,Declarations,Option Base
11、1 OPC arrays indices start with 1Dim WithEvents MyServer As OPCServer OPC Server Object (Events optional)Dim WithEvents MyGroups As OPCGroups OPC Group Collection (Events opt.)Dim WithEvents MyGroup As OPCGroup OPC Group Object itemsDim nrItems As IntegerDim MyItems As OPCItems OPC Item Collection O
12、bjectDim MyItem As OPCItem OPC Item ObjectDim ItemsID(2) As String fully qualified items (see later) Dim ClientHandles(2) As Long Dim ServerHandles() As Long must be a dynamic arrayDim ServerErrors() As Long must be a dynamic array,Reference: OPC Automation 2.0 must be included into Visual Basic or
13、C#(if missing: copy opcdaauto.dll to C:WINNTSystem32opddaauto)and register it: C:regsvr32 C:WINNTSystem32opddaauto.A simple way to do it: install Software Toolboxs TopServer (freeware),查找OPC服务器,The GetOPCServers function applied to a dummy Server object allow to list the existing servers on this nod
14、e 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 OPCServer Dim Servers As Varian
15、t this is an array of strings Dim cntServers As Integer Set dummyServer = New OPCServer create a dummy server object Servers = dummyServer.GetOPCServers(netNodeName) returns all available servers For cntServers = LBound(Servers) To UBound(Servers) display the names MsgBox Servers(cntServers) Next cn
16、tServers Set Getserver = Nothing delete this object (was created by New) Exit Sub,连接到OPC服务器,Set MyServer = New OPCServer create server objectMyServer.Connect (Matrikon.OPC.Simulation) connect to Matrikon server,Before connecting, it is safe to check the name of the server from the servers list.Also,
17、 it is preferable to include the connection in a separate routine since it can fail:,Function ServerGetCare(Name As String, ServerNode As String) As OPCServer On Error GoTo ServerGetCareErr Dim MyOPCServer As New OPCServer MyOPCServer.Connect ServerName, ServerNode connect risky Set ServerGetCare =
18、MyOPCServer Exit FunctionServerGetCare_Err: error handler if connect fails Err.Clear MsgBox Could not connect Set MyServer = Nothing Exit Function,浏览服务器,The object OPCBrowser (of type collection) acts as a pointer to the servers tree:,Dim MyServer As OPCServerDim MyBrowser As OPCBrowserDim vName As
19、VariantMyServer.Connect Matrikon.OPC.Simulation, Orion server and node name (DCOM)Set MyBrowser = MyServer.CreateBrowser create an OPC browserMyBrowser.ShowBranches show the branchesFor Each vName In MyBrowser MsgBox Branch: & vName display the branch nameNext vNameMyBrowser.ShowLeafs explore the le
20、avesFor Each vName In MyBrowser MsgBox Leaf: & vName display the leavess nameNext vName,浏览,MyBrowser.MoveDown (strBranch) go down the selected branch treeMyBrowser.MoveUp go up the selected branch tree,site properties,Server root,Level,Temperature,Valve_On,Site A,Store,Machine Room,Grinder,Wash Room
21、,Climate,server,Tank,There may be leaves at every branch, since a branch may have properties,获得 ItemID,myOPCBrowser.MoveDown(TankArea)myOPCBrowser.MoveDown(Tank1)FQI = myOPCBrowser.GetItemId (Level),Of course, one can write an Item ID directly when defining a group, but it is safer to browse the ser
22、ver and get the FQI from there, since the delimiter depends on the server.,To get the fully qualified itemID, one positions the browser at the place where the leaf is attached to the branch and calls GetItemID,e.g. FQI could be Controller1;Tanks!Level,创建 OPCGroups and OPCItems,Set MyGroups = MyServe
23、r.OPCGroups create groups collectionSet MyGroup1 = MyGroups.Add(GRP1) add group, name privateSet MyItems = MyGroup1.OPCItems define the OPCItems of groupFQItemIDs(1) = Area2.Tank1.Level fully qualified itemIDClientHandles(1) = 5 arbitraryFQItemIDs(2) = Area2.Tank1.Temperature fully qualified itemIDC
24、lientHandles(2) = 6 arbitrary (but different)nrItems = 2MyItems.AddItems _ adds the items tocollection nrItems, _ input parameter FQItemIDs, _ supply fully qualified ID ClientHandles, _ supply ClientHandles ServerHandles, _ return parameterServerHandles ServerErrors return parameter ServerErrorsMyGr
25、oup1.ClientHandle = 1 handle of the group (no s) !MyGroup1.IsActive = True now ready to send and receiveMyGroup1.IsSubscribed = True and to generate events,The role of the ServerHandles and ClientHandles will be explained later,客户端 的数据结构,FullyQualifiedItemID,ClientHandle,ServerHandle,Channel1.Device
26、1.Temp1,communicated to server by registering group,ServerError,Value,Quality,TimeStamp,100,34543,0,123.4,OK,12:09.234,Channel1.Device1.Speed1,102,22532,0,999.8,OK,12:02.214,Channel1.PLC2.Door,203,534676,0,0,OK,12:03.002,Channel1.PLC2.Valve3,204,787234,0,1,OK,12:02.345,Channel1.PLC2.CloseDoor,205,58
27、432,0,0,BAD,12:02.345,returned by serverwhen registering,dynamic changes(refreshed on change),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 i
28、tem (Boolean, Float,) is implicit, but known at the server,.,.,.,.,.,.,Dim thisGroup As OPCGroup Dim cntItems As Integer Dim source As Integer Dim serverHandles(2) As Long Dim values() As Variant Dim errors() As Long serverHandles(1) = ServerHandle(11) copy from global variables serverHandles(2) = S
29、erverHandle(14) source = OPCcache could also be OPCDevice thisGroup.SyncRead source, nrItems, serverHandles, identifies the items to be read ! values, returns be a dynamic array errors returns a dynamic array For cntItems = LBound(serverHandles) To UBound(serverHandles) 1.n MsgBox CStr(cntItems) & :
30、 & values(cntItems) Next cntItems,myGroup.SynchRead(),client,Call,Reply,server,Group的同步读,single Items的异步读,AsyncRead(),AsyncReadComplete(),server,Dim WithEvents MyGroup.MyGroup.AsyncRead nrItems, ServerHandles, ServerErrors, TransactionID, CancelID,Call,Reply,client,Private Sub Mygroup_AsyncReadCompl
31、ete ( ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date, Errors() As Long) MsgBox (Async Read Complete)End Sub,Asynchronous read separates Call and Reply.Call supplies the ServerHandlesReply returns the co
32、rresponding ClientHandles,AsyncRead (source= device),AsyncReadComplete(),server,Call,Reply,client,AsyncRead (source= cache),AsyncRead (source = device),Although the AsynchReadComplete carries the ClientHandle of each item, it does not tell which AsynchRead caused the AsynchReadComplete event to fire
33、.,Call and Reply are linked by the TransactionID: this ID is returned in AsynchReadComplete。It can also be used to cancel the operation,AsyncCancel (),AsyncCancelComplete (),Transaction ID,Reading (by events) the OPC group,Dim WithEvents MyGroup.Private Sub MyGroup_DataChange( _ ByVal TransactionID
34、As Long, _ ByVal NrItems As Long, _ ClientHandles() As Long, _ returned by the server to the client ItemValues() As Variant, _ Qualities() As Long, _ TimeStamps() As Date) Dim cntItems As Integer For cntItems = LBound(ClientHandles) To UBound(ClientHandles) index 1.n TextValue(cntItems - 1).Text = I
35、temValues(cntItems) display TextTimeStamp(cntItems - 1).Text = DateAdd(h, 9, TimeStamps(cntItems) TextQuality(cntItems - 1).Text = Qualities(cntItems) Next cntItemsEnd Sub,This function is called each time an item in the group changesThe ClientHandles (here: 5 and 6) identifies the variables, not th
36、e “fully qualified itemID”The values are displayed in the TextValue, TextTimeStamp and TextQuality fields. The refresh rate is given in the group definition.,Groups Events,Although transmission by groups is more efficient than AsyncRead, it can be improvedby using Groups Events (Global Data Change)T
37、his event is fired whenever a variable of a group changes.If the group is subscribed also to a Group Event (DataChange), I.e. if the group isdeclared WithEvents, then both Events will be fired. The application must sort out the groups and the items.,GlobalDataChange,Dim WithEvents MyGroups As OPCGro
38、ups.Private Sub MyGroups_GlobalDataChange( ByVal TransactionID As Long, =0 if called by Refresh ByVal GroupHandle As Long, ByVal NumItems As Long, ClientHandles() As Long, identifies the items ItemValues() As Variant, value of the items Qualities() As Long, value of the items TimeStamps() As Date) t
39、imestamps of the itemsSelect Case GroupHandle depending on the group . Case 1 treat group 1 Case 2 treat group 2,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),OPC DA: Server Events,Dim Wit
40、hEvents MyServer As OPCServer define the event. .Private Sub Myserver_ServerShutDown(ByVal Reason As String)MsgBox my OPC Server & MyServer.ServerName & quitEnd Sub,This event signals to the client that the server shut down.The client must declare its server WithEvents“ and provide the corresponding
41、 event SubroutineThis should stop all actions, otherwise exceptions will occur.,OPC 的断开,Private Sub ServerShutdown Dim dummyServer As OPCServer Dim Servers As Variant this is an array of strings Dim cntServers As Integer Set myGroup1 = Nothing create a dummy server object Set myGroups = Nothing retu
42、rns all available servers MyServer.Remove MyServer.RemoveAllGroups MyServer.Disconnect delete this object (was created by New) Set MyServer = Nothing,To speed up connection/disconnection, an OPC server remembers its groups and clients when a client disconnects. To do this, an OPC server initialises
43、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).,OPC DA的类库,The OPC DA specification is not formal, conformance can hardly be checked against this docu
44、ment. 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 seld
45、om 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.,OPC AE 接口标准,AE 相关
46、 概念 AE的组态AE的目的AE 通信模式事件的种类报警的条件报警确认,AE 相关 概念,An event is a general change of state that is relevant to the OPC server.An event signal a change: 1) in the field device (production started)2) in the OPC server (alarm acknowledged)3) in the application (operator action)An alarm is a state of the proces
47、s that requires attention and is relevant to the OPC server. An alarm is represented by an alarm condition, (or short: condition), a state machineindicating if the alarm has been enabled, triggered or acknowledged.,AE相关概念,An event or an alarm does not transmit analogue process values, but they trans
48、mit information about their origin, the time of their occurrence and a messageintended for a human operator.Alarms and events may not get lost (contrarily to OPC DA, which does not guarantee completeness)Alarms and event are precisely time-stamped by their source, (contrarily to process variables, w
49、hich are time-stamped by the receiving OPC server).,controller,OPC AE Cliente.g. event logger,OPC AE Server,e.g. Ethernet,controllers,field devices,OPC AE Cliente.g. alarm printer,OPC A&E specified interface,plant measurement points,event notification,An OPC AE Serveris configured using the informat
50、ion coming from the development tools for the controllers,events,Events defined in the controllers are mirrored to the OPC AE server,controller,field bus,AE的组态,AE的目的,The controllers (PLC) generate events in response to changes in the plant variables together with their precise time of occurrence, ty