I want to add nodes to my OPC UA server.
Here is my
code link.
I can generate opc ua server % client simply with this code, but can't add nodes to server. There are only basic nodes that are provided by OPC UA Foundation.
This is a server code.
using System.Windows.Forms;
using Opc.Ua;
using Opc.Ua.Server;
using Opc.Ua.Configuration;
using System.Collections.Generic;
using System;
using System.Xml.Linq;
using Opc.Ua.Export;
using System.Runtime.Remoting.Contexts;
namespace MinimalServer
{
public partial class MinimalServerMainForm : Form
{
private ApplicationConfiguration _config;
StandardServer _server;
public MinimalServerMainForm()
{
InitializeComponent();
_config = CreateOpcUaAppConfiguration();
CheckApplicationInstanceCertificate(_config);
_server = new StandardServer();
_server.Start(_config);
_server.AddNodes();
UrlCB.Items.Clear();
foreach (EndpointDescription endpoint in _server.GetEndpoints())
{
if (UrlCB.FindStringExact(endpoint.EndpointUrl) == -1)
{
UrlCB.Items.Add(endpoint.EndpointUrl);
}
}
if (UrlCB.Items.Count > 0)
{
UrlCB.SelectedIndex = 0;
}
}
private ApplicationConfiguration CreateOpcUaAppConfiguration()
{
var config = new ApplicationConfiguration()
{
ApplicationName = "MinimalServer",
ApplicationType = ApplicationType.Server,
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier()
{
StoreType = @"Directory",
StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\MachineDefault",
SubjectName = "CN=MinimalServer, DC=" + System.Net.Dns.GetHostName()
},
TrustedPeerCertificates = new CertificateTrustList
{
StoreType = @"Directory",
StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Applications"
},
NonceLength = 32,
AutoAcceptUntrustedCertificates = true
},
ServerConfiguration = new ServerConfiguration()
{
SecurityPolicies =
{
new ServerSecurityPolicy()
{
SecurityMode = MessageSecurityMode.None,
SecurityPolicyUri = SecurityPolicies.None,
SecurityLevel = 0
}
},
BaseAddresses = new StringCollection()
{
"opc.tcp://localhost:51210/UA/MinimalServer"
}
},
Extensions = new XmlElementCollection()
};
config.Validate(ApplicationType.Server);
if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
{
config.CertificateValidator.CertificateValidation += (s, e) =>
{
e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted);
};
}
return config;
}
private void CheckApplicationInstanceCertificate(ApplicationConfiguration config)
{
ApplicationInstance application = new ApplicationInstance();
application.ApplicationConfiguration = config;
application.CheckApplicationInstanceCertificate(false, 2048);
}
private void MinimalServerMainForm_FormClosed
(object sender, FormClosedEventArgs e)
{
_server.Stop();
}
}
}
And this is about
addNodes()
function.
public virtual ResponseHeader AddNodes
(RequestHeader requestHeader, AddNodesItemCollection nodesToAdd,
out AddNodesResultCollection results, out DiagnosticInfoCollection diagnosticInfos)
{
results = null;
diagnosticInfos = null;
ValidateRequest(requestHeader);
return CreateResponse(requestHeader, 2148204544u);
}
I want to add my own data node like "
hello world
", etc.
Does anyone know how to add nodes to opc ua server?
What I have tried:
I tried to use
addNodes()
function with its declaration by making sample factors for function. But the structure was not matching with its purpose.