//----------------------------------------------------------------
// EasyUA Client Sample Application
// ================================
// This Windows Console application uses the EasyUA methods to read/write data from an OPC UA server.
//
// Copyright 2017 Advosol Inc.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Advosol.EasyUA;
using Opc.Ua;
namespace ConsoleAppSample
{
class Program
{
static string appName = "EasyUA Sample";
static string testServer = "opc.tcp://localhost:62849/Advosol/uaPLUSsim/discovery";
static string readNode = "ns=2;s=SimulatedData.Ramp";
static string writeNode = "ns=2;s=Static.Simple Types.Int";
//---------------------------------------------------------------
// The application connects to the UA server and creates a subscritpion for one item.
// Data change notifications display the value and write the value to another item in the same server.
static void Main(string[] args)
{
try
{
UaClient.UaAppConfigFileAutoCreate = appName; // auto create a default UA config file
uaApp = new UaClient(null);
uaApp.LoadConfiguration();
session = uaApp.CreateSession(testServer, false, "test");
//session.StatusChange += new OnStatusChange(onSessionStatusChange);
session.Connect(null);
writeNodeInfo = session.ReadNode(writeNode); // read the node details to get the datatype
//--------------- add Subscription and MonitoredItem
session.SubscriptionRequestKeepaliveCount = 5;
subscr = session.AddSubscription("s1", 500);
//subscr.DataChangeCallback += onSubscrDataChangeNotification; // notification on subscription level
miRamp = subscr.AddItem(readNode);
miRamp.Notification += new OnMonitoredItemNotification(miRamp_Notification); // notification for this MonitoredItem
subscr.ApplyChanges(); // create in the server
Console.WriteLine("UA server access working. Type RETURN to terminate.");
Console.ReadLine();
subscr.Dispose();
session.Close();
session.Dispose();
//---------------- exit
}
catch (Exception ex)
{
Console.WriteLine("Accessing server failed: " + ex.Message);
Console.ReadLine();
return; // exit
}
}
static UaClient uaApp;
static Session session = null;
static Subscription subscr = null;
static MonitoredItem miRamp = null;
static Node writeNodeInfo;
//----------------------------------------------------------------------------------
// Handler for data change notifications from MonitoredItems.
// The method handles the data only if the handling on MonitoredItem level is selected.
// The different handlers are implemented to demonstrate the handling options.
static void miRamp_Notification(MonitoredItem monitoredItem, IEncodeable notification)
{
try
{
MonitoredItemNotification dataChange = notification as MonitoredItemNotification;
if (dataChange != null)
{
object val = dataChange.Value.Value; // the changed value of the subscribed MonitoredItem
WriteValue wval = session.MakeWriteValue((VariableNode)writeNodeInfo, val); // object with NodeId and type converted value
session.WriteAttribute(wval); // write value to another node
Console.Write(val.ToString() + " ");
}
}
catch (Exception ex)
{
Console.WriteLine(monitoredItem.DisplayName + ": " + ex.Message);
}
}
}
}