Read Tag Data Continuously
This tutorial will demonstrate how to monitor OAS Tag values continuously by using the AddTag, AddTags, RemoveTag, RemoveTags, and RemoveAllTags method 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. This information is written to the Visual Studio debug Output pane.
VB
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Namespace OASDataSample
Class Program
Shared oasd As OASData.Data = New OASData.Data()
Private Shared Sub Main(ByVal args As String())
oasd.ValuesChangedAll += AddressOf Oasd_ValuesChangedAll
End Sub
Private Shared Sub Oasd_ValuesChangedAll(ByVal Tags As String(),
ByVal Values As Object(),
ByVal Qualities As Boolean(),
ByVal TimeStamps As DateTime())
For i As Integer = 0 To Tags.Length - 1
System.Diagnostics.Debug.WriteLine("{0} : {1} : {2} : {3}", TimeStamps(i), Tags(i), Values(i), Qualities(i))
Next
End Sub
End Class
End Namespace
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASDataSample
{
class Program
{
static OASData.Data oasd = new OASData.Data();
static void Main(string[] args)
{
oasd.ValuesChangedAll += Oasd_ValuesChangedAll;
}
private static void Oasd_ValuesChangedAll(
string[] Tags,
object[] Values,
bool[] Qualities,
DateTime[] TimeStamps)
{
for(int i = 0; i < Tags.Length; i++)
{
System.Diagnostics.Debug.WriteLine("{0} : {1} : {2} : {3}",
TimeStamps[i], Tags[i], Values[i], Qualities[i]);
}
}
}
}
Step 2: Gather Input Arguments
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().
Of course, this code illustrates using the AddTag and RemoveTag methods for adding/removing one Tag at a time, but could be collapsed to just use the AddTags and RemoveTags to achieve the same goal.
VB
Private Shared Sub Main(ByVal args As String())
oasd.ValuesChangedAll += Oasd_ValuesChangedAll
Console.WriteLine("Type 'add <tag> ...' to start watching one or more tags.")
Console.WriteLine("Type 'remove <tag> ...' to stop watching one or more tags.")
Console.WriteLine("Type 'removeall' to stop watching all tags.")
Console.WriteLine("Type ENTER to quit.")
While True
Dim raw_cmd As String = Console.ReadLine()
Dim tag As String = Nothing
Dim tags As String() = Nothing
If String.IsNullOrWhiteSpace(raw_cmd) Then
Exit While
End If
Dim cmds As String() = raw_cmd.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
If cmds.Length = 2 Then
tag = cmds(1)
ElseIf cmds.Length > 2 Then
tags = New String(cmds.Length - 1 - 1) {}
Array.Copy(cmds, 1, tags, 0, tags.Length)
End If
Dim command As String = cmds(0).ToLower()
Select Case command
Case "add"
If Not String.IsNullOrWhiteSpace(tag) Then
oasd.AddTag(tag)
Else
oasd.AddTags(tags)
End If
Case "remove"
If Not String.IsNullOrWhiteSpace(tag) Then
oasd.RemoveTag(tag)
Else
oasd.RemoveTags(tags)
End If
Case "removeall"
oasd.RemoveAllTags()
Case Else
Continue While
End Select
End While
End Sub
C#
static void Main(string[] args)
{
oasd.ValuesChangedAll += Oasd_ValuesChangedAll;
Console.WriteLine("Type 'add <tag> ...' to start watching one or more tags.");
Console.WriteLine("Type 'remove <tag> ...' to stop watching one or more tags.");
Console.WriteLine("Type 'removeall' to stop watching all tags.");
Console.WriteLine("Type ENTER to quit.");
while (true)
{
string raw_cmd = Console.ReadLine();
string tag = null;
string[] tags = null;
if (string.IsNullOrWhiteSpace(raw_cmd)) { break; }
string[] cmds = raw_cmd.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
if (cmds.Length == 2)
{
tag = cmds[1];
}
else if (cmds.Length > 2)
{
// copy all of the strings after the command
tags = new string[cmds.Length-1];
Array.Copy(cmds, 1, tags, 0, tags.Length);
}
string command = cmds[0].ToLower();
switch(command)
{
case "add":
if (!string.IsNullOrWhiteSpace(tag))
{
oasd.AddTag(tag);
}
else
{
oasd.AddTags(tags);
}
break;
case "remove":
if (!string.IsNullOrWhiteSpace(tag))
{
oasd.RemoveTag(tag);
}
else
{
oasd.RemoveTags(tags);
}
break;
case "removeall":
oasd.RemoveAllTags();
break;
default:
continue;
}
}
}
Step 3: The Full Application
Below is the full source code of the sample application.
VB
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Namespace OASDataSample
Class Program
Shared oasd As OASData.Data = New OASData.Data()
Private Shared Sub Main(ByVal args As String())
oasd.ValuesChangedAll += AddressOf Oasd_ValuesChangedAll
Console.WriteLine("Type 'add <tag> ...' to start watching one or more tags.")
Console.WriteLine("Type 'remove <tag> ...' to stop watching one or more tags.")
Console.WriteLine("Type 'removeall' to stop watching all tags.")
Console.WriteLine("Type ENTER to quit.")
While True
Dim raw_cmd As String = Console.ReadLine()
Dim tag As String = Nothing
Dim tags As String() = Nothing
If String.IsNullOrWhiteSpace(raw_cmd) Then
Exit While
End If
Dim cmds As String() = raw_cmd.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
If cmds.Length = 2 Then
tag = cmds(1)
ElseIf cmds.Length > 2 Then
tags = New String(cmds.Length - 1 - 1) {}
Array.Copy(cmds, 1, tags, 0, tags.Length)
End If
Dim command As String = cmds(0).ToLower()
Select Case command
Case "add"
If Not String.IsNullOrWhiteSpace(tag) Then
oasd.AddTag(tag)
Else
oasd.AddTags(tags)
End If
Case "remove"
If Not String.IsNullOrWhiteSpace(tag) Then
oasd.RemoveTag(tag)
Else
oasd.RemoveTags(tags)
End If
Case "removeall"
oasd.RemoveAllTags()
Case Else
Continue While
End Select
End While
End Sub
Private Shared Sub Oasd_ValuesChangedAll(ByVal Tags As String(), ByVal Values As Object(), ByVal Qualities As Boolean(), ByVal TimeStamps As DateTime())
For i As Integer = 0 To Tags.Length - 1
System.Diagnostics.Debug.WriteLine("{0} : {1} : {2} : {3}", TimeStamps(i), Tags(i), Values(i), Qualities(i))
Next
End Sub
End Class
End Namespace
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASDataSample
{
class Program
{
static OASData.Data oasd = new OASData.Data();
static void Main(string[] args)
{
oasd.ValuesChangedAll += Oasd_ValuesChangedAll;
Console.WriteLine("Type 'add <tag> ...' to start watching one or more tags.");
Console.WriteLine("Type 'remove <tag> ...' to stop watching one or more tags.");
Console.WriteLine("Type 'removeall' to stop watching all tags.");
Console.WriteLine("Type ENTER to quit.");
while (true)
{
string raw_cmd = Console.ReadLine();
string tag = null;
string[] tags = null;
if (string.IsNullOrWhiteSpace(raw_cmd)) { break; }
string[] cmds = raw_cmd.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
if (cmds.Length == 2)
{
tag = cmds[1];
}
else if (cmds.Length > 2)
{
// copy all of the strings after the command
tags = new string[cmds.Length-1];
Array.Copy(cmds, 1, tags, 0, tags.Length);
}
string command = cmds[0].ToLower();
switch(command)
{
case "add":
if (!string.IsNullOrWhiteSpace(tag))
{
oasd.AddTag(tag);
}
else
{
oasd.AddTags(tags);
}
break;
case "remove":
if (!string.IsNullOrWhiteSpace(tag))
{
oasd.RemoveTag(tag);
}
else
{
oasd.RemoveTags(tags);
}
break;
case "removeall":
oasd.RemoveAllTags();
break;
default:
continue;
}
}
}
private static void Oasd_ValuesChangedAll(
string[] Tags,
object[] Values,
bool[] Qualities,
DateTime[] TimeStamps)
{
for(int i = 0; i < Tags.Length; i++)
{
System.Diagnostics.Debug.WriteLine("{0} : {1} : {2} : {3}",
TimeStamps[i], Tags[i], Values[i], Qualities[i]);
}
}
}
}