Read OPC Items Synchronously
If you need to read data directly from an OPC source without mapping to an OAS Tag, the OASData.Data.ReadOPCItems method. This is a synchronous call, much like the SyncReadTags method. With this operation you can request current values for one or more Tags directly.
Step 1: The Basic Application
Setup is identical to the sample demonstrating how to read tags synchronously, using a single static instance of the OASData.Data class.
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 ReadOPCItems method which returns an array of values matching the length and order of the array of Tags we pass in. The first parameter is the network node of the OAS server we're connecting to. If this is left blank or null, localhost is assumed. We also include an errors by reference to 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.
Unlike when we address OAS Tags, you'll be referencing the OPC item path. For example, if you want to pull the value of the simulated Sine wave from the local simulator included with the OAS Platform, use the following:
EEI.OPCSimulator\SimDevice.Sine
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 OPC Items 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.ReadOPCItems(Nothing, 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 OPC Items 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.ReadOPCItems(null ,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));
}
}
}
}
}
}