我写了个OPC客户端程序,但读不上来数据,为什么呢?
程序如下:
Option Explicit
Dim ServerName As String
Dim NodeName As String
Dim GroupName As String
Dim MaxITEMNum As Long
Dim BranchCnt As Integer
Dim LeafCnt As Integer
Dim WithEvents MyOPCServer As OPCServer
Dim WithEvents MyOPCGroup As OPCGroup
Dim MyOPCGroupColl As OPCGroups
Dim MyOPCItemColl As OPCItems
Dim MyOPCItems As OPCItems
Dim MyOPCItem As OPCItem
'
Dim ClientHandles(4) As Long
Dim ServerHandles() As Long
Dim DataReadItem(4) As String
Dim DataReadValue(4) As Variant
Dim errors() As Long
Dim ServerHandlesIn() As Long
Dim ServerHandlesOut() As Long
Dim TrandID As Long
Dim CancelID As Long
'
Dim itemv() As Variant
Dim itemq() As Long
Dim OPCVache As Integer
Dim ItemNums As Long
Dim ii As Integer
Private Sub Connect_Click()
Dim ItemNum As Integer
Set MyOPCServer = New OPCServer
'OPCServer connection
ServerName = Text1.Text
NodeName = "WWW-6C0E52EE8D2"
MyOPCServer.Connect ServerName, NodeName
'Create OPCGroups
Set MyOPCGroupColl = MyOPCServer.OPCGroups
MyOPCGroupColl.DefaultGroupIsActive = True
'Create ReadGroup
Set MyOPCGroup = MyOPCGroupColl.Add("Group1")
'add items for Read group
'-----------Settings for ReadGroup
MyOPCGroup.IsSubscribed = True 'must be true when using asyn read and write
MyOPCGroup.UpdateRate = 1000
ItemNums = 4
DataReadItem(1) = "NewTag"
DataReadItem(2) = "NewTag_1"
DataReadItem(3) = "NewTag_2"
DataReadItem(4) = "NewTag_3"
ClientHandles(1) = 1
ClientHandles(2) = 2
ClientHandles(3) = 3
ClientHandles(4) = 4
MyOPCGroup.OPCItems.AddItems ItemNums, DataReadItem(), ClientHandles(), ServerHandles(), errors()
End Sub
Private Sub GetServerList_Click()
Call GetServer
End Sub
Sub GetServer()
List1.Clear
'initiate a OPCServer
Set MyOPCServer = New OPCServer
'get this node's server list
Dim ServerList
NodeName = "WWW-6C0E52EE8D2"
'Return a array of Severnamelist
ServerList = MyOPCServer.GetOPCServers(NodeName)
'list all the servers
For ii = LBound(ServerList) To UBound(ServerList)
List1.AddItem (ServerList(ii))
Next ii
Set MyOPCServer = Nothing
End Sub
Private Sub List1_Click()
Text1.Text = List1.Text
End Sub
Private Sub Read_Click()
执行到该处报错
Call MyOPCGroup.SyncRead(OPCVache, ItemNums, ServerHandles, DataReadValue, errors)
For ii = 1 To ItemNums
List2.AddItem (DataReadItem(ii) & "=" & DataReadValue(ii))
Next
End Sub
请帮忙看一下,问题出在哪呢?
[解决办法]
帮你顶。
[解决办法]
单单这样看很辛苦啊!帮你顶下
[解决办法]
不要只是顶,帮忙解决问题呀!!!!
[解决办法]
先用PLC厂家自己的OPC客户端测试一下,搞清楚通信是否正常。另,如果是本机,那么不需要写节点名。
你用哪家的PLC?OPC Server是什么?看上去和我平时用的代码不太一样。
[解决办法]
通讯是没问题的,WinccOPCServer
Connect 都可以,就是不能读取上数据,真是头痛
用网上收来例程也未能实现!!!!!!