Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
Posted

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

VB
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


... 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:

VB
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!
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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