0

我们正在使用以下文档来创建一个 VB.NET Windows 应用程序来与 KepServerEx 通信。

标题:ClientAce:创建一个简单的 Windows 窗体应用程序

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

使用上述文档,我们可以成功地从 KepServerEx 读取数据。

但是,我们也希望将在 VB.NET 应用程序中输入的数据发送回 KepServerEx。这可能吗?

4

1 回答 1

0

通过使用以下代码解决 -

“Kepware.ClientAce.OpcDaClient.DaServerMgt.Write(itemIdentifiers, OPCWriteValue)”

在哪里 -

项目标识符(n)

  • Kepware.ClientAce.OpcDaClient.ItemIdentifier
  • 包含标签定义

OPCWriteValue(n)

  • Kepware.ClientAce.OpcDaClient.ItemValue
  • 包含标签值

完整代码如下:

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
于 2020-12-07T04:51:40.737 回答