Can not read OPC tag value from C# OPCSession until i write a value into tag with external software (UaExpert)

Viewed 199

in c#:

using Opc.Ua class

1- I initialize an OPCUAServer:

myOPCUAServer = new OPCUAClass(Properties.Settings.Default.OPCServerAddress, Properties.Settings.Default.OPCServerPort, Properties.Settings.Default.OPCServerApplicationName, TagList, false, 1, Properties.Settings.Default.OPCServerNamespace);

1.1

public OPCUAClass(string serverAddres, string serverport, string serverApplicationName, Dictionary<string, TagClass> taglist, bool sessionrenewalRequired, double sessionRenewalMinutes, string nameSpace)
        {
            ServerAddress = serverAddres;
            ServerPortNumber = serverport;
            ServerApplicationName = serverApplicationName;
            ApplicationName = "AB Servicio cosignas y parametros";
            TagList = taglist;
            SessionRenewalRequired = sessionrenewalRequired;
            SessionRenewalPeriodMins = sessionRenewalMinutes;
            OPCNameSpace = nameSpace;
            LastTimeOPCServerFoundAlive = DateTime.Now;
            InitializeOPCUAClient();
            SubscribeItems();

            if (SessionRenewalRequired)
            {
                LastTimeSessionRenewed = DateTime.Now;
                RenewerThread = new Thread(renewSessionThread);
                RenewerThread.Start();
            }
        }
            

1.2

public void InitializeOPCUAClient()
        {
            var config = new ApplicationConfiguration()
            {
                ApplicationName = ApplicationName,
                ApplicationUri = Utils.Format(@"urn:{0}:" + ApplicationName + "", ServerAddress),
                ApplicationType = ApplicationType.Client,

                SecurityConfiguration = new SecurityConfiguration
                {
                    ApplicationCertificate = new CertificateIdentifier { StoreType = @"Directory", StorePath = @"%ProgramData%\OPC Foundation\CertificateStores\MachineDefault", SubjectName = Utils.Format(@"CN={0}, DC={1}", ApplicationName, ServerAddress) },
                    TrustedIssuerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%ProgramData%\OPC Foundation\CertificateStores\UA Certificate Authorities" },
                    TrustedPeerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%ProgramData%\OPC Foundation\CertificateStores\UA Applications" },
                    RejectedCertificateStore = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%ProgramData%\OPC Foundation\CertificateStores\RejectedCertificates" },
                    AutoAcceptUntrustedCertificates = true,
                    AddAppCertToTrustedStore = true
                },
                DiscoveryServerConfiguration = new DiscoveryServerConfiguration
                {
                    BaseAddresses = { "opc.tcp://localhost:4840" },
                    ServerNames = { "UALocalDiscoveryServer" }
                },
                TransportConfigurations = new TransportConfigurationCollection(),
                TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
                ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 360000 },
                TraceConfiguration = new TraceConfiguration()
            };
            config.Validate(ApplicationType.Client).GetAwaiter().GetResult();
            if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
            {
                config.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); };
            }

            var application = new ApplicationInstance
            {
                ApplicationName = ApplicationName,
                ApplicationType = ApplicationType.Client,
                ApplicationConfiguration = config
            };
            application.CheckApplicationInstanceCertificate(false, 2048).GetAwaiter().GetResult();



            string serverAddress = ServerAddress;
            var selectedEndpoint = CoreClientUtils.SelectEndpoint("opc.tcp://" + serverAddress + ":" + ServerPortNumber + "/" + ServerApplicationName, useSecurity: SecurityEnabled, discoverTimeout: 15000);

            OPCSession = Session.Create(config, new ConfiguredEndpoint(null, selectedEndpoint, EndpointConfiguration.Create(config)), false, "", 60000, null, null).GetAwaiter().GetResult();
            {
                var subscription = new Subscription(OPCSession.DefaultSubscription) { PublishingInterval = 1000 };

                OPCSession.AddSubscription(subscription);
                subscription.Create();
            }
        }

2- I suscribe to tags:

TagList.Clear();

                foreach (string opcTag in tags_opc.Values)
                {
                    TagList.Add(opcTag, new TagClass(opcTag, opcTag));
                }

 myOPCUAServer.TagList = TagList;
 myOPCUAServer.SubscribeItems();

3- I try to read tags after i make sure i am suscribed

MonitoredItem item = OPCSession.Subscriptions.FirstOrDefault()
                .MonitoredItems.Where(mi => mi.StartNodeId.Identifier.ToString().EndsWith(tag_opc)).FirstOrDefault();

            if (item != null)
            {
                ReadValueId nodeToRead = new ReadValueId();
                nodeToRead.NodeId = item.StartNodeId;
                nodeToRead.AttributeId = Attributes.Value;

                ReadValueIdCollection nodesToRead = new ReadValueIdCollection();
                nodesToRead.Add(nodeToRead);

                // read current value.
                DataValueCollection result = null;
                DiagnosticInfoCollection diagnosticInfo = null;

                OPCSession.Read(
                    null,
                    0,
                    TimestampsToReturn.Neither,
                    nodesToRead,
                    out result,
                    out diagnosticInfo);

Note out result

I am using Logix Designer software to access PLC Controller tags, as an OPC Server.

Lets suppose my OPC Server includes:

[003]My_tag.element tag

If i try to read it with C# code:

result[0].StatusCode.ToString() will be BadNodeIdUnknown

Then i use UaExpert software to write 3 into [003]My_tag.element tag

Afterwards, when i read the same tag with C# code:

result[0].StatusCode.ToString() will be Good

So it became readable after writing something into it.

Before anything, writing a value into [003]My_tag.element tag through Logix Designer does not solve anything, so it is straightly related to activating something through external software, and not tag value itself.

Any ideas? Thanks.

0 Answers
Related