Author Topic: Reading Multiple Tags with the Stand Alone CLX Driver  (Read 1093 times)

Brian Broyles

  • Newbie
  • *
  • Posts: 2
    • View Profile
Reading Multiple Tags with the Stand Alone CLX Driver
« on: July 13, 2020, 01:49:43 PM »
Hi,  I am looking for some sample code (VB or C#) of how to read multiple unrelated tag names with either a subscription or any other read.  The examples in the included documentation confused me, in that I don't see how the data retrieved by the event handler is associated with the initial tag names using a read.

I am trying to speed up my read loop, currently I am using a background worker, and reading in each tag individually.  I don't have the ability to change the names of the tags on the PLC.

Thanks


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Reading Multiple Tags with the Stand Alone CLX Driver
« Reply #1 on: July 13, 2020, 03:46:00 PM »
Subscriptions are the most efficient way to read when you need periodic updates of values. The driver will optimize the communications for you into as few packets as possible.

To create a subscription:
Code: [Select]
       Try
            SubscriptionID = CLXDriver1.Subscribe("MyTag", 1, AddressOf SubscriptionCallback)
        Catch ex As Exception
            MsgBox("Failed to subscribe. " & ex.Message)
        End Try

The data is returned on a time period based on the PollRateOverride. The callback routine is setup like this:
Code: [Select]
    Private Sub SubscriptionCallback(sender As Object, e As ClxDriver.Common.PlcComEventArgs)
        If e.ErrorId = 0 Then
            Label1.Text = e.PlcAddress & "=" & e.Values(0)
        End If
    End Sub

Be sure to keep all of the SubscriptionID values and unsubscribe when no longer needed:
Code: [Select]
        CLXDriver1.UnSubscribe(SubscriptionID)

Brian Broyles

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Reading Multiple Tags with the Stand Alone CLX Driver
« Reply #2 on: July 16, 2020, 03:31:40 PM »
So I need to set up a subscription for every tag?  I have about 50 of them.  I have the tags in an array, I guess I could create a loop and setup each tag on the next iteration of the loop.

It looks like the single callback can handle all of the tags, correct?

Thanks
« Last Edit: July 16, 2020, 03:35:19 PM by Brian Broyles »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Reading Multiple Tags with the Stand Alone CLX Driver
« Reply #3 on: July 16, 2020, 03:42:07 PM »
Code: [Select]
For i=0 to MyTags.Length-1
       Try
            SubscriptionID = CLXDriver1.Subscribe(MyTag[i], 1, AddressOf SubscriptionCallback)
            SubscriptionList.add(SubscriptionID)
        Catch ex As Exception
            MsgBox("Failed to subscribe to " & MyTag[i] & " : " & ex.Message)
        End Try
Next
Code: [Select]
    Private Sub SubscriptionCallback(sender As Object, e As ClxDriver.Common.PlcComEventArgs)
        If e.ErrorId = 0 Then
            Label1.Text = e.PlcAddress & "=" & e.Values(0)
        End If
    End Sub