kepserverex

Is it possible to read/write tags in kepserver using VB.NET application?


We are using following document for creating a VB.NET windows application to communicate with KepServerEx.

Title: ClientAce: Creating a Simple Windows Form Application

https://www.kepware.com/getattachment/66dac2e9-1496-4b22-9301-454e506a5ca6/clientace-simple-windows-form-application.pdf

Using the above document, we could successfully read data from KepServerEx.

However we also want to send the data inputted in VB.NET application back to the KepServerEx. Is this possible?


Solution

  • Solved by using following code -

    "Kepware.ClientAce.OpcDaClient.DaServerMgt.Write(itemIdentifiers, OPCWriteValue)"

    where -

    itemIdentifiers(n)

    OPCWriteValue(n)

    Full code below:

    Imports Kepware.ClientAce    
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Dim maxAge As Integer = 0
        Dim WithEvents daServerMgt As New Kepware.ClientAce.OpcDaClient.DaServerMgt
        Dim activeClientSubscriptionHandle As Integer
        Dim activeServerSubscriptionHandle As Integer
        Dim itemIdentifiers(1) As Kepware.ClientAce.OpcDaClient.ItemIdentifier
        Dim itemValues(1) As Kepware.ClientAce.OpcDaClient.ItemValue
        Dim OPCWriteValue(1) As Kepware.ClientAce.OpcDaClient.ItemValue
    
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
            Dim url As String
            url = "opcda://localhost/Kepware.KEPServerEX.V6"
            Dim clientHandle As Integer
            clientHandle = 1
    
            Dim connectInfo As New Kepware.ClientAce.OpcDaClient.ConnectInfo
            connectInfo.ClientName = "OPC UA Test Client"
            connectInfo.LocalId = "en"
            connectInfo.KeepAliveTime = 60000
            connectInfo.RetryAfterConnectionError = True
            connectInfo.RetryInitialConnection = False
            Dim connectFailed As Boolean
            connectFailed = False
    
            itemIdentifiers(0) = New Kepware.ClientAce.OpcDaClient.ItemIdentifier
            itemIdentifiers(0).ItemName = "Channel1.Device1.Tag1"
            itemIdentifiers(0).ClientHandle = 0
            itemIdentifiers(0).DataType = Type.GetType("System.Int16")
    
            itemIdentifiers(1) = New Kepware.ClientAce.OpcDaClient.ItemIdentifier
            itemIdentifiers(1).ItemName = "Channel1.Device1.Tag2"
            itemIdentifiers(1).ClientHandle = 1
            itemIdentifiers(1).DataType = Type.GetType("System.Int16")
    
            Try
                daServerMgt.Connect(url, clientHandle, connectInfo, connectFailed)
        
            Catch ex As Exception
                MsgBox("Handled Connect exception. Reason: " & ex.Message)
    
                ' Make sure following code knows connection failed:
                connectFailed = True
    
            End Try
    
            Dim clientSubscriptionHandle As Integer = 1
            Dim active As Boolean = True
            Dim updateRate As Integer = 1000
            Dim deadBand As Single = 0
            Dim revisedUpdateRate As Integer
    
            Try
                daServerMgt.Subscribe(clientSubscriptionHandle, active, updateRate, revisedUpdateRate, deadBand, itemIdentifiers, activeServerSubscriptionHandle)
    
                ' Handle result:
    
                ' Save the active client subscription handle for use in 
                ' DataChanged events:
                activeClientSubscriptionHandle = clientSubscriptionHandle
    
                ' Check item result ID:
                For itemIndex = 0 To 1
                    If itemIdentifiers(itemIndex).ResultID.Succeeded = False Then
    
                        ' Show a message box if an item could not be added to subscription.
                        ' You would probably use some other, less annoying, means to alert
                        ' the user to failed item enrolments in an actual application. 
                        MsgBox("Failed to add item " & itemIdentifiers(itemIndex).ItemName & " to subscription")
                    End If
                Next
    
            Catch ex As Exception
                MsgBox("Handled Subscribe exception. Reason: " & ex.Message)
            End Try
    
            Try
    
                daServerMgt.Read( _
                maxAge, _
                itemIdentifiers, _
                itemValues)
    
                ' Handle results
    
                Dim item As Integer
    
                For item = 0 To 1
    
                    If itemIdentifiers(item).ResultID.Succeeded = True Then
    
                        Console.WriteLine( _
                        "Value: " & itemValues(item).Value & _
                        " Quality: " & itemValues(item).Quality.Name & _
                        " Timestamp: " & itemValues(item).TimeStamp)
    
                    Else
    
                        Console.WriteLine("Read failed for item: " & _
                        itemIdentifiers(item).ItemName)
    
                    End If
    
                Next
    
            Catch ex As Exception
    
                Console.WriteLine("Read exception. Reason: " & ex.Message)
    
            End Try
    
        End Sub
    
       Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            OPCWriteValue(0) = New Kepware.ClientAce.OpcDaClient.ItemValue
            OPCWriteValue(0).Value = TextBox1.Text
            OPCWriteValue(1) = New Kepware.ClientAce.OpcDaClient.ItemValue
            OPCWriteValue(1).Value = TextBox2.Text
            Try
                daServerMgt.Write(itemIdentifiers, OPCWriteValue)
            Catch ex As Exception
                'Handle the write exception
                Console.WriteLine("Sync write failed with exception " & ex.Message)
            End Try
    
        End Sub
    
    End Class