Option Explicit
Option Base 1
Private Sub A1_Click()
Dim WithEvents MyOPCServer As OPCServer '这样声明不了,报子程序或函数属性无效不知为什么啊
'Dim MyOPCServer As OPCServer 只有这样声明才行
Dim MyOPCGroup As OPCGroup
Dim MyOPCGroups As OPCGroups
Dim MyOPCItem1s As OPCItems
Dim MyOPCItem As OPCItem
Dim itemcount As Long
Dim itemid(1) As String
Dim itemclienthandle(1) As Long
Dim itemserverhandle() As Long
Dim itemservererr1() As Long
Dim datatype As Variant
Dim path As Variant
Dim source As Integer
Dim numitem As Long
Dim serverhandles(1) As Long
Dim values() As Variant
Dim err() As Long
Dim qualitities() As Variant
Dim timestamps() As Variant
Set MyOPCServer = New OPCServer
MyOPCServer.Connect "OPCServer.WinCC", "USER-20151013JC"
Set MyOPCGroups = MyOPCServer.OPCGroups
MyOPCGroups.DefaultGroupUpdateRate = 1000
MyOPCGroups.DefaultGroupIsActive = True
Set MyOPCGroup = MyOPCGroups.Add("MyGroup")
Set MyOPCItem1s = MyOPCGroup.OPCItems
MyOPCGroup.IsSubscribed = True
itemcount = 1
itemclienthandle(1) = 1
source = OPCCache
itemid(1) = "aaa" 'WINCC内部变量”aaa"
MyOPCItem1s.AddItems itemcount, itemid, itemclienthandle, itemserverhandle, itemservererr1
numitem = 1
serverhandles(1) = 1
MyOPCGroup.SyncRead source, numitem, serverhandles, values, err
Text1.Text = values(1)
MsgBox CStr(values(1)) '读出数一直为空
'MsgBox MyOPCGroups.Count
'MsgBox MyOPCServer.ServerName
'MsgBox MyOPCServer.ServerState
'MsgBox MyOPCServer.StartTime
'MsgBox MyOPCServer.LastUpdateTime
'MsgBox MyOPCServer.LocaleID '1
End Sub1
上面不知为什么用不了,withevents 语句,在WINCC1运行时,VB可以连上WINCC1,但读不出数据不知为什么,在此请在神指教