紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 3084|回复: 0

[VB/VB.NET] OPC 客户端代码,VB的

[复制链接]
冰糖 发表于 2010-12-18 13:00:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
  1. EXAMPLE
  2. The brief coding example below connects to the Ovation OPC Server, adds a group, adds
  3. items, reads values and writes values.
  4. 1. Start up the Visual Basic Program or the Visual Basic for Applications environment for
  5. an Microsoft Office Product. Add the OPC Automation Library 2.0 (opcdaauto.dll) to the
  6. list of references using the Tools\References menu item.
  7. 2. Copy the example subroutine below into a module. Execute the code from the
  8. debugger.
  9. Option Explicit
  10. ' This is an example of the use of Ovation OPC
  11. ' This simple example will have to be modified to be
  12. ' used by an Ovation system. The correct machine name
  13. ' and correct point names must be inserted in the appropriate
  14. ' variables
  15. Sub OPCExample()
  16. Dim myOPCServer As OPCServer
  17. Dim myOPCBrowser As OPCBrowser
  18. Dim myOPCGroup As OPCGroup
  19. Dim myOPCItem(3) As OPCItem
  20. Dim OPCItemTemp As OPCItem
  21. Dim myServerName As String
  22. Dim myNodeName As String
  23. Dim myGroupName As String
  24. Dim myItemName(3) As String
  25. Dim i As Integer
  26. Dim lCount As Long
  27. Dim lClientHandle As Long
  28. Dim myClientHandles(3) As Long
  29. Dim myServerHandles() As Long
  30. Dim myErrors() As Long
  31. Dim lReturn As Long
  32. Dim myValue As Variant
  33. Dim myQuality As Variant
  34. Dim myTimeStamp As Variant
  35. B.2 Automation Interface
  36. B-2 U3-1036 (Rev 1)
  37. Emerson Process Management Proprietary Class 2C
  38. '
  39. ' Modify these as needed for a specific
  40. ' OPC Server/group/item
  41. '
  42. myServerName = "Ovation.OPC.4"
  43. myNodeName = "Drop200"
  44. myGroupName = "Group"
  45. '
  46. ' This name must definitely be modified
  47. ' to match a legal name on the OvationHighway
  48. ' The syntax for a fully qualified Ovation Item is:
  49. ' Point.Unit@Network.Field
  50. '
  51. ' ex. la006-122.Unit1@w3.HL
  52. '
  53. ' Create a new server
  54. Set myOPCServer = New OPCServer
  55. ' Connect to the Ovation OPC Server
  56. On Error GoTo NoConnect
  57. Call myOPCServer.Connect(myServerName, myNodeName)
  58. Set myOPCBrowser = myOPCServer.CreateBrowser
  59. ' Add a group
  60. On Error GoTo NoGroup
  61. Set myOPCGroup = myOPCServer.OPCGroups.Add(myGroupName)
  62. '
  63. ' modify some group values
  64. '
  65. myOPCGroup.UpdateRate = 500
  66. myGroupName = "Group2"
  67. myOPCGroup.Name = myGroupName
  68. '
  69. ' add 3 OPC Items
  70. '
  71. On Error GoTo NoAddItem
  72. ' initialize data
  73. myItemName(1) = "la006-122"
  74. myItemName(2) = "da006-122"
  75. myItemName(3) = "ld006-122"
  76. myClientHandles(1) = 2500
  77. myClientHandles(2) = 2501
  78. myClientHandles(3) = 2502
  79. Call myOPCGroup.OPCItems.AddItems(3, _
  80. myItemName, _
  81. myClientHandles, _
  82. myServerHandles, _
  83. myErrors)
  84. ' read the data
  85. For Each OPCItemTemp In myOPCGroup.OPCItems
  86. Call OPCItemTemp.Read(0, myValue, myQuality, myTimeStamp)
  87. Debug.Print OPCItemTemp.ItemID & " " & myValue & " quality " &
  88. myQuality & " time " & myTimeStamp
  89. Next OPCItemTemp
  90. ' write the data
  91. On Error GoTo NoWrite
  92. B.2 Automation Interface
  93. U3-1036 (Rev 1) B-3
  94. Emerson Process Management Proprietary Class 2C
  95. Set OPCItemTemp = myOPCGroup.OPCItems.GetOPCItem(myServerHandles(1))
  96. OPCItemTemp.Write (OPCItemTemp.Value + 1#)
  97. Set OPCItemTemp = myOPCGroup.OPCItems.GetOPCItem(myServerHandles(2))
  98. OPCItemTemp.Write (OPCItemTemp.Value + 1#)
  99. Set OPCItemTemp = myOPCGroup.OPCItems.GetOPCItem(myServerHandles(3))
  100. OPCItemTemp.Write (Not OPCItemTemp.Value)
  101. For Each OPCItemTemp In myOPCGroup.OPCItems
  102. OPCItemTemp.Write (OPCItemTemp.Value + 1)
  103. Call OPCItemTemp.Read(0, myValue, myQuality, myTimeStamp)
  104. Debug.Print OPCItemTemp.ItemID & " " & myValue & " quality " &
  105. myQuality & " time " & myTimeStamp
  106. Next OPCItemTemp
  107. '
  108. ' Leave with the quiet confidence of a job well done
  109. '
  110. On Error GoTo RemoveItemsFault
  111. lCount = myOPCGroup.OPCItems.Count
  112. Call myOPCGroup.OPCItems.Remove(lCount, myServerHandles, myErrors)
  113. myOPCServer.OPCGroups.Remove (myGroupName)
  114. myOPCServer.Disconnect
  115. Exit Sub
  116. NoWrite:
  117. Debug.Print "Write Item " & " returns error; " & Err.Number & "; ("
  118. & Err.Description & "); """
  119. Resume Next
  120. NoAddItem:
  121. Debug.Print "Add Item " & myOPCItem(i).ItemID & " returns error " &
  122. Err.Number & " (" & Err.Description & ")"
  123. Resume Next
  124. RemoveItemsFault:
  125. Debug.Print "Remove Items returns error " & Err.Number & " (" &
  126. Err.Description & ")"
  127. myOPCServer.OPCGroups.Remove (myGroupName)
  128. myOPCServer.Disconnect
  129. Exit Sub
  130. NoGroup:
  131. MsgBox "Can't add group " & myGroupName
  132. myOPCServer.Disconnect
  133. Exit Sub
  134. NoConnect:
  135. ' fail to connect
  136. MsgBox "Connect to " & myNodeName & " Server " & myServerName
  137. Exit Sub
  138. End Sub
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


--------------------------------------------------------------------------------------------------------------------
本站是工控技术交流站点,论坛内容均为网络收集或会员所发表,并不代表本站立场,会员拥有该内容的所有权力及责任!
本站内容如有侵犯您的版权,请按下面方式联系本站管理员,我们将及时删除处理
管理员:冰糖 QQ:5483695(请直击主题), Mail:admin#ziwai.net(#改成@) 其它非本人.
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

QQ|Archiver|手机版|小黑屋|紫外工控论坛. ( 苏ICP备11032118号-1 )

GMT+8, 2024-5-5 04:50 , Processed in 0.421880 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表