opc读多个wincc变量到excel的代码,请高手们帮我看下

已锁定

Ray_world

  • 帖子

    45
  • 精华

    1
  • 被关注

    20

论坛等级:游侠

注册时间:2012-08-20

普通 普通 如何晋级?

opc读多个wincc变量到excel的代码,请高手们帮我看下

998

2

2014-02-25 19:57:40

excel和wincc7.0数据交换,用opc可以在实例的代码通上,问题是实例上只有两个变量(资料编号A0554),我想要在这段代码上去多添加几个wincc变量,请叫我应该如何添加?我已经试啦一个下午啦,帮帮忙啊!
Option Explicit

Option Base 1


Dim WithEvents MyOPCServer As OpcServer

Dim WithEvents MyOPCGroup As OPCGroup

Dim MyOPCGroupColl As OPCGroups

Dim MyOPCItemColl As OPCItems

Dim MyOPCItems As OPCItems

Dim MyOPCItem As OPCItem

Dim plcVal() As Variant




Dim ClientHandles(2) As Long

Dim ServerHandles() As Long

Dim Values(2) As Variant

Dim Errors() As Long

Dim ItemIDs(2) As String

Dim GroupName As String

Dim NodeName As String

Dim ServerName As String



'---------------------------------------------------------------------

' Sub StartClient()

' Purpose: Connect to OPC_server, create group and add item

'---------------------------------------------------------------------

Sub StartClient()

On Error GoTo ErrorHandler

'----------- We freely can choose a ClientHandle and GroupName

ClientHandles(1) = 1

ClientHandles(2) = 2



GroupName = "MyGroup"

'----------- Get the ItemID from cell "A1"

NodeName = Range("A1").Value
ServerName = "OPCServer.WinCC" 'Range("B1").Value

ItemIDs(1) = Range("A3").Value

ItemIDs(2) = Range("A4").Value




'增加tag2


'----------- Get an instance of the OPC server

Set MyOPCServer = New OpcServer

MyOPCServer.Connect ServerName, NodeName



Set MyOPCGroupColl = MyOPCServer.OPCGroups

'----------- Set the default active state for adding groups

MyOPCGroupColl.DefaultGroupIsActive = True

'----------- Add our group to the Collection

Set MyOPCGroup = MyOPCGroupColl.Add(GroupName)



Set MyOPCItemColl = MyOPCGroup.OPCItems

'----------- Add one item, ServerHandles are returned

MyOPCItemColl.AddItems 2, ItemIDs, ClientHandles, ServerHandles, Errors

'----------- A group that is subscribed receives asynchronous notifications

MyOPCGroup.IsSubscribed = True

Exit Sub



ErrorHandler:

MsgBox "Error: " & Err.Des cription, vbCritical, "ERROR"
Err.Clear


End Sub



'---------------------------------------------------------------------

' Sub StopClient()

' Purpose: Release the objects and disconnect from the server

'---------------------------------------------------------------------

Sub StopClient()

'----------- Release the Group and Server objects
On Error Resume Next
MyOPCGroupColl.RemoveAll

'----------- Disconnect from the server and clean up

MyOPCServer.Disconnect

Set MyOPCItemColl = Nothing

Set MyOPCGroup = Nothing

Set MyOPCGroupColl = Nothing

Set MyOPCServer = Nothing

End Sub



Private Sub CommandButton1_Click()

End Sub

Private Sub CommandButton2_Click()

End Sub

'---------------------------------------------------------------------

' Sub MyOPCGroup_DataChange()

' Purpose: This event is fired when a value, quality or timestamp in our Group has changed

'---------------------------------------------------------------------

'----------- If OPC-DA Automation 2.1 is installed, use:

Private Sub MyOPCGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)

'----------- Set the spreadsheet cell values to the values read

If NumItems = 1 Then

Select Case ClientHandles(1)
Case 1

Range("B3").Value = CStr(ItemValues(1))

Range("C3").Value = Hex(Qualities(1))

Range("D3").Value = CStr(TimeStamps(1))

Case 2

Range("B4").Value = CStr(ItemValues(1))

Range("C4").Value = Hex(Qualities(1))

Range("D4").Value = CStr(TimeStamps(1))



Case Else

End Select

Else

Range("B3").Value = CStr(ItemValues(1))

Range("C3").Value = Hex(Qualities(1))

Range("D3").Value = CStr(TimeStamps(1))


Range("B4").Value = CStr(ItemValues(2))

Range("C4").Value = Hex(Qualities(2))

Range("D4").Value = CStr(TimeStamps(2))




End If


End Sub



Private Sub MyOPCServer_ServerShutDown(ByVal Reason As String)

End Sub

Private Sub StartOPC_Click()
StartClient
End Sub

Private Sub StopOPC_Click()
StopClient
End Sub

'---------------------------------------------------------------------

' Sub worksheet_change()

' Purpose: This event is fired when our worksheet changes, so we can write a new value

'---------------------------------------------------------------------

Private Sub worksheet_change(ByVal Selection As Range)



Values(1) = Range("B3")
Values(2) = Range("B4")


End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Values(1) = Target

End Sub
请大师们指点迷津。
opc读多个wincc变量到excel的代码,请高手们帮我看下 已锁定
编辑推荐: 关闭

请填写推广理由:

本版热门话题

SIMATIC WinCC / Panel

共有32574条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

  • 分享

  • 只看
    楼主

top
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。