上位机智能通信统一解决方案OPC应用

该文介绍了一个使用OPCUA协议进行上位机应用开发的例子,具体是与西门子1500PLC的通信。代码示例展示了如何创建配置客户端、设置证书验证策略,以及如何进行节点浏览和数据读取操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上位机应用开发中的通信需求

通信过程/通信协议多样性

通信统一化处理方案:OPCOpen Platform Communications)、OPC UAOPC Unified Architecture

基于西门子1500PLCOPC服务器对接

 internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            OpcUa();

            Console.ReadLine();
        }

        private static async void OpcUa()
        {
            //ApplicationConfiguration configuration,
            //ConfiguredEndpoint endpoint,
            //bool updateBeforeConnect,
            //string sessionName,
            //uint sessionTimeout,
            //IUserIdentity identity,
            //IList<string> preferredLocales

            // 准备第一个参数
            ApplicationConfiguration ac = new ApplicationConfiguration();
            ac.ClientConfiguration = new ClientConfiguration();

            CertificateValidator cv = new CertificateValidator
            {
                RejectSHA1SignedCertificates = false,
            };
            cv.CertificateValidation += (se, ev) =>
            {
                if (ev.Error.StatusCode.Code == StatusCodes.BadCertificateUntrusted)
                {
                    ev.Accept = true;
                }
            };
            ac.CertificateValidator = cv;

            // 准备第二个参数
            EndpointDescription desc = CoreClientUtils.SelectEndpoint(
                discoveryUrl: "opc.tcp://OPCSERVER:49320",
                false
                );
            ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, desc);

            Session session = await Session.Create(
                ac,
                endpoint,
                false,
                "PLC1500_opc",
                60000,
                new UserIdentity("admin", "admin"),
                new string[] { }
                );

            // 简单   用户名密码
            // 证书   服务器  任意证书自动接收    服务器颁发证书





            // 浏览节点
            //Browser browser = new Browser(session);
            //var collection = browser.Browse(new NodeId("ns=2;s=通道 1.设备 1"));
            //foreach (var node in collection)
            //{

            //}




            // 读取
            //RequestHeader requestHeader,
            //double maxAge,
            //TimestampsToReturn timestampsToReturn,
            //ReadValueIdCollection nodesToRead,
            //out DataValueCollection results,
            //out DiagnosticInfoCollection diagnosticInfos
            ReadValueIdCollection readValueIds = new ReadValueIdCollection();

            ReadValueId id = new ReadValueId();
            id.NodeId = "ns=2;s=数据类型示例.16 位设备.R 寄存器.Word1";
            id.AttributeId = Attributes.Value;
            readValueIds.Add(id);

            id = new ReadValueId();
            id.NodeId = "ns=2;s=数据类型示例.16 位设备.R 寄存器.Word2";
            id.AttributeId = Attributes.Value;
            readValueIds.Add(id);

            id = new ReadValueId();
            id.NodeId = "ns=2;s=数据类型示例.16 位设备.R 寄存器.WordArray";
            id.AttributeId = Attributes.Value;
            readValueIds.Add(id);

            session.Read(
                new RequestHeader(),
                0,
                TimestampsToReturn.Both,
                readValueIds,
                out DataValueCollection results,
                out DiagnosticInfoCollection diagnosticInfos
                );

            foreach (var value in results)
            {
                if (value.WrappedValue.TypeInfo.ValueRank == -1)//单值
                    Console.WriteLine(value);
                else if (value.WrappedValue.TypeInfo.ValueRank == 1)
                {
                    foreach (var v in (ushort[])value.WrappedValue.Value)
                    {
                        Console.WriteLine(v);
                    }
                }
            }

        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

meslog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值