本文介绍了创建Opc.Da.Suscription失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow to everyone,我是使用vb.net的opc.net开发人员,现在我正在使用opc.net api为OPC.DA服务器开发新版本的opc客户端。

我的麻烦是当我尝试做一个prepareVars时,这包括用opc.items生成一个数组。我的代码:





Hellow to everybody, i'm opc.net developer using vb.net, nowadays i'm working on a new version of an opc client for an OPC.DA server using opc.net api.
My trouble is when i try to do a prepareVars, this consists in generate an array with opc.items. My code:


'Opc.Da.SubscriptionState(groupState) : OPC Group Properties, required to read/write
        Dim groupStateVar As Opc.Da.SubscriptionState = New Opc.Da.SubscriptionState
        'GroupState Name
        groupStateVar.Name = "variables"
        'GroupState Update time
        groupStateVar.UpdateRate = 1000
        'GroupState Active state
        groupStateVar.Active = True
        'Opc.Da.Subscription(group) : Group of OPC items(OPC variables) creation using Opc.Da.SubscriptionState(groupState)
        Me.variables = Me.opc_server_object.CreateSubscription(groupStateVar)
        Me.opc_server_object.CreateSubscription(suscriptionState)
        Dim items As Opc.Da.Item() = New Opc.Da.Item(allowed_variable_names.Count - 1) {}
        Me.items_list_vars = items
        Dim position As Integer = 0
        For Each varName In Me.variable_names
           
            If allowed_variable_names.Contains(varName) Then
                items(position) = New Opc.Da.Item()
                items(position).ItemName = varName
                
                position += 1
            End If
        Next
        If items.Count <> 0 Then
            items = Me.variables.AddItems(items)
        End If





问题是当我尝试准备Me.variables时这总是返回NullReferenceException被用户代码解除



我在网上找到了一个示例,帮助我生成这种测试:





The problem is when i try to prepare "Me.variables" This allways return a "NullReferenceException was unhndled by user code"

I found an example on the net an helps me to generate this kind of test:

'########## SERVER CONNECTION ##################
        'OpcCom.Factory(fact) : required for server connection
        Dim fact As OpcCom.Factory = New OpcCom.Factory()
        'Opc.URL(url) : required for server connection, defines opc server url using "opcda://" protocol
        Dim url = New Opc.URL("opcda://localhost/CoDeSys.OPC.DA")
        'Opc.Da.Server(opc_server_object) : Contructor for server object
        Dim opc_server_object = New Opc.Da.Server(fact, Nothing)
        'Function used to connecto to OPC Server (OPC .NET API 2.0)
        opc_server_object.Connect(url, New Opc.ConnectData(New System.Net.NetworkCredential))

        '########## SERVER PROPERTIES ##################
        'Opc.Da.ServerStatus(server_status) : Used to get all server properties
        Dim server_status As Opc.Da.ServerStatus = opc_server_object.GetStatus()
        myTraceLine("Vendor Info: " & server_status.VendorInfo)
        myTraceLine("Product Version: " & server_status.ProductVersion)
        myTraceLine("Status Info: " & server_status.StatusInfo)
        myTraceLine("Start Time: " & Opc.Convert.ToString(server_status.StartTime))
        myTraceLine("Current Time: " & Opc.Convert.ToString(server_status.CurrentTime))

        '########## ITEM GROUP DEFINITION BLOCK ##################
        'Opc.Da.SubscriptionState(groupState) : OPC Group Properties, required to read/write 
        Dim groupState = New Opc.Da.SubscriptionState
        'GroupState Name
        groupState.Name = "readGroup"
        'GroupState Update time
        groupState.UpdateRate = 1000
        'GroupState Active state
        groupState.Active = True
        'Opc.Da.Subscription(group) : Group of OPC items(OPC variables) creation using Opc.Da.SubscriptionState(groupState) 
        group = opc_server_object.CreateSubscription(groupState)

        '########## ITEM GROUP BLOCK ##################
        'Array of variable names as Opc.Da.Item's 
        Dim items As Opc.Da.Item() = New Opc.Da.Item(2) {}
        items(0) = New Opc.Da.Item()
        items(0).ItemName = "PLCX_varname1"
        items(1) = New Opc.Da.Item()
        items(1).ItemName = "PLCX_varname2"
        items(2) = New Opc.Da.Item()
        items(2).ItemName = "PLCX_varname3"
        items = group.AddItems(items)

        '########## WRITE BLOCK #######################
        Dim item_values As Opc.Da.ItemValue() = New Opc.Da.ItemValue(2) {}
        item_values(0) = New Opc.Da.ItemValue()
        item_values(1) = New Opc.Da.ItemValue()
        item_values(2) = New Opc.Da.ItemValue()
        item_values(0).ServerHandle = items(0).ServerHandle
        item_values(0).Value = True
        item_values(1).ServerHandle = items(1).ServerHandle
        item_values(1).Value = False
        item_values(2).ServerHandle = items(2).ServerHandle
        item_values(2).Value = 90
        group.Write(item_values)


        '########## READ BLOCK #######################
        Dim results As Opc.Da.ItemValueResult() = group.Read(items)
        'group.DataChanged += New Opc.Da.DataChangedEventHandler(OnTransactionCompleted)
        'Opc.Da.ItemValueResult(results()) : Used to store read results


        '########## DATA OUTPUT ######################
        For Each result In results
            myTraceLine("Result: " & " key: " & result.Key & " value: " & result.Value)
        Next
        Dim filter As Opc.Da.BrowseFilters = New Opc.Da.BrowseFilters()
        myTraceLine("filter values: " & filter.MaxElementsReturned)
        Dim pos As Opc.Da.BrowsePosition = New Opc.Da.BrowsePosition(New Opc.ItemIdentifier, filter)
        Dim br_elem As Opc.Da.BrowseElement() = New Opc.Da.BrowseElement(1) {}
        br_elem = opc_server_object.Browse(New Opc.ItemIdentifier, filter, pos)
        myTraceLine("Result: " & br_elem(0).Name)





这运行正常,示例之间的第一个差异是一个memeber类opcsuscription,第一个例子是类的一部分,我使用Me.variables(定义为opcitem数组),第二个示例是使用Dim创建的(不是类属性)



我的问题是,是否有人遇到同样的问题,如果有解决方案或我做错了什么。



非常感谢和抱歉,我试图解释我能做到的最佳方式。我会帮助你。



This runs ok, the first diference between examples is a memeber class opcsuscription, in first example is part from class and i'm using Me.variables (defined as opcitem array) and on second example is created with Dim (not class property)

My question is if anybody was on same problem and if had solution or what I'm doing wrong.

Thanks a lot and sorry I was tryied to explain the best way i can. I will apreciate your help.

推荐答案

For Each varName In Me.variable_names

           If allowed_variable_names.Contains(varName) Then
               items(position) = New Opc.Da.Item()
               items(position).ItemName = varName

               position += 1
           End If
       Next





...在此为每个i填充允许变量的项目,但在某些情况下我有没有任何值的项目上的位置,这里是使用StringCollection修复它的代码:





... in this for each i fill items with allowed variables, but in some cases i had positions on items with nothing values, here is the code to fix it using a StringCollection:

Dim vars As StringCollection = New StringCollection

For Each varName In Me.variable_names
    If allowed_variable_names.Contains(varName) Then
        vars.Add(varName)
    End If
Next
Dim items As Opc.Da.Item() = New Opc.Da.Item(vars.Count - 1) {}
Dim position As Integer = 0
For Each var In vars
    items(position) = New Opc.Da.Item()
    items(position).ItemName = var
    position += 1
Next
Me.items_list_vars = items
If items.Count <> 0 Then
    items = Me.variables.AddItems(items)
End If





此代码正确运行,现在我拥有所有项目值,谢谢!



This code runs correctly and now i have all item values, thanks!



这篇关于创建Opc.Da.Suscription失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:42