发表于:2003-11-21 10:34:00
9楼
提供一个类似的例子,VB编程应差不多, 不过抱歉1.)是英文的2.)是Rockwell RSLinx OPC -VB的例子
Within the VB Development enviroment add a reference to the RSLinx OPC automation DLL. For
RsLinx Versions lower than 2.30 it is called RsLinxOPCAuto.DLL otherwise it is called RsiOPCAuto.DLL. This DLL exposes the RSLinx OPC Server automation interfaces to the client application .
Within your program or class module declare the following objects :
[Dim or Private] WithEvents oServer As OPCServer
[Dim or Private] WithEvents oGroup As OPCGroup
[Dim or Private] oItems As OPCItems
** The WithEvents keyword is used if you want to utilize the Event sink attributes of the OPCServer and OPCGroup Objects.
The following code illustrates how to connect to the OPC server and create an OPC group:
set oServer = new OPCServer
oServer.Connect "RSLinx OPC Server"
Set oGroup = oServer.OPCGroups.Add(mvaropcGroup)
oGroup.OPCItems.DefaultAccessPath = mDAccessPath
Set oItems = oGroup.OPCItems
oGroup.IsActive = False
oGroup.IsSubscribed = True
oGroup.UpdateRate = 250
** The Default access path is a string that contains the name of a valid DDE/OPC topic that has been created in RSLinx and points to appropriate controller (Softlogix or ControlLogix). This access path applies to all
items within a specific group. To specify individual paths per item use the following syntax: [Path]Tag Name for each item.
** to see the Interfaces/objects exposed by the RSLinx OPC Automation DLL use the VB Object browser.
Next you need to add OPC items to the created OPCGroup.
These items represent PLC tags and should follow the Allen-Bradley naming conventions eg. PLC-5 N7:0 or N7:0,L10 (specifies an array of ten contigious elements). This is accomplished by utilizing the "oGroup.OpcItems.AddItems" function. It is important to dimension the associated input arrays with a base of 1. This is a requirement imposed in the OPC automation specification. otherwise you will get errors. When you have successfully added the appropriate items you need to set the "oGroup.IsActive" property to "True" such that RSLinx will start
reading the desired Tags.
To read/write specific Tags you can utilize the following functions:
1) oGroup.AsyncRead or oGroup.AsyncWrite (asynchronous access)
2) oGroup.SyncRead or oGroup.SyncWrite
3) oItem.Read or oItem.Write (Synchronous only)
4) If you only need the values when they change add code to the oGroup_DataChange event (the "oGroup.IsSubscribed" property must be set to True)