创建Opc.Da.Suscription失败 [英] creating Opc.Da.Suscription fails

查看:150
本文介绍了创建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.

推荐答案

如果有人想知道,我解决了这个问题。在代码中:



I solved the problem if anyone wants to know. In the code:

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!


我很惊讶你没有收到编译错误或警告的消息。

如果Me.Variables是真正的Opc.Da.Item []那么你的陈述

Me.variables = Me.opc_server_object.CreateSubscription (groupStateVar)应该在编译之前抛出一个错误。

类型不匹配,订阅是从Me.opc_server_object.CreateSubscription(groupStateVar)返回的内容。



获取商品你可能

Me.variables = Me.opc_server_object.CreateSubscription(groupStateVar).Items





或者只是创建一个参考订阅[这我知道有效 - 因为我已经这样做了。]

Opc.Da.Subscription temp = Me.opc_server_object.CreateSubscription(groupStateVar)

Me。 variables = Me.opc_server_object.Subscriptions [temp] .Items;

对不起,这是C#'ish,但你应该可以很容易地将它翻译成VB.NET。
I am surprised you did not get a message for compile error or warning.
If Me.Variables is truly Opc.Da.Item[] then your statement
Me.variables = Me.opc_server_object.CreateSubscription(groupStateVar) should throw an error before compiling.
There is a type Mismatch, the Subscription is what is returned from Me.opc_server_object.CreateSubscription(groupStateVar).

To get the items you might
Me.variables = Me.opc_server_object.CreateSubscription(groupStateVar).Items


Or just create a ref Subscription [This I know works - because I have done this.]
Opc.Da.Subscription temp = Me.opc_server_object.CreateSubscription(groupStateVar)
Me.variables = Me.opc_server_object.Subscriptions[temp].Items;
Sorry this is C#'ish but you should be able to translate it to VB.NET fairly easily.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆