Write Tag Data
The OASData.Data class allows you to write values directly to Tags, both synchronously and asynchronously. The WriteTags method is the most efficient way to write to tags. When writing to tags you can optionally include the timestamp to set for each tag you write to.
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: Write Tags Asynchronously
In this example, we use the WriteTags method, which writes multiple tag values in a single call. So we construct arrays of Tag strings and values and pass them in with one call.
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() {"Pump.Value", "Write Float.Value", "Write String.Value"}
Dim values = New Object() {True, 1.234, "Message to write."}
oasd.WriteTags(tags, values)
' Optionally include an array of timestamps to set the timestamp of each tag.
' oasd.WriteTags(tags, values, timestamps)
Console.WriteLine("Write to Tags is Completed.")
Console.ReadKey()
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)
{
var tags = new string[] {"Pump.Value", "Write Float.Value", "Write String.Value"};
var values = new[] {true, 1.234, "Message to write."};
oasd.WriteTags(tags, values);
// Optionally include an array of timestamps to set the timestamp of each tag.
// oasd.WriteTags(tags, values, timestamps);
Console.WriteLine("Write to Tags is Completed.");
Console.ReadKey();
}
}
}
To Write to a single tag use the WriteTag method.
Step 2: Write Tags Synchronously
Both the WriteTag and WriteTags methods write Tag values to the OAS server asynchronously. At times, you may prefer to use a blocking call to wait until the operation completes before proceeding. For this, use the SyncWriteTags or SyncWriteTagsWithConfirmation methods, with the latter providing more detail on the result of the operation.
Below demostrates calling SyncWriteTags. The possible return values for each Tag submitted:
- 0: no error
- 1: service not reachable
- 2: mismatched Tags and Values arrays passed in
VB
Dim results As Integer() = oasd.SyncWriteTags(tagnames.ToArray(), tagvalues.ToArray())
For j As Integer = 0 To tagnames.Count - 1
Console.WriteLine("{0} : {1}", tagnames(j), results(j))
Next
C#
int[] results = oasd.SyncWriteTags(tagnames.ToArray(), tagvalues.ToArray());
for(int j = 0; j < tagnames.Count; j++)
{
Console.WriteLine("{0} : {1}", tagnames[j], results[j]);
}
Below demonstrates calling SyncWriteTagsWithConfirmation. The possible return values for each Tag submitted:
- 0: no error
- 1: service not reachable
- 2: value could not be set within the deadband for Double and Single values within the timeout period
- 3: mismatched Tags and Values arrays passed in
VB
Dim results As Integer() = oasd.SyncWriteTagsWithConfirmation(tagnames.ToArray(), tagvalues.ToArray())
For j As Integer = 0 To tagnames.Count - 1
Console.WriteLine("{0} : {1}", tagnames(j), results(j))
Next
C#
int[] results = oasd.SyncWriteTagsWithConfirmation(tagnames.ToArray(), tagvalues.ToArray());
for(int j = 0; j < tagnames.Count; j++)
{
Console.WriteLine("{0} : {1}", tagnames[j], results[j]);
}