【OPC UA】使用C#读取OPC UA电液控数据

这篇具有很好参考价值的文章主要介绍了【OPC UA】使用C#读取OPC UA电液控数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


前言

OPC UA与OPC DA协议常见于工业生产中使用,例如煤矿的综采支架电液控系统。OPC UA 是OPC 的后继标准,只是后面增加了UA ,意指”统一架构”(Unified Architecture).它的主要目的是摆脱windows! 实现与平台无关的OPC.从OPC 演进到OPC UA,它的目的并没有改变,依然是为了实现分布式控制系统中的分布式对象技术.但是它的方式变成了与平台无关.面向了开放系统.这也就意味着我们可以在一个Arm /linux平台上实现OPC 的server,或者在云端linux平台上实现Client 程序.

一、读取数据前的确认事项

目前,大部分OPC UA协议都会使用Kepserver EX6作为服务器端,当我们作为客户端去读取服务器端的数据时,必须确保服务器与客户端配置的匹配。
1: 服务器端与客户端必须保持网络通畅,这是最基本的要求。
2:确认服务器的IP地址与开放的端口号。
3:确认服务器端的安全策略,是否需要密码等。

二、具体步骤

1.与服务器连接

代码如下(C#为例):

try
            {
                // Create the configuration.     
                ApplicationConfiguration configuration = Helpers.CreateClientConfiguration();
                // Create the endpoint configuration (use the application configuration to provide default values).
                EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(configuration);
                // The default timeout for a requests sent using the channel.
                endpointConfiguration.OperationTimeout = 300000;
                // Use the pure binary encoding on the wire.
                endpointConfiguration.UseBinaryEncoding = true;
                // Create the endpoint.
                ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
                // Create the binding factory.
                BindingFactory bindingFactory = BindingFactory.Create(configuration);
                X509Certificate2 clientCertificate = configuration.SecurityConfiguration.ApplicationCertificate.Find();
                // Set up a callback to handle certificate validation errors.
                configuration.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
                // Initialize the channel which will be created with the server.
                SessionChannel channel = SessionChannel.Create(
                    configuration,
                    endpointDescription,
                    endpointConfiguration,
                    bindingFactory,
                    clientCertificate,
                    null);
                // Wrap the channel with the session object.
                // This call will fail if the server does not trust the client certificate.
                m_Session = new Session(channel, configuration, endpoint);
                m_Session.ReturnDiagnostics = DiagnosticsMasks.All;
                // Register keep alive callback.
                m_Session.KeepAlive += new KeepAliveEventHandler(Session_KeepAlive);
                UserIdentity identity = new UserIdentity();
                // Create the session. This actually connects to the server.
                // Passing null for the user identity will create an anonymous session.
                m_Session.Open("OPC UA client", identity);
                return m_Session;
            }
            catch (Exception e)
            {
                throw e;
            }

2.读取所有节点

代码如下(示例):

public ReferenceDescriptionCollection BrowseNodes(NodeId sourceId)
        {
            m_Session = this.MySession;
            if (m_Session == null){return null;}
            // fetch references from the server.
            // find all of the components of the node.
            BrowseDescription nodeToBrowse1 = new BrowseDescription();

            nodeToBrowse1.NodeId = sourceId;
            nodeToBrowse1.BrowseDirection = BrowseDirection.Forward;
            //nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.Aggregates;
            nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.HasChild;
            nodeToBrowse1.IncludeSubtypes = false;
            //nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable);
            nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Variable);
            nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All;
            Browser bro = new Browser(m_Session);
            ReferenceDescriptionCollection references = bro.Browse(sourceId);
            return references;
        }

获取指定节点下的引用

private ReferenceDescriptionCollection getTargetItemList(ReferenceDescriptionCollection references, string targetStr)
        {
            ReferenceDescriptionCollection retrunRef = new ReferenceDescriptionCollection();
            foreach (ReferenceDescription rf in references)
            {
                if (!targetStr.Equals("item"))
                {
                    if (!rf.DisplayName.ToString().StartsWith(@"_") && targetStr.Equals(rf.DisplayName.ToString()) && rf.BrowseName.ToString() != "FolderType")
                    {
                        retrunRef = BrowseNodes((NodeId)rf.NodeId);
                        break;
                    }
                }
                else
                {
                    if (!rf.DisplayName.ToString().StartsWith(@"_") && rf.DisplayName.ToString() != "FolderType")
                    {
                        retrunRef.Add(rf);
                    }
                }
            }
            return retrunRef;
        }

读取数据

public ResponseHeader ReadValues(
            NodeIdCollection nodesToRead,
            out DataValueCollection results)
        {
            ResponseHeader response = null;
            results = null;
            DiagnosticInfoCollection diagnosticInfos;
            // build list of attributes to read.
            ReadValueIdCollection valueIdsToRead = new ReadValueIdCollection();
            try
            {
                for (int i = 0; i < nodesToRead.Count; i++)
                {
                    ReadValueId attributeToRead = new ReadValueId();
                    attributeToRead.NodeId = nodesToRead[i];
                    attributeToRead.AttributeId = Attributes.Value;
                    attributeToRead.Handle = attributeIdToString(Attributes.Value);
                    valueIdsToRead.Add(attributeToRead);
                }
                response = m_Session.Read(
                        null,
                        0,
                        TimestampsToReturn.Both,
                        valueIdsToRead,
                        out results,
                        out diagnosticInfos);
            }
            catch (Exception e)
            {
                throw e;
            }
            return response;
        }

总结

当我们作为客户端去读取服务器端的数据时,更多的还是要遵循服务器端的配置结构,比如,server、channel、group、item各个结构之间的组成关系。利用上面几个关键的方法,可以顺利的读取各中接口的OPC UA数据。文章来源地址https://www.toymoban.com/news/detail-617559.html

到了这里,关于【OPC UA】使用C#读取OPC UA电液控数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包