我做了一个VB6.0的程序,连接OPC、读、写,都可以,但是唯独刷新不行。运行起来就是一直提示超下限。
Private MyOPCServer As OPCServer
Private WithEvents MyOPCGroup As OPCGroup
Private MyOPCItems() As OPCItem
Private Sub Command1_Click()
Dim i, j As Integer
Set MyOPCServer = New OPCServer
'connect the OPC Server
MyOPCServer.Connect "OPC.SimaticNET"
'Server State
If MyOPCServer.ServerState = 1 Then
Command1.Enabled = False
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Else
MsgBox "Connection Error!"
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
End If
'add one OPC Group
Set MyOPCGroup = MyOPCServer.OPCGroups.Add("Gruppe1")
'apply for DataChange
MyOPCGroup.IsSubscribed = True
MyOPCGroup.UpdateRate = 500
'add Items
ReDim MyOPCItems(4)
' Set MyOPCItems(0) = MyOPCGroup.OPCItems.AddItem("s7:[S7 connection_1],MB0", 1)
' Set MyOPCItems(1) = MyOPCGroup.OPCItems.AddItem("s7:[S7 connection_1],MB1", 1)
' Set MyOPCItems(2) = MyOPCGroup.OPCItems.AddItem("s7:[S7 connection_1],MB2", 1)
For i = 0 To 2
Set MyOPCItems(i) = MyOPCGroup.OPCItems.AddItem(Text1(i).Text, 1)
Next i
End Sub
Private Sub Command2_Click()
'disconnect the OPC Server
Call MyOPCServer.Disconnect
Set MyOPCServer = Nothing
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
End Sub
问题补充:
还有下半段 不让贴
图片说明:
最佳答案
建议使用UBound函数获取一下下标的最大值。
建议参考:
http://xue163.com/19990/46083/460831993.html
*******************************************************
Dim a() ‘定义数组变量
a = Array("x", "y", "z") ’给数组变量赋值,一共三个值,数组下标分别是0,1,2
MsgBox UBound(a) ''获取最大数组下标的值
提问者对于答案的评价:
最后用这个方法还是没作出来,用了西门子的例子,用了NUMITEMS,可以使用,但仅限于3个数字都改变的情况。比如 三个数是 1,2,3.我把第二个改写为4,然后三个格子变成4,2,3.再读一下,结果是1,4,3.我觉得还是有问题。
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc150290.html