Read Tag Data Continuously
This tutorial will demonstrate how to monitor OAS Tag values continuously by using the AddTags of the OASData.Data class, along with a handler for the OASData.Data.ValuesChangedAll event for just-in-time handling of Tag changes.
This tutorial builds upon the simple application from the Getting Started article.
Tag Syntax
When referencing Tags in any call, it is assumed that you are accessing them on the OAS server you have connected to, localhost by default. However, you can also access remote Tags on any OAS server that can be reached by the client as well as between servers connected via Live Data Cloud. Read more about the proper syntax for accessing Tags and Tag Variables.
Step 1: The Basic Application
The basic application code is a console app with a single static instance of the OASData.Data class. When the app starts, it registers a handler for the ValuesChangedAll event which writes out the current timestamp, value, and data quality of any monitored Tags subscribed with the AddTags method. This information is written to the Console Output.
VB
Imports System
Namespace OASDataSample
Module Program
Private WithEvents oasd As OASData.Data = New OASData.Data()
Sub Main(args As String())
Dim tags = New String() {"Ramp.Value", "Sine.Value", "Random.Value"}
oasd.AddTags(tags)
Console.ReadKey()
End Sub
Private Sub oasd_ValuesChangedAll(
Tags() As String,
Values() As Object,
Qualities() As Boolean,
TimeStamps() As Date) Handles oasd.ValuesChangedAll
For i As Integer = 0 To Tags.Length - 1
If Qualities(i) Then
Console.WriteLine("{0} : {1} : {2} : {3}", TimeStamps(i), Tags(i), Values(i), Qualities(i))
Else
Console.WriteLine("{0} : {1} : {2} : {3}", TimeStamps(i), Tags(i), "Bad Quality", Qualities(i))
End If
Next
End Sub
End Module
End Namespace
C#
using System;
namespace OASDataSample
{
class Program
{
static OASData.Data oasd = new OASData.Data();
static void Main(string[] args)
{
oasd.ValuesChangedAll += Oasd_ValuesChangedAll;
var tags = new string[] { "Ramp.Value", "Sine.Value", "Random.Value" };
oasd.AddTags(tags);
Console.ReadKey();
}
private static void Oasd_ValuesChangedAll(
string[] Tags,
object[] Values,
bool[] Qualities,
DateTime[] TimeStamps)
{
for (int i = 0; i < Tags.Length; i++)
{
if (Qualities[i])
{
Console.WriteLine("{0} : {1} : {2} : {3}", TimeStamps[i], Tags[i], Values[i], Qualities[i]);
}
else
{
Console.WriteLine("{0} : {1} : {2} : {3}", TimeStamps[i], Tags[i], "Bad Quality", Qualities[i]);
}
}
}
}
}
Focusing on the app's Main routine, we'll prompt the user to enter commands to add or remove Tags from being monitored. Then we'll read the input and execute the proper methods of the OASData.Data class.
For adding one single Tag at a time we call AddTag(string tag).
For adding multiple Tags in a single call we use AddTags(string[] tags).
For removing one single Tag at a time we call RemoveTag(string tag).
For removing multiple Tags in a single call we use RemoveTags(string[] tags).
And finally, to remove all tags we use RemoveAllTags().