Skip to content
  • There are no suggestions because the search field is empty.

OPC Data Client - OPC DA - VB.NET - Subscribe to Items Stored in a CSV File

Example code demonstrating how to read in and parse through a .csv file and then subscribe to tags read from the file is below.

Imports System.IO
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.OperationModel

Public Class Form1
    'the following variables are needed to store the textfile data
    Dim text1 As String
    Dim list1 As New List(Of String())

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadTextFile("TestTags.csv")
    End Sub

    Public Sub LoadTextFile(ByVal textFile As String)

        'load the textfile contents into memory
        Dim reader1 As TextReader = New StreamReader(textFile)
        'Dim num1 As Integer = 0

        'Read the lines in from the CSV and add them to a list
        text1 = reader1.ReadLine
        Do While (Not text1 Is Nothing)
            list1.Add(text1.Split(New Char() {","}))
            text1 = reader1.ReadLine
        Loop

        'this line will remove the first line if it contains
        'column names. If your file does not have column names, then delete the line below.
        list1.RemoveAt(0)

        'define an instance of the opc object
        Dim EasyDAClient As New OpcLabs.EasyOpc.DataAccess.EasyDAClient

        'attach to the value changed event handler
        AddHandler EasyDAClient.ItemChanged, AddressOf opc_ItemMeasurementChange

        'now subscribe to our items
        Dim num2 As Integer = 0
        Do While (num2 < list1.Count)
            Dim ServerDescriptor As New OpcLabs.EasyOpc.ServerDescriptor
            ServerDescriptor.MachineName = list1.Item(num2)(0)
            ServerDescriptor.ServerClass = list1.Item(num2)(1)

            Dim ItemDescriptor As New DAItemDescriptor
            ItemDescriptor.ItemId = list1.Item(num2)(2)

            Dim Group As New OpcLabs.EasyOpc.DataAccess.DAGroupParameters(3000, 0)

            'Dim State As Object

            EasyDAClient.SubscribeItem(ServerDescriptor, ItemDescriptor, Group, Nothing)
            num2 += 1
        Loop
    End Sub

    'this is the event handler that receives the changing data
    Private Sub opc_ItemMeasurementChange(ByVal sender As Object, ByVal updatedItem As EasyDAItemChangedEventArgs)
        Dim ItemDescriptor As New DAItemDescriptor
        Dim text1 As String = String.Format("Item: {0}; Value: {1}; Quality: {2}; Timestamp: {3}", New Object() {ItemDescriptor.ItemId, updatedItem.Vtq.Value, updatedItem.Vtq.Quality, updatedItem.Vtq.Timestamp})
        Me.ListBox1.Items.Add(text1)
    End Sub
End Class