云计算百科
云计算领域专业知识百科平台

c#上位机操作 opc ua 服务器实例并调试成功

一、操作类 using Opc.Ua; using Opc.Ua.Client; using System.Diagnostics; namespace opcua {
    /// <summary>     ///  OPC UA 服务器(以prosys为例)操作类 2026.2     ///  c# Opc.Ua.Client链接OPC UA 服务器     ///  写入数值,写入bytes,写入object     ///  读取数组,读取bytes ,读取objec     ///  author:ljc     /// </summary>     public class OpcUa     {

        private static Session opcSession = null;

        #region 链接关闭链接

        //链接链接         public static async Task<Session> OpcConn(string url)         {
            try             {
                // 1. 配置客户端连接参数                 var endpointUrl = url;                 //var endpoint = CoreClientUtils.SelectEndpoint(endpointUrl, useSecurity: false);

                // 创建配置对象                 var config = new Opc.Ua.ApplicationConfiguration                 {
                    ApplicationName = \”OpcUaClientTest\”,                     ApplicationType = ApplicationType.Client,                     SecurityConfiguration = new SecurityConfiguration                     {
                        // 禁用安全验证(测试环境使用,生产环境需配置证书)                         AutoAcceptUntrustedCertificates = true,                         //RejectSHA1SignedCertificates = false,                         //MinimumCertificateKeySize = 1024                     },                     TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },                     ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }                 };                 // 验证配置                 config.ValidateAsync(ApplicationType.Client);                 var endpoint = CoreClientUtils.SelectEndpoint(config, endpointUrl, false);                 // 2. 创建并连接 Session                 Console.WriteLine(\”正在连接 OPC UA 服务器…\”);

                opcSession = await Session.Create(                     config,                     new ConfiguredEndpoint(null, endpoint, EndpointConfiguration.Create(config)),                     false, // 是否重连                     \”OpcUaDemoSession\”, // 会话名称                     60000, // 会话超时时间(毫秒)                     new UserIdentity(new AnonymousIdentityToken()), // 匿名登录(如需认证可替换为UserPasswordIdentityToken)                     null);                 Trace.WriteLine($\”Session 创建成功,SessionId: {opcSession.SessionId}\”);

            }             catch (Exception ex)             {
                Trace.WriteLine($\”操作异常: {ex.Message}\”);             }             finally             {

            }             return opcSession;

        }           //关闭链接         public static void CloseConn(Session opcSession)         {
            // 5. 关闭并释放 Session             if (opcSession != null)             {
                Trace.WriteLine(\”正在关闭 Session…\”);   &nbs

赞(0)
未经允许不得转载:网硕互联帮助中心 » c#上位机操作 opc ua 服务器实例并调试成功
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!