Files
Mapo-IOB-WIN/IOB-OPC-UA/Applications/ConsoleReferenceClient/Program.cs
T
2021-05-06 12:16:40 +02:00

140 lines
5.9 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Configuration;
namespace Quickstarts.ConsoleReferenceClient
{
public static class Program
{
#region Private Fields
/// <summary>
/// Elenco degli items da monitorare come risultato del browse iniziale
/// </summary>
private static Dictionary<string, string> selectedItemList = new Dictionary<string, string>();
#endregion Private Fields
#region Public Methods
public static async Task Main(string[] args)
{
IOutput console = new ConsoleOutput();
console.WriteLine("OPC UA Console Reference Client");
try
{
// Define the UA Client application
ApplicationInstance application = new ApplicationInstance();
application.ApplicationName = "Quickstart Console Reference Client";
application.ApplicationType = ApplicationType.Client;
// load the application configuration.
await application.LoadApplicationConfiguration("ConsoleReferenceClient.Config.xml", silent: false);
// check the application certificate.
await application.CheckApplicationInstanceCertificate(silent: false, minimumKeySize: 0);
// create the UA Client object and connect to configured server.
UAClient uaClient = new UAClient(application.ApplicationConfiguration, console, ClientBase.ValidateResponse);
console.WriteLine("Connection Format: opc.tcp://{srvUrl}:{port}");
console.WriteLine("Please enter server url (Enter for default):");
string srvUrl = Console.ReadLine();
srvUrl = string.IsNullOrEmpty(srvUrl) ? "192.168.250.53" : srvUrl;
console.WriteLine("Please enter server port [4840]:");
string srvPort = Console.ReadLine();
srvPort = string.IsNullOrEmpty(srvPort) ? "4840" : srvPort;
uaClient.ServerUrl = $"opc.tcp://{srvUrl}:{srvPort}";
bool connected = await uaClient.ConnectAsync();
if (connected)
{
// Run tests for available methods.
uaClient.ReadNodes();
uaClient.WriteNodes();
console.WriteLine("Please enter NodeId Value (enter for next option) (ex: ns=4,i=5001)");
string fullVal = Console.ReadLine();
if (!string.IsNullOrEmpty(fullVal))
{
selectedItemList = uaClient.Browse(fullVal);
}
else
{
console.WriteLine("Please enter INT Value (ex: 5001 --> i=5001)");
string valIn01 = Console.ReadLine();
console.WriteLine("Please enter Namespace index (ex: 4 --> ns=4)");
string valIn02 = Console.ReadLine();
uint iValue = 5001;
ushort nsVal = 4;
if (!string.IsNullOrEmpty(valIn01))
{
uint.TryParse(valIn01, out iValue);
}
if (!string.IsNullOrEmpty(valIn02))
{
ushort.TryParse(valIn02, out nsVal);
}
selectedItemList = uaClient.Browse(nsVal, iValue);
}
console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
//uaClient.CallMethod();
List<Opc.Ua.Client.MonitoredItem> subscribedItems = uaClient.SubscribeToDataChanges(selectedItemList);
// Wait for some DataChange notifications from MonitoredItems
await Task.Delay(20_000 * 3 * 15);
uaClient.Disconnect();
}
else
{
console.WriteLine("Could not connect to server!");
}
console.WriteLine("\nProgram ended.");
console.WriteLine("Press any key to finish...");
Console.ReadKey();
}
catch (Exception ex)
{
console.WriteLine(ex.Message);
}
}
#endregion Public Methods
}
}