用VB 编写OPC服务器客户端程序,其中老是出现无效的过程调用或参数,求助!
调试程序发现错误出现在读数据“SetOne=MyOPCGroup.OPCItems(Index)”这句,另外,同步写数据是也没有写到服务器中去,程序整了好多天了还是没整出...
调试程序发现错误出现在读数据“Set One = MyOPCGroup.OPCItems(Index)”这句,另外,同步写数据是也没有写到服务器中去,程序整了好多天了还是没整出来。请求专业人士的帮忙解答,谢谢~~
Dim WithEvents MyOPCServer As OPCServer
Dim WithEvents MyOPCGroups As OPCGroups
Dim WithEvents MyOPCGroup As OPCGroup
Dim ServerName As String '服务器名
Dim GroupName As String '组名
Dim MyOPCItems As OPCItems
Dim ItemIDs() As String
Dim ItemNumb As Long
Dim MyServerHandles() As Long
Dim MyClientHandles() As Long
Dim Errors() As Long
Private Sub AddItem_Click()
On Error GoTo ErrorHandler
Set MyOPCGroups = MyOPCServer.OPCGroups
Set MyOPCGroup =MyOPCGroups.Add("GroupOne") '添加组名为Group One的组对象
MyOPCGroups.DefaultGroupUpdateRate = 500
ItemNumb = 4
Set MyOPCItems = MyOPCGroup.OPCItems
ReDim ItemIDs(ItemNumb)
ReDim MyClientHandles(ItemNumb)
ItemIDs(1) = "2,VB200,byte "
ItemIDs(2) = "2,VB201,byte "
ItemIDs(3) = "2,VB202,byte "
ItemIDs(4) = "2,VB203,byte "
Dim i As Integer
MyClientHandles(1) = 1
MyClientHandles(2) = 2
MyClientHandles(3) = 3
MyClientHandles(4) = 4
MyOPCGroup.IsActive = True '准备收发数据
MyOPCGroup.IsSubscribed = True '产生事件
Call MyOPCItems.AddItems(ItemNumb, ItemIDs,MyClientHandles, MyServerHandles, Errors)
End Sub
Private Sub Cmd_connect_Click()
Set MyOPCServer = New OPCServer
Call MyOPCServer.Connect(Text1.Text)
' connect from OPC Server
Cmd_disconnect.Enabled = True
Cmd_connect.Enabled = False
End Sub
Private Sub Cmd_disconnect_Click()
MyOPCServer.Disconnect
Disconnect from OPC Server
Set MyOPCServer = Nothing
Delete OPCServer Object
Cmd_disconnect.Enabled = False
Cmd_connect.Enabled = True
End Sub
Private Sub Cmd_Read_Click() '同步数据读写
Dim One As OPCItem
Dim Index As Long 'Index 为标签顺序号
Dim myValue(4) As Variant
Dim myQuality As Variant
Dim myTimeStamp As Variant
For Index = 1 To 4
Text2(Index - 1).Text =CStr(myValue(Index))
Next
Dim OneRead As String
Dim Xie As String
Set One = MyOPCGroup.OPCItems(Index)
One.Read OPCCache
OneRead = One.Value '读数据
MyOPCGroup.SyncRead OPCCache, ItemNumb,MyServerHandles, myValue, Errors
End Sub
Private Sub Cmd_write_Click()
Dim Index As Long 'Index 为标签顺序号
Dim myValue(4) As Variant
Dim myQuality As Variant
Dim myTimeStamp As Variant
For Index = 1 To 4
myValue(Index) = Text2(Index - 1).Text
Next
MyOPCGroup.SyncWrite 2, MyServerHandles,myValue, Errors
End Sub
Private Sub MyOPCGroup_DataChange(ByVal
TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long,
ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
Dim i As Long
Fori = 1 To NumItems
IfClientHandles(i) > 0 And ClientHandles(i) < 5 Then
Text4(i - 1).BackColor = vbWhite
Text4(i- 1).Text = ItemValues(i)
EndIf
Nexti
End Sub
ps:主要是本人现在没有财富分悬赏了,还请请多多见谅! 展开
Dim WithEvents MyOPCServer As OPCServer
Dim WithEvents MyOPCGroups As OPCGroups
Dim WithEvents MyOPCGroup As OPCGroup
Dim ServerName As String '服务器名
Dim GroupName As String '组名
Dim MyOPCItems As OPCItems
Dim ItemIDs() As String
Dim ItemNumb As Long
Dim MyServerHandles() As Long
Dim MyClientHandles() As Long
Dim Errors() As Long
Private Sub AddItem_Click()
On Error GoTo ErrorHandler
Set MyOPCGroups = MyOPCServer.OPCGroups
Set MyOPCGroup =MyOPCGroups.Add("GroupOne") '添加组名为Group One的组对象
MyOPCGroups.DefaultGroupUpdateRate = 500
ItemNumb = 4
Set MyOPCItems = MyOPCGroup.OPCItems
ReDim ItemIDs(ItemNumb)
ReDim MyClientHandles(ItemNumb)
ItemIDs(1) = "2,VB200,byte "
ItemIDs(2) = "2,VB201,byte "
ItemIDs(3) = "2,VB202,byte "
ItemIDs(4) = "2,VB203,byte "
Dim i As Integer
MyClientHandles(1) = 1
MyClientHandles(2) = 2
MyClientHandles(3) = 3
MyClientHandles(4) = 4
MyOPCGroup.IsActive = True '准备收发数据
MyOPCGroup.IsSubscribed = True '产生事件
Call MyOPCItems.AddItems(ItemNumb, ItemIDs,MyClientHandles, MyServerHandles, Errors)
End Sub
Private Sub Cmd_connect_Click()
Set MyOPCServer = New OPCServer
Call MyOPCServer.Connect(Text1.Text)
' connect from OPC Server
Cmd_disconnect.Enabled = True
Cmd_connect.Enabled = False
End Sub
Private Sub Cmd_disconnect_Click()
MyOPCServer.Disconnect
Disconnect from OPC Server
Set MyOPCServer = Nothing
Delete OPCServer Object
Cmd_disconnect.Enabled = False
Cmd_connect.Enabled = True
End Sub
Private Sub Cmd_Read_Click() '同步数据读写
Dim One As OPCItem
Dim Index As Long 'Index 为标签顺序号
Dim myValue(4) As Variant
Dim myQuality As Variant
Dim myTimeStamp As Variant
For Index = 1 To 4
Text2(Index - 1).Text =CStr(myValue(Index))
Next
Dim OneRead As String
Dim Xie As String
Set One = MyOPCGroup.OPCItems(Index)
One.Read OPCCache
OneRead = One.Value '读数据
MyOPCGroup.SyncRead OPCCache, ItemNumb,MyServerHandles, myValue, Errors
End Sub
Private Sub Cmd_write_Click()
Dim Index As Long 'Index 为标签顺序号
Dim myValue(4) As Variant
Dim myQuality As Variant
Dim myTimeStamp As Variant
For Index = 1 To 4
myValue(Index) = Text2(Index - 1).Text
Next
MyOPCGroup.SyncWrite 2, MyServerHandles,myValue, Errors
End Sub
Private Sub MyOPCGroup_DataChange(ByVal
TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long,
ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
Dim i As Long
Fori = 1 To NumItems
IfClientHandles(i) > 0 And ClientHandles(i) < 5 Then
Text4(i - 1).BackColor = vbWhite
Text4(i- 1).Text = ItemValues(i)
EndIf
Nexti
End Sub
ps:主要是本人现在没有财富分悬赏了,还请请多多见谅! 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询