AdvancedHMI Software
General Category => Support Questions => Topic started by: Brian Broyles 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
-
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:
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:
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:
CLXDriver1.UnSubscribe(SubscriptionID)
-
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
-
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
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