马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
- EXAMPLE
- The brief coding example below connects to the Ovation OPC Server, adds a group, adds
- items, reads values and writes values.
- 1. Start up the Visual Basic Program or the Visual Basic for Applications environment for
- an Microsoft Office Product. Add the OPC Automation Library 2.0 (opcdaauto.dll) to the
- list of references using the Tools\References menu item.
- 2. Copy the example subroutine below into a module. Execute the code from the
- debugger.
- Option Explicit
- ' This is an example of the use of Ovation OPC
- ' This simple example will have to be modified to be
- ' used by an Ovation system. The correct machine name
- ' and correct point names must be inserted in the appropriate
- ' variables
- Sub OPCExample()
- Dim myOPCServer As OPCServer
- Dim myOPCBrowser As OPCBrowser
- Dim myOPCGroup As OPCGroup
- Dim myOPCItem(3) As OPCItem
- Dim OPCItemTemp As OPCItem
- Dim myServerName As String
- Dim myNodeName As String
- Dim myGroupName As String
- Dim myItemName(3) As String
- Dim i As Integer
- Dim lCount As Long
- Dim lClientHandle As Long
- Dim myClientHandles(3) As Long
- Dim myServerHandles() As Long
- Dim myErrors() As Long
- Dim lReturn As Long
- Dim myValue As Variant
- Dim myQuality As Variant
- Dim myTimeStamp As Variant
- B.2 Automation Interface
- B-2 U3-1036 (Rev 1)
- Emerson Process Management Proprietary Class 2C
- '
- ' Modify these as needed for a specific
- ' OPC Server/group/item
- '
- myServerName = "Ovation.OPC.4"
- myNodeName = "Drop200"
- myGroupName = "Group"
- '
- ' This name must definitely be modified
- ' to match a legal name on the OvationHighway
- ' The syntax for a fully qualified Ovation Item is:
- ' Point.Unit@Network.Field
- '
- ' ex. la006-122.Unit1@w3.HL
- '
- ' Create a new server
- Set myOPCServer = New OPCServer
- ' Connect to the Ovation OPC Server
- On Error GoTo NoConnect
- Call myOPCServer.Connect(myServerName, myNodeName)
- Set myOPCBrowser = myOPCServer.CreateBrowser
- ' Add a group
- On Error GoTo NoGroup
- Set myOPCGroup = myOPCServer.OPCGroups.Add(myGroupName)
- '
- ' modify some group values
- '
- myOPCGroup.UpdateRate = 500
- myGroupName = "Group2"
- myOPCGroup.Name = myGroupName
- '
- ' add 3 OPC Items
- '
- On Error GoTo NoAddItem
- ' initialize data
- myItemName(1) = "la006-122"
- myItemName(2) = "da006-122"
- myItemName(3) = "ld006-122"
- myClientHandles(1) = 2500
- myClientHandles(2) = 2501
- myClientHandles(3) = 2502
- Call myOPCGroup.OPCItems.AddItems(3, _
- myItemName, _
- myClientHandles, _
- myServerHandles, _
- myErrors)
- ' read the data
- For Each OPCItemTemp In myOPCGroup.OPCItems
- Call OPCItemTemp.Read(0, myValue, myQuality, myTimeStamp)
- Debug.Print OPCItemTemp.ItemID & " " & myValue & " quality " &
- myQuality & " time " & myTimeStamp
- Next OPCItemTemp
- ' write the data
- On Error GoTo NoWrite
- B.2 Automation Interface
- U3-1036 (Rev 1) B-3
- Emerson Process Management Proprietary Class 2C
- Set OPCItemTemp = myOPCGroup.OPCItems.GetOPCItem(myServerHandles(1))
- OPCItemTemp.Write (OPCItemTemp.Value + 1#)
- Set OPCItemTemp = myOPCGroup.OPCItems.GetOPCItem(myServerHandles(2))
- OPCItemTemp.Write (OPCItemTemp.Value + 1#)
- Set OPCItemTemp = myOPCGroup.OPCItems.GetOPCItem(myServerHandles(3))
- OPCItemTemp.Write (Not OPCItemTemp.Value)
- For Each OPCItemTemp In myOPCGroup.OPCItems
- OPCItemTemp.Write (OPCItemTemp.Value + 1)
- Call OPCItemTemp.Read(0, myValue, myQuality, myTimeStamp)
- Debug.Print OPCItemTemp.ItemID & " " & myValue & " quality " &
- myQuality & " time " & myTimeStamp
- Next OPCItemTemp
- '
- ' Leave with the quiet confidence of a job well done
- '
- On Error GoTo RemoveItemsFault
- lCount = myOPCGroup.OPCItems.Count
- Call myOPCGroup.OPCItems.Remove(lCount, myServerHandles, myErrors)
- myOPCServer.OPCGroups.Remove (myGroupName)
- myOPCServer.Disconnect
- Exit Sub
- NoWrite:
- Debug.Print "Write Item " & " returns error; " & Err.Number & "; ("
- & Err.Description & "); """
- Resume Next
- NoAddItem:
- Debug.Print "Add Item " & myOPCItem(i).ItemID & " returns error " &
- Err.Number & " (" & Err.Description & ")"
- Resume Next
- RemoveItemsFault:
- Debug.Print "Remove Items returns error " & Err.Number & " (" &
- Err.Description & ")"
- myOPCServer.OPCGroups.Remove (myGroupName)
- myOPCServer.Disconnect
- Exit Sub
- NoGroup:
- MsgBox "Can't add group " & myGroupName
- myOPCServer.Disconnect
- Exit Sub
- NoConnect:
- ' fail to connect
- MsgBox "Connect to " & myNodeName & " Server " & myServerName
- Exit Sub
- End Sub
复制代码 |