资源描述
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,#,内容提要,概述,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),OPC,接口,服务器,OPC Server,即,OPC,启动服务器,通过它获得其他对象和服务的起始类,并用于返回,OPC Group,类对象。,OPC Server,级别有多种属性,其中包含一个,OPC,服务器对象的状态和版本等信息。这种级别中的对象由客户应用创建。,IOPCServer,接口包含管理,OPC Group,级别中的对象的方法。如将组加入服务器或从服务器中删除组的方法(,AddGroup,,,RemoveGroup,)。,IOPCBrowseServerAddressSpace,接口包含查找服务器地址空间的方法。,IOPCCommon,接口方法用于通知服务器语言的设置和客户机的名称。,标准,OPC,服务器对象,必需的接口定义,OPC,服务器必需的接口有,Iunknown,IOPCServer,IOPCCommer,IConnectionPointContainer,组,(OPC Group),OPC Group,存储由若干,OPC Item,组成的,Group,信息,并用于返回,OPC Item,类对象。,OPC Group,级别管理被称为,OPC Item,的各个过程变量。,IOPCItemMgt,接口提供将项加入组或从组中删除项的方法(“,AddItem,”,,“,RemoveItem,”,)。,IOPCGroupStateMgt,接口的方法用于处理组专用的参数或复制组。,标准,OPC,组对象,项,(OPC Item),OPC Item,存储具体,Item,的定义、数据值、状态值等信息。,OPC Item,级别的一个对象代表与一个过程变量的连接。该对象的唯一接口是,OPCItemDisp,。关于,OPC Item,的信息可以在属性表中找到,例如数值(“,Value,”,)属性或存取路径(“,AccessPath,”,)属性。,OPC,项对象,OPC,的过程数据构造,OPC,项代表了与服务器中的数据的连接。客户程序对,OPC,项的操作都是通过包容此项的,OPC,组来进行的,而不是直接把,OPC,项作为一个对象来操作。每个,OPC,项都有值,(Value),。品质,(Quality),和时间戳,(Time Stamp),三个属性。,EnumOPC,对象属性条目,OPC,客户与,OPC,服务器,自定义接口与自动化接口,OPC Automation,wrapper,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,的数据更新,DA,的数据订阅,DA,的同步写入,DA,的异步写入,DA,的同步读取,myGroup.SynchRead(),client,Call,Reply,server,synchronous,DA,的异步读取,myGroup.AsyncRead(),myGroup_AsyncReadComplete(),server,Call,Reply,client,DA,的数据订阅,server,Subscribe,Notify,myGroup.IsSubscribed,myGroup_DataChange(),myGroup_DataChange(),client,Notify,数据订阅传输模式,server,Subscribe,Notify,myGroup.IsSubscribed,myGroup_DataChange(),myGroup_DataChange(),client,Notify,myGroup.Refresh(),myGroup_DataChange(),Notify,数据源,: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 end 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,服务器,建立,OPC Group,和,OPC Item,;,通过,Group,和,Item,进行数据访问;,处理完毕,关闭连接。,OPC DA,对象的层次模型,OPCItem,OPCBrowser,OPCServer,OPCGroups(collection),OPCGroup,OPCItems(collection),Description,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.,OPCItem,OPCItem,OPCGroup,OPC DA,初始化连接,find out existing OPC servers,connect to that OPC server,create an OPCBrowser object,create an OPCGroups object,create an OPCServer object,create an OPCGroup object,build array of items,checking with the browser that these items exist in this server,add item array to the group,activate and subscribe group,next,group,初始化,myDummyServer.GetOPCServers,myServer.Connect,Set myBrowser=myServer.Browser,Set myGroups=myServer.Groups,myServer=new OPCServer,Set myGroup1=myGroups.AddSet MyItems=MyGroup1.OPCItems,FQItems11=Device1.Temp1,ClientHandle11=101,ReDim ServerHandle1(nrItems),ReDim ServerErrors1(nrItems),ReDim Value1(nrItems),myGroup1.AddItems,myGroup1.IsActive,myGroup1.IsSubscribed,Declarations,Option Base 1,OPC arrays indices start with 1,Dim,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,items,Dim nrItems,As Integer,Dim MyItems As OPCItems,OPC Item Collection Object,Dim MyItem As OPCItem,OPC Item Object,Dim ItemsID(2)As String,fully qualified items(see later),Dim ClientHandles(2)As Long,Dim ServerHandles()As Long,must be a dynamic array,Dim ServerErrors()As Long,must be a dynamic array,Reference:OPC Automation 2.0 must be included into Visual Basic or 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 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,dummyS,erver As OPCServer,Dim Servers As Variant,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,cntServers,Set Getserver=Nothing,delete,this,object(,was created,by,N,ew,),Exit Sub,连接到,OPC,服务器,Set MyServer=New OPCServer,create server object,MyServer.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,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=MyOPCServer,Exit Function,ServerGetCare_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 OPCServer,Dim MyBrowser As OPCBrowser,Dim vName As Variant,MyServer.Connect,Matrikon.OPC.Simulation,Orion,server,and node name,(DCOM),Set MyBrowser=MyServer.CreateBrowser,create an OPC browser,MyBrowser.ShowBranches,show the branches,For Each vName In MyBrowser,MsgBox Branch:&,vName,display the branch name,Next vName,MyBrowser.Show,Leafs,explore the,leaves,For Each vName In MyBrowser,MsgBox Leaf:&,vName,display the leavess name,Next vName,浏览,MyBrowser.MoveDown(strBranch)go down the selected branch tree,MyBrowser.MoveUp go up the selected branch tree,site properties,TAG,Server root,Level,Temperature,Valve_On,TAG,TAG,TAG,Site A,Store,Machine Room,Grinder,Wash Room,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 server 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=MyServer.OPCGroups create groups collection,Set MyGroup1=MyGroups.Add(GRP1)add group,name private,Set MyItems=MyGroup1.OPCItems define the OPCItems of group,FQItemIDs(1)=Area2.Tank1.Level fully qualified itemID,ClientHandles(1)=5 arbitrary,FQItemIDs(2)=Area2.Tank1.Temperature fully qualified itemID,ClientHandles(2)=6 arbitrary(but different),nrItems=2,MyItems.AddItems _ adds the items tocollection nrItems,_ input parameter,FQItemIDs,_ supply fully qualified ID,ClientHandles,_ supply ClientHandles,ServerHandles,_ return parameterServerHandles,ServerErrors return parameter ServerErrors,MyGroup1.ClientHandle=1 handle of the group(no s)!,MyGroup1.IsActive=True now ready to send and receive,MyGroup1.IsSubscribed=True and to generate events,The role of the ServerHandles and ClientHandles will be explained later,客户端 的数据结构,Fully,QualifiedItem,ID,ClientHandle,ServerHandle,Channel1.Device1.Temp1,communicated to server by registering group,ServerError,Value,Quality,TimeStamp,100,34543,0,123.4,OK,12:09.234,Channel1.Device1.,Speed,1,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,58432,0,0,BAD,12:02.345,returned by server,when 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 item(Boolean,Float,)is implicit,but known at the server,.,.,.,.,.,.,Dim thisGroup As OPCGroup,Dim cntItems As Integer,Dim source As Integer,Dim,s,erverHandles(2)As Long,Dim,v,alues()As Variant,Dim,e,rrors()As Long,s,erverHandles(1)=ServerHandle(11),copy,from global variables,s,erverHandles(2)=ServerHandle(14),source=OPCcache,could also be OPCDevice,thisGroup.SyncRead source,nrItems,s,erverHandles,identifies the items to be read!,v,alues,returns be a dynamic array,e,rrors,returns a dynamic array,For cntItems=LBound(,s,erverHandles)To UBound(,s,erverHandles)1.n,MsgBox CStr(cntItems)&:&,v,alues(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_AsyncReadComplete(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 ServerHandles,Reply returns the corresponding 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.,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 As Long,_,ByVal N,r,Items 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 =ItemValues(cntItems)display,TextTimeStamp(cntItems-1).Text=DateAdd(h,9,TimeStamps(cntItems),TextQuality(cntItems-1).Text =Qualities(cntItems),Next 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.,Groups Events,Although transmission by groups is more efficient than AsyncRead,it can be improved,by using Groups Events(Global Data Change),This 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 is,declared WithEvents,then both Events will be fired.,The application must sort out the groups and the items.,GlobalDataChange,Dim WithEvents MyGroups As OPCGroups,.,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)timestamps of the items,Select 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 WithEvents MyServer As OPCServer,define the event,.,Private Sub Myserver_ServerShutDown(ByVal Reason As String),MsgBox my OPC Server&MyServer.ServerName&quit,End Sub,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.,OPC,的断开,Private Sub ServerShutdown,Dim,dummyS,erver 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,returns all available servers,MyServer.Remove,MyServer.RemoveAllGroups,MyServer.Disconnect,delete,this,object(,was created,by,N,ew,),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 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 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 indic
展开阅读全文