紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 3401|回复: 0

[OPC技术] VB通过OPC与S7 200 PLC通讯的代码实例

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

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

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

x
  1. Option Explicit
  2. Option Base 1
  3. Private QOpcServer As OPCServer
  4. Private QGroups As OPCGroups
  5. Private WithEvents QGroup As OPCGroup
  6. Private QItems As OPCItems
  7. Private QItemServerHandles() As LongPrivate Sub Form_Load()
  8. On Error GoTo ErrorHandler
  9. '建立与OPC服务器的连接
  10. Set QOpcServer = New OPCServer
  11. Call QOpcServer.Connect("S7200.OPCServer") 'S7200.OPCServer是固定的OPC名称

  12. '建立一个数据群组
  13. Set QGroups = QOpcServer.OPCGroups    ' Get OPCGroups Collection Object from QOPCServer
  14. ' Set Default Properties for Group Collection
  15. ' These Properties are used to set the Properies for new Groups
  16. QGroups.DefaultGroupIsActive = 500 ' Set Default Group Update Rate to 500 ms
  17. QGroups.DefaultGroupIsActive = False ' Set Default Group Active State to Inactive
  18. Set QGroup = QGroups.Add("QGroup1") ' Add a new Group to the Group Collection
  19. ' Set Group Properties
  20. QGroup.IsSubscribed = True ' Enable Callbacks
  21. QGroup.UpdateRate = 100 '这个群组的刷新速度是100ms

  22. '往群组里添加要传送的地址单元
  23. Dim i As Long
  24. Dim ErrorFlag As Boolean
  25. Dim ItemObj As OPCItem
  26. Dim ItemIDs(2) As String
  27. Dim ItemClientHandles(2) As Long
  28. Dim Errors() As Long          ' Array for returned Item related errors
  29. ErrorFlag = False
  30. Set QItems = QGroup.OPCItems       ' 关联OPC Items
  31. ' Initialize the [IN] parameters for the Add Items call
  32. ' ItemIDs -> ItemIDs of the Items to add
  33. ' ItemClientHandles -> Client defined handles for the Items. The Server sends these handles in the Callbacks
  34. ItemIDs(1) = "2,q1.0,bool" ' Read ItemId 1 from Text Box
  35. ItemIDs(2) = "2,VW10,Word" ' Read ItemId 2 from Text Box
  36. ItemClientHandles(1) = 1
  37. ItemClientHandles(2) = 2
  38. ' [OUT] parameters are
  39. ' ItemServerHandles -> Server defined handles for the Items. The client must use these handles for all Read/Write calls
  40. ' Errors -> Item related errors
  41. ' Add Items to the Group
  42. Call QItems.AddItems(2, ItemIDs, ItemClientHandles, QItemServerHandles, Errors)
  43. ' Check Item Errors
  44. For i = 1 To 2
  45.    If Not Errors(i) = 0 Then
  46.       MsgBox "Item " + Str$(i) + " FAILED. Error Code = " + Str$(Errors(i)), vbCritical
  47.       ErrorFlag = True
  48.    End If
  49. Next
  50. ' Continue only if all Items SUCCEEDED
  51. If ErrorFlag Then
  52.    Dim RemoveErrors() As Long
  53.    Dim RemoveHandles(1) As Long
  54.    ' Remove Succeede Items
  55.    For i = 1 To 2
  56.       If Errors(i) = 0 Then
  57.             RemoveHandles(1) = QItemServerHandles(i)
  58.             Call QItems.Remove(1, RemoveHandles, RemoveErrors)
  59.       End If
  60.    Next
  61. End If
  62. If Not QGroup Is Nothing Then
  63. QGroup.IsActive = True
  64. End If
  65. Exit Sub
  66. ErrorHandler:
  67. MsgBox Err.Description + Chr(13) + "联接到OPC服务器", vbCritical, "Error"
  68. End SubPrivate Sub cmdWriteSync_Click()
  69. 'On Error GoTo ErrorHandler
  70. Dim i As Long
  71. Dim Values(2) As Variant
  72. Dim Errors() As Long          ' Array for returned Item related errors' Initialize the [IN] parameters for the SyncWrite call
  73. ' Values -> Values to write
  74. Values(1) = txtWriteVal1.Text ' Read Value 1 from Text Box
  75. Values(2) = txtWriteVal2.Text ' Read Value 2 from Text Box
  76. ' ItemServerHandles -> Server defined handles from the AddItems call

  77. ' Write Values Syncronous
  78. Call QGroup.SyncWrite(2, QItemServerHandles, Values, Errors)

  79. ' Check Item Errors
  80. For i = 1 To 2
  81.    If Not Errors(i) = 0 Then MsgBox "Item " + Str$(i) + " FAILED. Error Code = " + Str$(Errors(i)), vbCritical
  82. NextExit Sub
  83. ErrorHandler:
  84. MsgBox Err.Description + Chr(13) + "Writing Items Syncronous", vbCritical, "ERROR"
  85. End Sub' Callback from OnDataChange
  86. Private Sub QGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
  87. 'On Error GoTo ErrorHandler
  88. Dim i As Long
  89. ' Check Parameters
  90. For i = 1 To NumItems
  91.    If ClientHandles(i) > 0 And ClientHandles(i) < 3 Then
  92.       ' Values -> Values from read complete
  93.       ' Qualities -> Qualities of the values
  94.       If Qualities(i) = 192 Then
  95.             txtChangeVal.Item(ClientHandles(i) - 1).Text = ItemValues(i) ' Write Value to Text Box
  96.       End If
  97.    Else
  98.       MsgBox "DataChange Item " + Str$(i) + " has invalid Client Handle ", vbCritical
  99.    End If
  100. Next
  101. Exit Sub
  102. ErrorHandler:
  103. MsgBox Err.Description + Chr(13) + "OnDataChange", vbCritical, "ERRORCha"
  104. End SubPrivate Sub cmdExit_Click()
  105. Unload Me
  106. End Sub
  107. ' Unload Form. Event
  108. Private Sub Form_Unload(Cancel As Integer)
  109. Dim i As Long
  110. Dim Errors() As Long          ' Array for returned Item related errors
  111. ' Remove Items from the Group
  112. Call QItems.Remove(2, QItemServerHandles, Errors)
  113. ' Check Item Errors
  114. For i = 1 To 2
  115.    If Not Errors(i) = 0 Then MsgBox "Item " + Str$(i) + " FAILED. Error Code = " + Str$(Errors(i)), vbCritical
  116. Next
  117. Erase QItemServerHandles    ' Erase Item Server Handle Array

  118. QGroups.RemoveAll    ' Removes all Groups
  119. Set QGroup = Nothing ' Delete OPCGroup Object
  120. Set QGroups = Nothing ' Delete OPCGroups Collection Object

  121. QOpcServer.Disconnect       ' Disconnect from OPC Server
  122. Set QOpcServer = Nothing    ' Delete OPCServer Object
  123. End Sub
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


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

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

GMT+8, 2024-5-4 06:28 , Processed in 0.453125 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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