Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
 
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 23-Jan-13 6:34am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
... 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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solved! if you need help with something like this try to send a message Wink | ;)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
i am new to this OPC protocol implementation , i have to integrate my application which is in MFC and vc++ with some other company OPC protocol to send the alarm/event notification to existing BMS application. Can you please give some idea how should start to integrate this.
  Permalink  
Comments
Ferran Garriga at 6-Jun-14 13:11pm
   
Hi manu,
I highly recommend that first of all you obtain information enough to know that protocol specifically you intend to use and take a few examples of code to read and write varaibles on the server, once this is done, continuing perhaps using collections and objects of server.
I do not know the use of the protocols using c + +, but once created a project and added the dll needed for development using the Explorer object in IDE's such as Visual Studio, to get an idea of the objects and methods available for the purpose your application.
 
Hope this helps and good luck ;)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 380
1 OriginalGriff 230
2 DamithSL 220
3 Zoltán Zörgő 150
4 Maciej Los 135
0 OriginalGriff 7,740
1 DamithSL 5,694
2 Sergey Alexandrovich Kryukov 5,404
3 Maciej Los 5,061
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 6 Jun 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100