C# OpenProtocol 开放以太网协议 读写数据 订阅数据
主要使用的软件是 HslCommunication 关于这个软件的本身,详细可以参考下面的地址:
github地址:https://github.com/dathlin/HslCommunication
官网:http://www.hslcommunication.cn
加群咨询学习信息:http://www.hslcommunication.cn/Cooperation
在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控制台输入下面的指令安装:
1 | 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. 引用库
1 2 | using HslCommunication; using HslCommunication.Profinet.OpenProtocol; |
2. 演示简单的连接读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // 实例化对象,指定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. 封装的高级读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // 当然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. 订阅操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // 实例化对象,指定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 ); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2019-12-09 C# 使用.net core 驱动树莓派的IO信号
2017-12-09 Modbus tcp 格式说明 通讯机制 附C#测试工具用于学习,测试