Click here to Skip to main content
15,896,912 members

creating Opc.Da.Suscription fails

Ferran Garriga asked:

Open original thread
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:


VB
'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


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)


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.
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900