首页 > 编程语言 >C# OpenProtocol 开放以太网协议 读写数据 订阅数据

C# OpenProtocol 开放以太网协议 读写数据 订阅数据

时间:2022-12-09 14:36:50浏览次数:64  
标签:Console sub C# OpenProtocol openProtocol read connect WriteLine 以太网

主要使用的软件是 HslCommunication 关于这个软件的本身,详细可以参考下面的地址:

github地址:https://github.com/dathlin/HslCommunication

官网:http://www.hslcommunication.cn

 

加群咨询学习信息:http://www.hslcommunication.cn/Cooperation 

在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控制台输入下面的指令安装:

 

Install-Package HslCommunication

  

如果需要教程:Nuget安装教程:http://www.cnblogs.com/dathlin/p/7705014.html

组件的api地址:http://api.hslcommunication.cn

使用手册:http://www.hsltechnology.cn/Doc/HslCommunication

Demo下载地址:http://www.hsltechnology.cn/Home/Download

 

在开始之前,我们先来看看HslCommunication能干什么?

 

 

我们使用这个库来实现 OpenProtocol 协议

 

 

1. 引用库


 

using HslCommunication;
using HslCommunication.Profinet.OpenProtocol;

  

 

2. 演示简单的连接读取


 

            // 实例化对象,指定open协议的ip地址
            OpenProtocolNet openProtocol = new OpenProtocolNet( "192.168.1.110", 4545 );

            // 连接
            OperateResult connect = openProtocol.ConnectServer( );
            if (connect.IsSuccess)
            {
                // connect success!
            }
            else
            {
                // connect failed 连接失败,应该提醒或是返回
                Console.WriteLine( "connect failed: " + connect.Message );
                return;
            }

            // 举例自定义读取数据
            // 以下例子使用 MID0010 例子读取参数ID的集合,具体参见手册
            // Send: 00200010            NUL
            // Rece: 00290011            002001002NUL
            OperateResult<string> read = openProtocol.ReadCustomer( 10, 1, -1, -1, null );
            if (read.IsSuccess)
            {
                List<int> list = new List<int>();
                int count = Convert.ToInt32( read.Content.Substring( 20, 3 ) );

                for (int i = 0; i < count; i++)
                {
                    list.Add( Convert.ToInt32( read.Content.Substring( 23 + i * 3, 3 ) ) );
                }

                // list 就是所有参数ID的集合,其他命令也是类似的解析

            }
            else
            {
                Console.WriteLine( "read failed: " + read.Message );
            }


            // 系统退出的时候关闭连接
            openProtocol.ConnectClose( );

  

 

 

3. 封装的高级读取


 

 

            // 当然hslcommunication里也提供了几个更加便捷的数据交互的方法
            // 以下例子使用 MID0010 例子读取参数ID的集合
            OperateResult<int[]> read = openProtocol.ParameterSetMessages.ParameterSetIDUpload( );
            if (read.IsSuccess)
            {
                // read.Content 就是读取的结果;
            }
            else
            {
                Console.WriteLine( "read failed: " + read.Message );
            }

            // 比如订阅,取消订阅操作
            OperateResult sub = openProtocol.ParameterSetMessages.ParameterSetSelectedSubscribe( );
            if (sub.IsSuccess)
            {
                Console.WriteLine( "sub success" );
            }
            else
            {
                Console.WriteLine( "sub faield: " + sub.Message );
            }

            OperateResult unsub = openProtocol.ParameterSetMessages.ParameterSetSelectedUnsubscribe( );
            if (unsub.IsSuccess)
            {
                Console.WriteLine( "unsub success" );
            }
            else
            {
                Console.WriteLine( "unsub faield: " + unsub.Message );
            }

  

4. 订阅操作


 

            // 实例化对象,指定open协议的ip地址
            OpenProtocolNet openProtocol = new OpenProtocolNet( "192.168.1.110", 4545 );
            // 绑定事件即可支持订阅操作
            openProtocol.OnReceivedOpenMessage += ( object sender, OpenEventArgs e ) =>
            {
                string mid = e.Content.Substring( 4, 4 );
                if (mid == "0015")
                {
                    // 如果订阅了 0014 的功能码

                }
                else if (mid == "0035")
                {
                    // 如果订阅了 Job Message的0034 功能码

                }
                // 等等
            };

            // 连接
            OperateResult connect = openProtocol.ConnectServer( );
            if (connect.IsSuccess)
            {
                // connect success!
            }
            else
            {
                // connect failed 连接失败,应该提醒或是返回
                Console.WriteLine( "connect failed: " + connect.Message );
                return;
            }

            // 订阅 0014
            OperateResult sub = openProtocol.ParameterSetMessages.ParameterSetSelectedSubscribe( );
            if (sub.IsSuccess)
            {
                Console.WriteLine( "sub success" );
            }
            else
            {
                Console.WriteLine( "sub faield: " + sub.Message );
            }

  

 

标签:Console,sub,C#,OpenProtocol,openProtocol,read,connect,WriteLine,以太网
From: https://www.cnblogs.com/dathlin/p/16968856.html

相关文章

  • RuntimeException 子类
    RuntimeException子类RuntimeException子类ArithmeticExceptionArrayIndexOutOfBoundsExceptionArrayStoreExceptionClassCastExceptionEnumConstantNotPresentExceptionIl......
  • 腾讯云对象存储cos获取图片像素信息
    腾讯云对象存储cos获取图片像素信息1上传时增加参数记录图片像素信息2展示时获取图片像素信息1上传时增加参数记录图片像素信息项目中是通过流类型上传文件到腾讯云cos的......
  • openstack ceph
    OpenStack集成ceph详细过程可以查看ceph官方文档:cephdocumentOpenStackQueens版本,1台控制节点controller,1台计算节点compute;1.创建存储池Glance:Glance可以把镜像存......
  • 轻松上手ECS云服务器
    轻松上手ECS云服务器•什么是阿里云服务器ECS?•如何操作阿里云服务器ECS?•总结什么是阿里云服务器ECS?既然是要上手ECS云服务器,那么我们首先要了解......
  • KubeSphere 接入外部 Elasticsearch 最佳实践
    作者:张坚,科大讯飞开发工程师,云原生爱好者。大家好,我是张坚。今天来聊聊如何在KubeSphere中集成外置的ES组件。KubeSphere在安装完成时候可以启用日志组件,这样会安装ES......
  • Unity 基础 之 WebCamTexture 获取设备摄像头(包括PC和移动端),简单渲染到 UGUI 或 游戏
    一、简单介绍Unity中的一些基础知识点。本节介绍,使用WebCamTexture 获取设别的摄像头,并且进行渲染,同时解决有可能第一次授权却没有显示画面的情况。 二、实现原理......
  • m基于ACO蚁群优化的FCM模糊聚类算法matlab仿真
    1.算法概述蚁群算法是通过对自然界中真实蚂蚁的集体行为的观察,模拟而得到一种仿生优化算法,它具有很好的并行性,分布性.根据蚂蚁群体不同的集体行为特征,蚁群算法可分为受......
  • git pick
    1.查看分支x 提交版本号记录gitlog分支X    //  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2.切换到待合并分支 ygitcheckouty3.执行合并gitcherry-pick xxxx......
  • 2022第三届云原生编程挑战赛--Serverless VSCode WebIDE使用体验
    2022第三届云原生编程挑战赛--ServerlessVSCodeWebIDE使用体验​​活动背景​​​​赛题说明​​​​ServerlessVSCodeWebIDE体验​​​​体验入口​​​​什么是Server......
  • 关于 SQL navigation function 的一点使用记录
    来自于SQL2011对窗口函数的增强,新添加了叫导航函数的类别,进一步丰富了窗口的计算能力。这将一次记录几个比较常用的导航函数他们包含,FIRST_VALUELAST_VALUENTH_VALU......