Write OPC Items
The OASData.Data class allows you to write values directly to OPC Items, both synchronously and asynchronously. These calls are sent to the OPC server located at the network node provided in the call.
Step 1: Basic Application
Your application typically does not need more than one instance of the OASData.Data class. So, this can be created when your application starts.
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: Write OPC Items Asynchronously
Using the WriteOPCItemsWithoutResults method, you can write values to one or more OPC Items in a single call, without blocking the operation of your application.
The first parameter of the method call is the network node. Passing a null or empty string defaults to "localhost".
VB
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 OPC Items and values to write ascynchronously in the form:")
Console.WriteLine("<OPCItem> <value> <OPCItem> <value> ...")
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 items As String() = raw_cmd.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
Dim errs As Integer() = New Integer(items.Length - 1) {}
Dim opcnames As List(Of String) = New List(Of String)()
Dim values As List(Of Object) = New List(Of Object)()
For i As Integer = 0 To items.Length - 1 Step 2
If i + 1 >= items.Length Then Continue For
opcnames.Add(items(i))
values.Add(items(i + 1))
Next
If opcnames.Count > 0 Then
oasd.WriteOPCItemsWithoutResults("", opcnames.ToArray(), values.ToArray())
End If
End While
End Sub
End Class
End Namespace
C#
namespace OASDataSample
{
class Program
{
static OASData.Data oasd = new OASData.Data();
static void Main(string[] args)
{
Console.WriteLine("Type one or more OPC Items and values to write ascynchronously in the form:");
Console.WriteLine("<OPCItem> <value> <OPCItem> <value> ...");
Console.WriteLine("Type ENTER to quit.");
while (true)
{
string raw_cmd = Console.ReadLine();
if (string.IsNullOrWhiteSpace(raw_cmd)) { break; }
string[] items = raw_cmd.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int[] errs = new int[items.Length];
List<string> opcnames = new List<string>();
List<object> values = new List<object>();
for(int i = 0; i < items.Length; i+=2)
{
if (i + 1 >= items.Length) continue;
opcnames.Add(items[i]);
values.Add(items[i + 1]);
}
if (opcnames.Count > 0)
{
oasd.WriteOPCItemsWithoutResults("", opcnames.ToArray(), values.ToArray());
}
}
}
}
}
Step 3: Write OPC Items Synchronously
If you prefer to block your application until the write operation completes, use the WriteOPCItems method.
The possible return values for each OPC Item submitted:
- 0 : no error
- 1 : error writing the OPC Item
- 2 : mismatched Tags and Values arrays passed in
VB
Dim results As Integer() = oasd.WriteOPCItems("", opcnames.ToArray(), values.ToArray())
For j As Integer = 0 To opcnames.Count - 1
Console.WriteLine("{0} : {1}", opcnames(j), results(j))
Next
C#
int[] results = oasd.WriteOPCItems("", opcnames.ToArray(), values.ToArray());
for (int j = 0; j < opcnames.Count; j++)
{
Console.WriteLine("{0} : {1}", opcnames[j], results[j]);
}