主要代码如下:
Dim ObjServer As OPCServer
Dim GroupCollection As OPCGroups
Dim GroupObj As OPCGroup
Dim MyItems As OPCItems
Private Sub Command1_Click()
Set ObjServer = New OPCServer
'AllOPCServers = ObjServer.GetOPCServers
Call ObjServer.Connect("S7200.OPCServer")
End Sub
Private Sub Command2_Click()
ObjServer.Disconnect
Set ObjServer = Nothing
End Sub
Private Sub Command3_Click()
Set GroupCollection = ObjServer.OPCGroups
GroupCollection.DefaultGroupUpdateRate = 500
GroupCollection.DefaultGroupIsActive = False
Set GroupObj = GroupCollection.Add("MyGroup")
End Sub
Private Sub Command4_Click()
GroupCollection.RemoveAll ' Removes all Groups
Set OPCGroup = Nothing ' Delete OPCGroup Object
Set GroupCollection = Nothing ' Delete OPCGroups Collection Object
End Sub
Private Sub Command5_Click()
Dim i As Integer
Dim ErrorFlag As Boolean
Const MAX_INDEX = 1
Dim lNumItems As Long
Dim lClientHandles(MAX_INDEX) As Long
Dim MyItemServerHandles(MAX_INDEX) As Long
Dim perror() As Long
Dim szItemIDs(MAX_INDEX) As String
Dim AccPath(MAX_INDEX) As Variant
Dim ReqDataTypes(MAX_INDEX) As Variant
szItemIDs(0) = "2,VW98,Word"
szItemIDs(1) = "2,VW100,Word"
'AccPath(0) = ""
'AccPath(1) = ""
'ReqDataTypes(0) = vbVLong
'ReqDataTypes(1) = vbVLong
lClientHandles(0) = 1
lClientHandles(1) = 2
lNumItems = MAX_INDEX
ErrorFlag = False
Set MyItems = GroupObj.OPCItems
'Call MyItems.AddItem("2,VW100,Word", 1)
Call MyItems.AddItems(2, szItemIDs, lClientHandles, MyItemServerHandles, perror)
End Sub
问题补充:
忘记说了,我用的是PC Access 1.0 SP3
在OPC服务器里面能看到S7200.OPCServer
VB里面也能引用
就是运行到AddItems的时候提示类型不匹配
那如果我想读取网络上两台PLC200的数据,在AddItems的格式是什么样的呢?
比如说:szItemIDs(0) = "192.168.0.2,2,VW98,Word"
最佳答案
楼主的代码似乎也没有什么问题,Item的语法是对的。
楼主可以使用$:\Program Files\Siemens\S7-200 PC Access\Samples\Visual Basic下面的例子测试一下,其实楼主的代码和例子中的差不多。
提问者对于答案的评价:
也谢谢了
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc132952.html