当前位置:   article > 正文

工业通讯协议 举例 EXCEL 脚本读取 OPC DA 制作生产数据报表_opc通讯推送excle数据

opc通讯推送excle数据

 

 

还是大学时 做的东东 。。。好久远的感觉 excel 读取wincc OPC DA server

废话不多说 上图 上代码

 

excel 中加入脚本代码

  1. Option Explicit
  2. Option Base 1
  3. Dim WithEvents MyOPCServer As OpcServer
  4. Dim WithEvents MyOPCGroup As OPCGroup
  5. Dim MyOPCGroupColl As OPCGroups
  6. Dim MyOPCItemColl As OPCItems
  7. Dim MyOPCItems As OPCItems
  8. Dim MyOPCItem As OPCItem
  9. Dim plcVal() As Variant
  10. Dim ClientHandles(2) As Long
  11. Dim ServerHandles() As Long
  12. Dim Values(2) As Variant
  13. Dim Errors() As Long
  14. Dim ItemIDs(2) As String
  15. Dim GroupName As String
  16. Dim NodeName As String
  17. Dim ServerName As String
  18. '---------------------------------------------------------------------
  19. ' Sub StartClient()
  20. ' Purpose: Connect to OPC_server, create group and add item
  21. '---------------------------------------------------------------------
  22. Sub StartClient()
  23. On Error GoTo ErrorHandler
  24. '----------- We freely can choose a ClientHandle and GroupName
  25. ClientHandles(1) = 1
  26. ClientHandles(2) = 2
  27. GroupName = "MyGroup"
  28. '----------- Get the ItemID from cell "A1"
  29. NodeName = Range("A1").Value
  30. ServerName = "OPCServer.WinCC" 'Range("B1").Value
  31. ItemIDs(1) = Range("A3").Value
  32. ItemIDs(2) = Range("A4").Value '增加tag2
  33. '----------- Get an instance of the OPC server
  34. Set MyOPCServer = New OpcServer
  35. MyOPCServer.Connect ServerName, NodeName
  36. Set MyOPCGroupColl = MyOPCServer.OPCGroups
  37. '----------- Set the default active state for adding groups
  38. MyOPCGroupColl.DefaultGroupIsActive = True
  39. '----------- Add our group to the Collection
  40. Set MyOPCGroup = MyOPCGroupColl.Add(GroupName)
  41. Set MyOPCItemColl = MyOPCGroup.OPCItems
  42. '----------- Add one item, ServerHandles are returned
  43. MyOPCItemColl.AddItems 2, ItemIDs, ClientHandles, ServerHandles, Errors
  44. '----------- A group that is subscribed receives asynchronous notifications
  45. MyOPCGroup.IsSubscribed = True
  46. Exit Sub
  47. ErrorHandler:
  48. MsgBox "Error: " & Err.Description, vbCritical, "ERROR"
  49. Err.Clear
  50. End Sub
  51. '---------------------------------------------------------------------
  52. ' Sub StopClient()
  53. ' Purpose: Release the objects and disconnect from the server
  54. '---------------------------------------------------------------------
  55. Sub StopClient()
  56. '----------- Release the Group and Server objects
  57. On Error Resume Next
  58. MyOPCGroupColl.RemoveAll
  59. '----------- Disconnect from the server and clean up
  60. MyOPCServer.Disconnect
  61. Set MyOPCItemColl = Nothing
  62. Set MyOPCGroup = Nothing
  63. Set MyOPCGroupColl = Nothing
  64. Set MyOPCServer = Nothing
  65. End Sub
  66. Private Sub CommandButton1_Click()
  67. End Sub
  68. Private Sub CommandButton2_Click()
  69. End Sub
  70. '---------------------------------------------------------------------
  71. ' Sub MyOPCGroup_DataChange()
  72. ' Purpose: This event is fired when a value, quality or timestamp in our Group has changed
  73. '---------------------------------------------------------------------
  74. '----------- If OPC-DA Automation 2.1 is installed, use:
  75. Private Sub MyOPCGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
  76. '----------- Set the spreadsheet cell values to the values read
  77. If NumItems = 1 Then
  78. Select Case ClientHandles(1)
  79. Case 1
  80. Range("B3").Value = CStr(ItemValues(1))
  81. Range("C3").Value = Hex(Qualities(1))
  82. Range("D3").Value = CStr(TimeStamps(1))
  83. Case 2
  84. Range("B4").Value = CStr(ItemValues(1))
  85. Range("C4").Value = Hex(Qualities(1))
  86. Range("D4").Value = CStr(TimeStamps(1))
  87. Case Else
  88. End Select
  89. Else
  90. Range("B3").Value = CStr(ItemValues(1))
  91. Range("C3").Value = Hex(Qualities(1))
  92. Range("D3").Value = CStr(TimeStamps(1))
  93. Range("B4").Value = CStr(ItemValues(2))
  94. Range("C4").Value = Hex(Qualities(2))
  95. Range("D4").Value = CStr(TimeStamps(2))
  96. End If
  97. End Sub
  98. Private Sub MyOPCServer_ServerShutDown(ByVal Reason As String)
  99. End Sub
  100. Private Sub StartOPC_Click()
  101. StartClient
  102. End Sub
  103. Private Sub StopOPC_Click()
  104. StopClient
  105. End Sub
  106. '---------------------------------------------------------------------
  107. ' Sub worksheet_change()
  108. ' Purpose: This event is fired when our worksheet changes, so we can write a new value
  109. '---------------------------------------------------------------------
  110. Private Sub worksheet_change(ByVal Selection As Range)
  111. '----------- Only if cell "B3" changes, write this value
  112. 'If Selection <> Range("B2") Then Exit Sub
  113. 'Values(1) = Selection.Cells.Value
  114. '----------- Write the new value in synchronous mode
  115. Values(1) = Range("B3")
  116. Values(2) = Range("B4")
  117. MyOPCGroup.SyncWrite 2, ServerHandles, Values, Errors
  118. End Sub
  119. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  120. Values(1) = Target
  121. End Sub

这样就可以把OPC DA server 中的数据读取出来 并按照自己格式 制作生产数据报表了

excel 文件 qq:553016857

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/705777
推荐阅读
相关标签
  

闽ICP备14008679号