Read Tag Data Synchronously
Using the ValuesChangedAll event to detect changes to Tag data is the most efficient method of reading real time Tag data. Yet, there may be times when you prefer to us a blocking call to request Tag data. This is accomplished using the OASData.Data.SyncReadTags. With this operation you can request current values for one or more Tags directly.
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
Similar to the basic applications in previous asynchronous examples, this sample uses a single static instance of the OASData.Data class. However, instead of adding and removing tags based on user input, we'll take the user input and issue a single synchronous call to the OAS server to retrieve Tag data.
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())
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)
{
// additional code here
}
}
}
Step 2: Gather Tag Data and display results
Next we'll build an array of strings that we'll pass into the SyncReadTags method which returns an array of values matching the length and order of the array of Tags we pass in. You'll notice that we also construct an errors array that is passed in by reference and will be populated with error codes for each tag passed in.
- 0 : no error
- 1 : bad data quality
- 2 : timeout
All that is left is to iterate through the responses and to display values.
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())
Console.WriteLine("Type one or more Tags to read synchronously.")
Console.WriteLine("Type ENTER to quit.")
While True
Dim raw_cmd As String = Console.ReadLine()
If String.IsNullOrWhiteSpace(raw_cmd) Then
Exit While
End If
Dim tags As String() = raw_cmd.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
Dim errs As Integer() = New Integer(tags.Length - 1) {}
Dim data As Object() = oasd.SyncReadTags(tags, errs)
If data IsNot Nothing AndAlso data.Length > 0 Then
For i As Integer = 0 To tags.Length - 1
Console.WriteLine("{0} : {1} : {2}", tags(i), data(i), (errs(i) = 0))
Next
End If
End While
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)
{
Console.WriteLine("Type one or more Tags to read synchronously.");
Console.WriteLine("Type ENTER to quit.");
while (true)
{
string raw_cmd = Console.ReadLine();
if (string.IsNullOrWhiteSpace(raw_cmd)) { break; }
string[] tags = raw_cmd.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
int[] errs = new int[tags.Length];
object[] data = oasd.SyncReadTags(tags, ref errs);
if (data != null && data.Length > 0)
{
for (int i = 0; i < tags.Length; i++)
{
Console.WriteLine("{0} : {1} : {2}", tags[i], data[i], (errs[i] == 0));
}
}
}
}
}
}