public void OpcUaHelperTest() { //config ApplicationConfiguration l_applicationConfig = new ApplicationConfiguration() { ApplicationName = "OdenwaldUnitTest", ApplicationType = ApplicationType.Client, SecurityConfiguration = new SecurityConfiguration { ApplicationCertificate = new CertificateIdentifier { StoreType = @"Windows", StorePath = @"CurrentUser\My", SubjectName = Utils.Format(@"CN={0}, DC={1}", "Odenwald", System.Net.Dns.GetHostName()) }, TrustedPeerCertificates = new CertificateTrustList { StoreType = @"Windows", StorePath = @"CurrentUser\TrustedPeople", }, NonceLength = 32, AutoAcceptUntrustedCertificates = true }, TransportConfigurations = new TransportConfigurationCollection(), TransportQuotas = new TransportQuotas { OperationTimeout = 15000 }, ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 } }; l_applicationConfig.Validate(ApplicationType.Client); if (l_applicationConfig.SecurityConfiguration.AutoAcceptUntrustedCertificates) { l_applicationConfig.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); }; } Session l_session = Session.Create(l_applicationConfig, new ConfiguredEndpoint(null, new EndpointDescription("opc.tcp://ac-l32-m7-lth1:51210/UA/SampleServer")), true, "OdenwaldUnitTest", 60000, null, null);//EndpointDescription need to be changed according to your OPC server string[] browsePaths = new string[] { "2:Data/2:Dynamic/2:Scalar/2:Int32Value" }; string nodeRelativePath = "Data.Dynamic.Scalar.Int32Value"; var nsCount = l_session.NamespaceUris.Count; string[] relativePaths = { "Objects/Data/Dynamic/Scalar/Int32Value/" }; relativePaths = nodeRelativePath.Split('.'); var nodeList = OpcUaHelper.TranslateBrowsePaths(l_session, ObjectIds.ObjectsFolder, l_session.NamespaceUris, browsePaths); Assert.NotNull(nodeList); Assert.Equal(1, nodeList.Count); }
/// <summary> /// Connects to the OPC server. /// </summary> private async Task <bool> ConnectToOpcServer() { try { OpcUaHelper helper = new OpcUaHelper(appDirs, kpNum, OpcUaHelper.RuntimeKind.View) { CertificateValidation = CertificateValidator_CertificateValidation }; if (await helper.ConnectAsync(deviceConfig.ConnectionOptions)) { opcSession = helper.OpcSession; return(true); } else { opcSession = null; return(false); } } catch (Exception ex) { ScadaUiUtils.ShowError(KpPhrases.ConnectServerError + ":" + Environment.NewLine + ex.Message); return(false); } finally { SetConnButtonsEnabled(); } }
public void TestFindNode() { ApplicationConfiguration l_applicationConfig = new ApplicationConfiguration() { ApplicationName = "OdenwaldUnitTest", ApplicationType = ApplicationType.Client, SecurityConfiguration = new SecurityConfiguration { ApplicationCertificate = new CertificateIdentifier { StoreType = @"Windows", StorePath = @"CurrentUser\My", SubjectName = Utils.Format(@"CN={0}, DC={1}", "Odenwald", System.Net.Dns.GetHostName()) }, TrustedPeerCertificates = new CertificateTrustList { StoreType = @"Windows", StorePath = @"CurrentUser\TrustedPeople", }, NonceLength = 32, AutoAcceptUntrustedCertificates = true }, TransportConfigurations = new TransportConfigurationCollection(), TransportQuotas = new TransportQuotas { OperationTimeout = 15000 }, ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 } }; l_applicationConfig.Validate(ApplicationType.Client); if (l_applicationConfig.SecurityConfiguration.AutoAcceptUntrustedCertificates) { l_applicationConfig.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); }; } Session l_session = Session.Create(l_applicationConfig, new ConfiguredEndpoint(null, new EndpointDescription("opc.tcp://ac-l32-m7-lth1:51210/UA/SampleServer")), true, "OdenwaldUnitTest", 60000, null, null);//EndpointDescription need to be changed according to your OPC server var nodeId = OpcUaHelper.GetReadValueIdCollection("Data.Dynamic.Scalar.Int32Value", Attributes.Value, l_session); Assert.Equal(nodeId[0].NodeId.ToString(), "ns=2;i=10849"); }
/// <summary> /// Connects to the OPC server. /// </summary> private void ConnectToOpcServer() { try { OpcUaHelper helper = new OpcUaHelper(AppDirs, Number, OpcUaHelper.RuntimeKind.Logic) { CertificateValidation = CertificateValidator_CertificateValidation, WriteToLog = WriteToLog }; connected = helper.ConnectAsync(deviceConfig.ConnectionOptions, ReqParams.Timeout).Result; autoAccept = autoAccept || helper.AutoAccept; opcSession = helper.OpcSession; opcSession.KeepAlive += OpcSession_KeepAlive; opcSession.Notification += OpcSession_Notification; } catch (Exception ex) { connected = false; WriteToLog((Localization.UseRussian ? "Ошибка при соединении с OPC-сервером: " : "Error connecting OPC server: ") + ex); } }