Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
            
            //--------here is what i'm trying to add nodes---------
            _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);  //인증서를 check하고 없으면 만들고 등록까지한다.
        }

        private void MinimalServerMainForm_FormClosed
                    (object sender, FormClosedEventArgs e)
        {
            _server.Stop();
        }
    }
}

And this is about addNodes() function.
C#
        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.
Posted
Updated 19-Sep-23 10:55am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900