OPC UA 连接客户端测试连接工具(KEPServerEX6)

关于opc ua介绍
OPC是应用于工业通信的,在windows环境的下一种通讯技术,原有的通信技术难以满足日益复杂的环境,在可扩展性,安全性,跨平台性方面的不足日益明显,所以OPC基金会在几年前提出了面向未来的架构设计的OPC 统一架构,简称OPC UA,截止目前为止,越来越多公司将OPC UA作为开放的数据标准,在未来工业4.0行业上也将大放异彩。
在OPC UA的服务器端。会公开一些数据节点,或是方法等信息,允许第三方使用标准的OPC协议来进行访问,在传输层已经安全的处理所有的消息,对于客户端的访问来说,应该是非常清楚简单的。
前期准备
准备好开发的IDE,首选Visual Studio2017版本以及最新版本,新建项目,打开NuGet管理器,添加OpcUaHelper。
在程序页面可以引用 using OpcUaHelper;
节点浏览器
在窗体界面添加一个button按钮;
双击进入事件,编写程序

private void button1_Click(object sender, EventArgs e)
{
    using (FormBrowseServer form = new FormBrowseServer())
    {
        form.ShowDialog();
    }
}

在这里插入图片描述打开后
在这里插入图片描述连接Kepserver EX6读取数据
客户端实例化

 private OpcUaClient opcUaClient = new OpcUaClient();
        private async void Form1_Load(object sender, EventArgs e)
        {
            // connect to server, this is a sample
            try
            {
                await opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
                //opc.tcp://118.24.36.220:62547/DataAccessServer");
            }
            catch (Exception ex)
            {
                ClientUtils.HandleException("Connected Failed", ex);
            }

        }
         private void button1_Click(object sender, EventArgs e)
        {
            using (FormBrowseServer form = new FormBrowseServer("opc.tcp://127.0.0.1:49320")) // opc.tcp://118.24.36.220:62547/DataAccessServer"))
            {
                form.ShowDialog();
            }
        }
    在窗体载入的时候实例化,在窗体关闭的时候断开连接。下面的节点操作和其他操作使用的实例都是这个opcUaClient,如果你连接的服务器是需要用户名和密码的,那么修改Load中的代码如下:
private async void Form1_Load(object sender, EventArgs e)
        {
            // connect to server, this is a sample
            try
            {
           	 opcUaClient.UserIdentity = new Opc.Ua.UserIdentity("admin", "123456");
                await opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
                //opc.tcp://118.24.36.220:62547/DataAccessServer");
            }
            catch (Exception ex)
            {
                ClientUtils.HandleException("Connected Failed", ex);
            }

        }

节点读取操作
我们要读取一个节点数据,有两个信息是必须知道的

  1. 节点的ID标识,就是在上述节点浏览器中的编辑框的信息(“ns=2;s=Machines/Machine A/Name”)

  2. 节点的数据类型,这个是必须知道的,不然也不好读取数据。(“string”)

上面的两个信息都可以通过节点浏览器来获取到信息,现在,我们已经获取到了这两个信息,就上面的括号里的数据,然后我们在新增一个按钮,来读取数据:

 private void button2_Click(object sender, EventArgs e)  //单一节点读取操作
        {
            try
            {
                //string value = opcUaClient.ReadNode<string>("i=2262");
                //MessageBox.Show(value); // 显示测试数据
                UInt16 value2 = opcUaClient.ReadNode<UInt16>("ns=2;s=通道 1.设备 1.标记 2");
                textBox5.Text = value2.ToString();
            }
            catch (Exception ex)
            { // 使用了opc ua的错误处理机制来处理错误,网络不通或是读取拒绝
                ClientUtils.HandleException(Text, ex);
            }
        }

KEP连接失败时:
在KEPserverEX6目录里找到opcuacm.exe,添加服务器端点可解决,在C#里也需要修改url。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,你的问题是如何在C#中使用OPCUA通讯,绑定KEPserverEX6的节点浏览目录到WPF的listbox上。我会尽力回答你的问题。 首先,你需要安装OPCUA的相关库。可以使用NuGet安装OPCUA库,具体操作如下: 1. 打开Visual Studio 2019,创建一个新的WPF应用程序项目。 2. 在“解决方案资源管理器”中,右键单击项目名称,然后选择“管理NuGet程序包”。 3. 在“NuGet程序包管理器”中,在搜索框中输入“OPCUA”,然后选择“OPCUA.Core”、“OPCUA.Client”和“OPCUA.Server”这三个包进行安装。 安装完成后,我们可以开始编写代码了。首先,我们需要创建一个OPC UA客户端实例。在MainWindow.xaml.cs文件中,添加以下代码: ```csharp using Opc.Ua; using Opc.Ua.Client; // 创建OPC UA客户端实例 private static readonly ApplicationConfiguration Config = new ApplicationConfiguration() { ApplicationName = "OPCUA-Client", ApplicationType = ApplicationType.Client, SecurityConfiguration = new SecurityConfiguration { ApplicationCertificate = new CertificateIdentifier { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPCUA-Client\pki\own" }, TrustedPeerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPCUA-Client\pki\trusted" }, TrustedIssuerCertificates = new CertificateTrustList { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPCUA-Client\pki\issuer" }, RejectedCertificateStore = new CertificateStoreIdentifier { StoreType = @"Directory", StorePath = @"%CommonApplicationData%\OPCUA-Client\pki\rejected" } }, TransportConfigurations = new TransportConfigurationCollection(), TransportQuotas = new TransportQuotas { OperationTimeout = 15000 }, ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000, WellKnownDiscoveryUrls = new StringCollection { "opc.tcp://localhost:4840" } }, TraceConfiguration = new TraceConfiguration() }; private static readonly SessionChannel _sessionChannel = null; private static readonly Session _session = null; public MainWindow() { InitializeComponent(); // 创建一个OPC UA客户端实例 var endpointUrl = "opc.tcp://localhost:4840"; var endpoint = new EndpointDescription(endpointUrl); var endpointConfiguration = EndpointConfiguration.Create(Config); var endpointBinding = endpoint.CreateBinding(); var endpointChannel = new ChannelBase(endpointConfiguration, endpointBinding, new Uri(endpointUrl)); _sessionChannel = SessionChannel.Create( Config, endpoint, endpointConfiguration, endpointBinding, new ConfiguredEndpoint(null, endpoint, endpointConfiguration), endpointChannel); var credentials = new UserIdentity(new AnonymousIdentityToken()); _session = Session.Create( Config, _sessionChannel, new ConfiguredEndpoint(null, endpoint, endpointConfiguration), true, String.Empty, credentials, null).Result; // 浏览节点 var rootNode = new ReferenceDescription { DisplayName = "Objects", NodeId = ObjectIds.ObjectsFolder, ReferenceTypeId = ReferenceTypeIds.Organizes, TypeDefinition = ObjectTypeIds.FolderType }; var nodes = _session.FetchReferences(rootNode).Result; var listItems = new List<string>(); foreach (var node in nodes) { listItems.Add(node.DisplayName.Text); } ListBox.ItemsSource = listItems; } ``` 在上述代码中,我们创建了一个OPC UA客户端实例,并且通过OPC UA客户端实例浏览了KEPserverEX6的节点目录,将其显示在了WPF的ListBox控件上。 最后,在MainWindow.xaml文件中,添加一个ListBox控件,代码如下: ```xml <Window x:Class="OPCUA_Client.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <ListBox x:Name="ListBox" HorizontalAlignment="Left" Height="400" Margin="10,10,0,0" VerticalAlignment="Top" Width="760"/> </Grid> </Window> ``` 至此,我们就完成了在C#中使用OPCUA通讯,绑定KEPserverEX6的节点浏览目录到WPF的ListBox上的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值