问题描述:例子是操作2个参数,我改成操作3个参数,结果不行,以下是部分代码
Private Sub cmdAddItem_Click()
On Error GoTo ErrorHandler
Dim i As Long
Dim ErrorFlag As Boolean
Dim ItemObj As OPCItem
Dim ItemIDs(3) As String
Dim ItemClientHandles(3) As Long
ReDim MyItemServerHandles(3)
Dim Errors() As Long '' Array for returned Item related errors
ErrorFlag = False
Set MyItems = MyGroup.OPCItems '' Get OPCItems Collection Object from MyOPCServer
ItemIDs(1) = txtItem1.Text '' Read ItemId 1 from Text Box
ItemIDs(2) = txtItem2.Text '' Read ItemId 2 from Text Box
ItemIDs(3) = txtItem3.Text '' Read ItemId 2 from Text Box
ItemClientHandles(1) = 1
ItemClientHandles(2) = 2
ItemClientHandles(3) = 3
Call MyItems.AddItems(3, ItemIDs, ItemClientHandles, MyItemServerHandles, Errors)
For i = 1 To 3
If Not Errors(i) = 0 Then
MsgBox "Item " + Str$(i) + " FAILED. Error Code = " + Str$(Errors(i)), vbCritical
ErrorFlag = True
End If
Next
If ErrorFlag Then
Dim RemoveErrors() As Long
Dim RemoveHandles(1) As Long
For i = 1 To 3
If Errors(i) = 0 Then
RemoveHandles(1) = MyItemServerHandles(i)
Call MyItems.Remove(1, RemoveHandles, RemoveErrors)
End If
Next
Else
cmdAddItem.Enabled = False
cmdRemGroup.Enabled = False
cmdRemItem.Enabled = True
cmdWriteSync.Enabled = True
cmdWriteAsync.Enabled = True
cmdReadSync.Enabled = True
cmdReadAsync.Enabled = True
End If
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Adding Items to the Group", vbCritical, "ERROR"
End Sub
问题补充:
Private Sub cmdReadAsync_Click()
On Error GoTo ErrorHandler
Dim i As Long
Dim Errors() As Long '' Array for returned Item related errors
Dim CID As Long '' CancelID, servergenerierter Wert, mit dem die Transaktion identifiziert
MyTID = MyTID + 1 '' Increment Transaction ID
Call MyGroup.AsyncRead(3, MyItemServerHandles, Errors, MyTID, CID)
For i = 1 To 3
If Not Errors(i) = 0 Then MsgBox "Item " + Str$(i) + " FAILED. Error Code = " + Str$(Errors(i)), vbCritical
Next
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + "Reading Items Asyncronous", vbCritical, "ERROR"
End Sub
这个是读的程序
最佳答案
大概看了,但没有试. 但这部分应该没有大问题. 问题可能是出在读或写动作上.
补充:
你用的是异步读方式,故检查异步读的AsyncReadComplete事件处理中,是否处理了相应的句柄,参考:
If (TransactionID = READASYNC_ID) Then
txtOperation = "AsyncRead"
For i = 1 To NumItems
Select Case ClientHandles(i)
Case ClientHandle(1)
Case ClientHandle(2)
Case ClientHandle(3)
Set Item = MyGroup.OPCItems.GetOPCItem(ServerHandle(3))
End Select
另外,把你的Serverhandle和ClientHandle定义为全局变量
提问者对于答案的评价:
多谢
原创文章,作者:ximenziask,如若转载,请注明出处:https://www.zhaoplc.com/plc358254.html