616 lines
23 KiB
C#
616 lines
23 KiB
C#
//=============================================================================
|
|
// Siemens AG
|
|
// (c)Copyright (2017) All Rights Reserved
|
|
//-----------------------------------------------------------------------------
|
|
// Tested with: Windows 7 Ultimate x64
|
|
// Engineering: Visual Studio 2013
|
|
// Functionality: Wrapps up important classes/methods of the OPC UA .NET Stack to help
|
|
// with simple client implementation
|
|
//-----------------------------------------------------------------------------
|
|
// Change log table:
|
|
// Version Date Expert in charge Changes applied
|
|
// 01.00.00 31.08.2016 (Siemens) First released version
|
|
// 01.01.00 22.02.2017 (Siemens) Implement user authentication, SHA256 Cert, Basic256Rsa256 connection,
|
|
// Basic256Rsa256 connections, read/write structs/UDTs
|
|
//=============================================================================
|
|
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IdentityModel;
|
|
using System.Linq;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Client;
|
|
using Siemens.UAClientHelper;
|
|
|
|
namespace UA_Client_1500
|
|
{
|
|
public partial class UAClientForm : Form
|
|
{
|
|
/// <summary>
|
|
/// Fields
|
|
/// </summary>
|
|
#region Fields
|
|
private Session mySession;
|
|
private Subscription mySubscription;
|
|
private UAClientHelperAPI myClientHelperAPI;
|
|
private EndpointDescription mySelectedEndpoint;
|
|
private MonitoredItem myMonitoredItem;
|
|
private List<String> myRegisteredNodeIdStrings;
|
|
private ReferenceDescriptionCollection referenceDescriptionCollection;
|
|
private List<string[]> myStructList;
|
|
private UAClientCertForm myCertForm;
|
|
private Int16 itemCount;
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Form Construction
|
|
/// </summary>
|
|
#region Construction
|
|
public UAClientForm()
|
|
{
|
|
InitializeComponent();
|
|
myClientHelperAPI = new UAClientHelperAPI();
|
|
myRegisteredNodeIdStrings = new List<String>();
|
|
browsePage.Enabled = false;
|
|
rwPage.Enabled = false;
|
|
subscribePage.Enabled = false;
|
|
structPage.Enabled = false;
|
|
itemCount = 0;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Event handlers called by the UI
|
|
/// </summary>
|
|
#region UserInteractionHandlers
|
|
private void EndpointButton_Click(object sender, EventArgs e)
|
|
{
|
|
endpointListView.Items.Clear();
|
|
//The local discovery URL for the discovery server
|
|
string discoveryUrl = discoveryTextBox.Text;
|
|
try
|
|
{
|
|
ApplicationDescriptionCollection servers = myClientHelperAPI.FindServers(discoveryUrl);
|
|
foreach (ApplicationDescription ad in servers)
|
|
{
|
|
foreach (string url in ad.DiscoveryUrls)
|
|
{
|
|
EndpointDescriptionCollection endpoints = myClientHelperAPI.GetEndpoints(url);
|
|
foreach (EndpointDescription ep in endpoints)
|
|
{
|
|
string securityPolicy = ep.SecurityPolicyUri.Remove(0, 42);
|
|
endpointListView.Items.Add("[" + ad.ApplicationName + "] " + " [" + ep.SecurityMode + "] " + " [" + securityPolicy + "] " + " [" + ep.EndpointUrl + "]").Tag = ep;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
private void SubscribeButton_Click(object sender, EventArgs e)
|
|
{
|
|
//this example only supports one item per subscription; remove the following IF loop to add more items
|
|
if (myMonitoredItem != null)
|
|
{
|
|
try
|
|
{
|
|
myMonitoredItem = myClientHelperAPI.RemoveMonitoredItem(mySubscription, myMonitoredItem);
|
|
}
|
|
catch
|
|
{
|
|
//ignore
|
|
;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
//use different item names for correct assignment at the notificatino event
|
|
itemCount++;
|
|
string monitoredItemName = "myItem" + itemCount.ToString();
|
|
if (mySubscription == null)
|
|
{
|
|
mySubscription = myClientHelperAPI.Subscribe(2000);
|
|
}
|
|
myMonitoredItem = myClientHelperAPI.AddMonitoredItem(mySubscription, subscriptionIdTextBox.Text, monitoredItemName, 1);
|
|
myClientHelperAPI.ItemChangedNotification += new MonitoredItemNotificationEventHandler(Notification_MonitoredItem);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
private void EpConnectButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (mySession != null && !mySession.Disposed)
|
|
{
|
|
try
|
|
{
|
|
mySubscription.Delete(true);
|
|
}
|
|
catch
|
|
{
|
|
;
|
|
}
|
|
|
|
myClientHelperAPI.Disconnect();
|
|
mySession = myClientHelperAPI.Session;
|
|
|
|
ResetUI();
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
myClientHelperAPI.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive);
|
|
myClientHelperAPI.CertificateValidationNotification += new CertificateValidationEventHandler(Notification_ServerCertificate);
|
|
|
|
myClientHelperAPI.Connect(mySelectedEndpoint, userPwButton.Checked, userTextBox.Text, pwTextBox.Text);
|
|
mySession = myClientHelperAPI.Session;
|
|
|
|
epConnectServerButton.Text = "Disconnect from server";
|
|
browsePage.Enabled = true;
|
|
rwPage.Enabled = true;
|
|
subscribePage.Enabled = true;
|
|
structPage.Enabled = true;
|
|
myCertForm = null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
myCertForm = null;
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
|
|
}
|
|
private void WriteValButton_Click(object sender, EventArgs e)
|
|
{
|
|
List<String> values = new List<string>();
|
|
List<String> nodeIdStrings = new List<string>();
|
|
values.Add(writeTextBox.Text);
|
|
nodeIdStrings.Add(writeIdTextBox.Text);
|
|
try
|
|
{
|
|
myClientHelperAPI.WriteValues(values, nodeIdStrings);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
private void UnsubscribeButton_Click(object sender, EventArgs e)
|
|
{
|
|
myClientHelperAPI.RemoveSubscription(mySubscription);
|
|
mySubscription = null;
|
|
itemCount = 0;
|
|
subscriptionLabel.Text = "";
|
|
}
|
|
private void NodeTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
|
|
{
|
|
descriptionGridView.Rows.Clear();
|
|
|
|
try
|
|
{
|
|
ReferenceDescription refDesc = (ReferenceDescription)e.Node.Tag;
|
|
Node node = myClientHelperAPI.ReadNode(refDesc.NodeId.ToString());
|
|
VariableNode variableNode = new VariableNode();
|
|
|
|
string[] row1 = new string[] { "Node Id", refDesc.NodeId.ToString() };
|
|
string[] row2 = new string[] { "Namespace Index", refDesc.NodeId.NamespaceIndex.ToString() };
|
|
string[] row3 = new string[] { "Identifier Type", refDesc.NodeId.IdType.ToString() };
|
|
string[] row4 = new string[] { "Identifier", refDesc.NodeId.Identifier.ToString() };
|
|
string[] row5 = new string[] { "Browse Name", refDesc.BrowseName.ToString() };
|
|
string[] row6 = new string[] { "Display Name", refDesc.DisplayName.ToString() };
|
|
string[] row7 = new string[] { "Node Class", refDesc.NodeClass.ToString() };
|
|
string[] row8 = new string[] { "Description", "null" };
|
|
try { row8 = new string[] { "Description", node.Description.ToString() }; }
|
|
catch { row8 = new string[] { "Description", "null" }; }
|
|
string[] row9 = new string[] { "Type Definition", refDesc.TypeDefinition.ToString() };
|
|
string[] row10 = new string[] { "Write Mask", node.WriteMask.ToString() };
|
|
string[] row11 = new string[] { "User Write Mask", node.UserWriteMask.ToString() };
|
|
if (node.NodeClass == NodeClass.Variable)
|
|
{
|
|
variableNode = (VariableNode)node.DataLock;
|
|
List<NodeId> nodeIds = new List<NodeId>();
|
|
List<string> displayNames = new List<string>();
|
|
List<ServiceResult> errors = new List<ServiceResult>();
|
|
NodeId nodeId = new NodeId(variableNode.DataType);
|
|
nodeIds.Add(nodeId);
|
|
mySession.ReadDisplayName(nodeIds, out displayNames, out errors);
|
|
|
|
string[] row12 = new string[] { "Data Type", displayNames[0] };
|
|
string[] row13 = new string[] { "Value Rank", variableNode.ValueRank.ToString() };
|
|
string[] row14 = new string[] { "Array Dimensions", variableNode.ArrayDimensions.Capacity.ToString() };
|
|
string[] row15 = new string[] { "Access Level", variableNode.AccessLevel.ToString() };
|
|
string[] row16 = new string[] { "Minimum Sampling Interval", variableNode.MinimumSamplingInterval.ToString() };
|
|
string[] row17 = new string[] { "Historizing", variableNode.Historizing.ToString() };
|
|
|
|
object[] rows = new object[] { row1, row2, row3, row4, row5, row6, row7, row8, row9, row10, row11, row12, row13, row14, row15, row16, row17 };
|
|
foreach (string[] rowArray in rows)
|
|
{
|
|
descriptionGridView.Rows.Add(rowArray);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
object[] rows = new object[] { row1, row2, row3, row4, row5, row6, row7, row8, row9, row10, row11 };
|
|
foreach (string[] rowArray in rows)
|
|
{
|
|
descriptionGridView.Rows.Add(rowArray);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
|
|
}
|
|
private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
myClientHelperAPI.Disconnect();
|
|
}
|
|
catch
|
|
{
|
|
;
|
|
}
|
|
}
|
|
private void NodeTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
|
|
{
|
|
e.Node.Nodes.Clear();
|
|
|
|
ReferenceDescriptionCollection referenceDescriptionCollection;
|
|
ReferenceDescription refDesc = (ReferenceDescription)e.Node.Tag;
|
|
|
|
try
|
|
{
|
|
referenceDescriptionCollection = myClientHelperAPI.BrowseNode(refDesc);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
return;
|
|
}
|
|
|
|
foreach (ReferenceDescription tempRefDesc in referenceDescriptionCollection)
|
|
{
|
|
e.Node.Nodes.Add(tempRefDesc.DisplayName.ToString()).Tag = tempRefDesc;
|
|
}
|
|
foreach (TreeNode node in e.Node.Nodes)
|
|
{
|
|
node.Nodes.Add("");
|
|
}
|
|
}
|
|
private void BrowsePage_Enter(object sender, EventArgs e)
|
|
{
|
|
if (referenceDescriptionCollection == null)
|
|
{
|
|
try
|
|
{
|
|
referenceDescriptionCollection = myClientHelperAPI.BrowseRoot();
|
|
foreach (ReferenceDescription refDesc in referenceDescriptionCollection)
|
|
{
|
|
nodeTreeView.Nodes.Add(refDesc.DisplayName.ToString()).Tag = refDesc;
|
|
foreach (TreeNode node in nodeTreeView.Nodes)
|
|
{
|
|
node.Nodes.Add("");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
}
|
|
private void ReadValButton_Click(object sender, EventArgs e)
|
|
{
|
|
List<String> nodeIdStrings = new List<String>();
|
|
List<String> values = new List<String>();
|
|
nodeIdStrings.Add(readIdTextBox.Text);
|
|
try
|
|
{
|
|
values = myClientHelperAPI.ReadValues(nodeIdStrings);
|
|
readTextBox.Text = values.ElementAt<String>(0);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
|
|
}
|
|
private void EndpointListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
|
|
{
|
|
mySelectedEndpoint = (EndpointDescription)e.Item.Tag;
|
|
}
|
|
private void OpcTabControl_Selecting(object sender, TabControlCancelEventArgs e)
|
|
{
|
|
e.Cancel = !e.TabPage.Enabled;
|
|
if (!e.TabPage.Enabled)
|
|
{
|
|
MessageBox.Show("Establish a connection to a server first.", "Error");
|
|
}
|
|
}
|
|
private void RegisterButton_Click(object sender, EventArgs e)
|
|
{
|
|
List<String> nodeIdStrings = new List<String>();
|
|
nodeIdStrings.Add(rgNodeIdTextBox.Text);
|
|
try
|
|
{
|
|
myRegisteredNodeIdStrings = myClientHelperAPI.RegisterNodeIds(nodeIdStrings);
|
|
regNodeIdTextBox.Text = myRegisteredNodeIdStrings.ElementAt<String>(0);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
|
|
}
|
|
private void UnregisterButton_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
myClientHelperAPI.UnregisterNodeIds(myRegisteredNodeIdStrings);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
myRegisteredNodeIdStrings.Clear();
|
|
regNodeIdTextBox.Text = "";
|
|
rgReadTextBox.Text = "";
|
|
rgWriteTextBox.Text = "";
|
|
}
|
|
private void RgReadButton_Click(object sender, EventArgs e)
|
|
{
|
|
List<String> values = new List<String>();
|
|
try
|
|
{
|
|
values = myClientHelperAPI.ReadValues(myRegisteredNodeIdStrings);
|
|
rgReadTextBox.Text = values.ElementAt<String>(0);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
private void RgWriteButton_Click(object sender, EventArgs e)
|
|
{
|
|
List<String> values = new List<string>();
|
|
values.Add(rgWriteTextBox.Text);
|
|
try
|
|
{
|
|
myClientHelperAPI.WriteValues(values, myRegisteredNodeIdStrings);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
private void UserPwButton_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
if (userPwButton.Checked)
|
|
{
|
|
userTextBox.Enabled = true;
|
|
pwTextBox.Enabled = true;
|
|
}
|
|
}
|
|
private void UserAnonButton_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
if (userAnonButton.Checked)
|
|
{
|
|
userTextBox.Enabled = false;
|
|
pwTextBox.Enabled = false;
|
|
}
|
|
}
|
|
private void StructReadButton_Click(object sender, EventArgs e)
|
|
{
|
|
structGridView.Rows.Clear();
|
|
myStructList = new List<string[]>();
|
|
|
|
try
|
|
{
|
|
myStructList = myClientHelperAPI.ReadStructUdt(structNodeIdTextBox.Text);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
|
|
foreach (string[] val in myStructList)
|
|
{
|
|
string[] row = new string[] { val[0], val[1], val[2] };
|
|
structGridView.Rows.Add(row);
|
|
}
|
|
}
|
|
private void StructWriteButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (structGridView.Rows.Count == 0)
|
|
{
|
|
MessageBox.Show("Read a struct/UDT first.","Error");
|
|
return;
|
|
}
|
|
|
|
//Clear the list and refill with values from GridView to get value changes
|
|
myStructList.Clear();
|
|
foreach (DataGridViewRow row in structGridView.Rows )
|
|
{
|
|
string[] tempString = new String[3];
|
|
try
|
|
{
|
|
tempString[0] = structGridView.Rows[row.Index].Cells[0].Value.ToString();
|
|
}
|
|
catch
|
|
{
|
|
tempString[0] = "";
|
|
}
|
|
|
|
try
|
|
{
|
|
tempString[1] = structGridView.Rows[row.Index].Cells[1].Value.ToString();
|
|
}
|
|
catch
|
|
{
|
|
tempString[1] = "";
|
|
}
|
|
|
|
try
|
|
{
|
|
tempString[2] = structGridView.Rows[row.Index].Cells[2].Value.ToString();
|
|
}
|
|
catch
|
|
{
|
|
tempString[2] = "";
|
|
}
|
|
|
|
myStructList.Add(tempString);
|
|
}
|
|
|
|
try
|
|
{
|
|
myClientHelperAPI.WriteStructUdt(structNodeIdTextBox.Text, myStructList);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Global OPC UA event handlers
|
|
/// </summary>
|
|
#region OpcEventHandlers
|
|
private void Notification_ServerCertificate(CertificateValidator cert, CertificateValidationEventArgs e)
|
|
{
|
|
//Handle certificate here
|
|
//To accept a certificate manually move it to the root folder (Start > mmc.exe > add snap-in > certificates)
|
|
//Or handle via UAClientCertForm
|
|
|
|
if (this.InvokeRequired)
|
|
{
|
|
this.BeginInvoke(new CertificateValidationEventHandler(Notification_ServerCertificate), cert, e);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
//Search for the server's certificate in store; if found -> accept
|
|
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
|
|
store.Open(OpenFlags.ReadOnly);
|
|
X509CertificateCollection certCol = store.Certificates.Find(X509FindType.FindByThumbprint, e.Certificate.Thumbprint, true);
|
|
store.Close();
|
|
if (certCol.Capacity > 0)
|
|
{
|
|
e.Accept = true;
|
|
}
|
|
//Show cert dialog if cert hasn't been accepted yet
|
|
else
|
|
{
|
|
if (!e.Accept)
|
|
{
|
|
myCertForm = new UAClientCertForm(e);
|
|
myCertForm.ShowDialog();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
;
|
|
}
|
|
}
|
|
private void Notification_MonitoredItem(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
|
|
{
|
|
if (this.InvokeRequired)
|
|
{
|
|
this.BeginInvoke(new MonitoredItemNotificationEventHandler(Notification_MonitoredItem), monitoredItem, e);
|
|
return;
|
|
}
|
|
MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
|
|
if (notification == null)
|
|
{
|
|
return;
|
|
}
|
|
subscriptionLabel.Text = "Item Name: " + monitoredItem.DisplayName +
|
|
"\nValue: " + Utils.Format("{0}", notification.Value.WrappedValue.ToString()) +
|
|
"\nStatus Code: " + Utils.Format("{0}", notification.Value.StatusCode.ToString()) +
|
|
"\nSource timestamp: " + notification.Value.SourceTimestamp.ToString() +
|
|
"\nServer timestamp: " + notification.Value.ServerTimestamp.ToString();
|
|
}
|
|
private void Notification_KeepAlive(Session sender, KeepAliveEventArgs e)
|
|
{
|
|
if (this.InvokeRequired)
|
|
{
|
|
this.BeginInvoke(new KeepAliveEventHandler(Notification_KeepAlive), sender, e);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
// check for events from discarded sessions.
|
|
if (!Object.ReferenceEquals(sender, mySession))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// check for disconnected session.
|
|
if (ServiceResult.IsBad(e.Status))
|
|
{
|
|
// try reconnecting using the existing session state
|
|
mySession.Reconnect();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error");
|
|
ResetUI();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Private methods for UI handling
|
|
/// </summary>
|
|
#region PrivateMethods
|
|
private void ResetUI()
|
|
{
|
|
descriptionGridView.Rows.Clear();
|
|
nodeTreeView.Nodes.Clear();
|
|
referenceDescriptionCollection = null;
|
|
structGridView.Rows.Clear();
|
|
myStructList = null;
|
|
|
|
subscriptionLabel.Text = "";
|
|
subscriptionIdTextBox.Text = "";
|
|
readIdTextBox.Text = "";
|
|
writeIdTextBox.Text = "";
|
|
readTextBox.Text = "";
|
|
writeTextBox.Text = "";
|
|
rgReadTextBox.Text = "";
|
|
rgWriteTextBox.Text = "";
|
|
rgNodeIdTextBox.Text = "";
|
|
regNodeIdTextBox.Text = "";
|
|
epConnectServerButton.Text = "Connect to server";
|
|
|
|
browsePage.Enabled = false;
|
|
rwPage.Enabled = false;
|
|
subscribePage.Enabled = false;
|
|
structPage.Enabled = false;
|
|
|
|
opcTabControl.SelectedIndex = 0;
|
|
}
|
|
#endregion
|
|
}
|
|
} |