【问题标题】:OPC DA Client - Unable to assign item.Value to VBA variableOPC DA 客户端 - 无法将 item.Value 分配给 VBA 变量
【发布时间】:2021-05-26 01:16:45
【问题描述】:

下面是一个用 VBA 编写的 OPC 客户端。它使用 OPC Foundation DA 库。我能够获取项目的当前值(我可以在本地窗口中读取它),但它没有将值分配给 myValue = theItem.Value 在休息期间悬停在 theItem.Value 上也会显示该值。

有什么想法吗?

Public Sub ReadValue()
    Dim serverNames As Variant
    Dim listServers As Variant
    Dim i As Integer
    Dim theStates As Variant

    Set theServer = New OPCServer
    serverNames = theServer.GetOPCServers
    theStates = Array("Disconnected", "Running", "Failed", "No Configuration", "Suspended", "In Test")
    For i = LBound(serverNames) To UBound(serverNames)
        Debug.Print (serverNames(i))
    Next i
    
    theServer.Connect ("MyOPCServer")
    Debug.Print theServer.VendorInfo
    Debug.Print theServer.MajorVersion & "." & theServer.MinorVersion
    Debug.Print theStates(theServer.ServerState)
    Debug.Print theServer.StartTime
    Debug.Print theServer.CurrentTime
    Debug.Print theServer.LastUpdateTime

    
    
    'Groups
        Dim theGroup As OPCGroup
        Dim theGroups As OPCGroups
        If theGroups Is Nothing Then
            Set theGroups = theServer.OPCGroups
        End If
        If theGroup Is Nothing Then
            Set theGroup = theGroups.Add("testing")
            txtName = theGroup.name
        End If
    
        theGroup.UpdateRate = CLng(1000)
        theGroup.DeadBand = CLng(1)
        theGroup.TimeBias = CLng(0)
        theGroup.IsActive = CBool(1)
        theGroup.IsSubscribed = CBool(1)
        
        
'
    Dim theItem As OPCItem
    Dim theItem1 As OPCItem
    Dim myItems As Variant
    Dim myValue As Variant
    Dim myWriteValues As Variant
    Dim handles(1) As Long
    Dim Errors() As Long
    Dim CancelID As Long
    Dim TransID As Long
    
    myItems = Array("MyPathBlahBlahBlah.CV")
    myWriteValues = Array(8, 0, 1)
    
    For i = LBound(myItems) To UBound(myItems)
        Set theItem = theGroup.OPCItems.AddItem(myItems(i), currentHandle)
        myValue = theItem.Value
        handles(1) = theGroup.OPCItems.Item(1).ServerHandle
        theGroup.OPCItems.Remove 1, handles, Errors

    Next i

   theServer.Disconnect
   
End Sub

【问题讨论】:

    标签: excel vba opc


    【解决方案1】:

    审核后/疑难解答。

    OPCItem 对象提供了读取服务器中项目的当前值并将新值写入项目的方法。我已将这些工具包含在此对话框中。 OPCItem 对象上提供的读取方法从服务器执行同步读取,并且可以配置为从缓存或设备读取。要从缓存中读取,组和项都应该处于活动状态,但直接从设备进行的同步读取操作不依赖于组或项的活动状态。

    添加以下代码允许我分配给变量。

    Dim source As OPCDataSource
    Dim myValue As Variant
    source = OPCDevice
    theItem.Read source, myValue
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2021-08-04
      • 2013-12-15
      • 2012-07-21
      相关资源
      最近更新 更多