C# OpenProtocol 开放以太网协议 读写数据 订阅数据
主要使用的软件是 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 );
}

浙公网安备 33010602011771号