点击或拖拽改变大小

OpenProtocolNet 类

开放以太网协议,在拧紧枪中应用广泛,本通信支持基本的问答机制以及订阅机制,支持完全自定义的参数指定及数据读取。
Open Ethernet protocol, widely used in tightening guns, this communication supports basic question answering mechanism and subscription mechanism, supports fully customized parameter specification and data reading.
继承层次
SystemObject
  HslCommunication.Core.NetBinaryCommunication
    HslCommunication.Core.NetTcpNetCommunication
      HslCommunication.Profinet.OpenProtocolOpenProtocolNet

命名空间:  HslCommunication.Profinet.OpenProtocol
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.1.0.0 (12.1.0.0)
语法
public class OpenProtocolNet : TcpNetCommunication

OpenProtocolNet 类型公开以下成员。

构造函数
  名称说明
公共方法OpenProtocolNet
实例化一个默认的对象
Instantiate a default object
公共方法OpenProtocolNet(String, Int32)
使用指定的IP地址及端口来初始化对象
Use the specified IP address and port to initialize the object
Top
属性
  名称说明
公共属性AutoAckControllerMessage
针对控制器的主动发送的消息,是否无视Ack标记,全部进行返回信号操作,默认为 False,将根据报文里的Ack信号来决定是否返回数据
If the default value is False, the controller will decide whether to return data based on the Ack signal in the packet
公共属性CommunicationPipe
获取或设置当前的管道信息,管道类型为CommunicationPipe的继承类,内置了PipeTcpNet管道,PipeUdpNet管道,PipeSerialPort管道等
(继承自 BinaryCommunication。)
公共属性ConnectionId
当前连接的唯一ID号,默认为长度20的guid码加随机数组成,方便列表管理,也可以自己指定
The unique ID number of the current connection. The default is a 20-digit guid code plus a random number.
(继承自 BinaryCommunication。)
公共属性代码示例ConnectTimeOut
获取或设置接收服务器反馈的时间,如果为负数,则不接收反馈
Gets or sets the time to receive server feedback, and if it is a negative number, does not receive feedback
(继承自 TcpNetCommunication。)
公共属性代码示例IpAddress
获取或是设置远程服务器的IP地址,如果是本机测试,那么需要设置为127.0.0.1
Get or set the IP address of the remote server. If it is a local test, then it needs to be set to 127.0.0.1
(继承自 TcpNetCommunication。)
公共属性JobMessage
任务消息的相关属性,可以用来获取任务的数据,订阅任务,取消订阅任务,选择任务,启动任务。
The relevant properties of task messages can be used to obtain task data, subscribe to tasks, unsubscribe tasks, select tasks, and start tasks.
公共属性LocalBinding
获取或设置绑定的本地的IP地址和端口号信息,如果端口设置为0,代表任何可用的端口
Get or set the bound local IP address and port number information, if the port is set to 0, it means any available port
(继承自 TcpNetCommunication。)
公共属性代码示例LogNet
组件的日志工具,支持日志记录,只要实例化后,当前网络的基本信息,就以DEBUG等级进行输出
The component's logging tool supports logging. As long as the instantiation of the basic network information, the output will be output at DEBUG
(继承自 BinaryCommunication。)
公共属性ParameterSetMessages
参数集合操作的相关属性,可以用来获取参数ID列表,设置数据等操作。
The properties related to parameter collection operations can be used to obtain parameter ID lists, set data, and other operations.
公共属性代码示例Port
获取或设置服务器的端口号,具体的值需要取决于对方的配置
Gets or sets the port number of the server. The specific value depends on the configuration of the other party.
(继承自 TcpNetCommunication。)
公共属性代码示例ReceiveTimeOut
获取或设置接收服务器反馈的时间,如果为负数,则不接收反馈
Gets or sets the time to receive server feedback, and if it is a negative number, does not receive feedback
(继承自 BinaryCommunication。)
公共属性RevisonOnConnected
获取或设置初始化连接时 MID0001 命令的版本号,默认为 1,如果设置小于 0,则表示不发送 MID0001 命令。
Get or set the version of the MID0001 command when initializing the connection, the default is 1, if the setting is less than 0, it means that the MID0001 command is not sent.
公共属性SendBeforeHex
获取或设置在发送通信报文前追加发送的字节信息,HEX格式,通常用于lora组网时,需要携带 00 00 00 02 四个字节的站地址功能。
Obtain or set the byte information sent before sending communication packets, HEX format, usually used for LORA networking, you need to carry 00 00 00 02 four-byte station address function.
(继承自 BinaryCommunication。)
公共属性SleepTime
获取或设置在正式接收对方返回数据前的时候,需要休息的时间,当设置为0的时候,不需要休息。
Get or set the time required to rest before officially receiving the data from the other party. When it is set to 0, no rest is required.
(继承自 BinaryCommunication。)
公共属性SocketKeepAliveTime
获取或设置客户端的Socket的心跳时间信息,这个是Socket底层自动实现的心跳包,不基于协议层实现。默认小于0,不开启心跳检测,如果需要开启,设置 60_000 比较合适,单位毫秒
Get or set the heartbeat time information of the Socket of the client. This is the heartbeat packet automatically implemented by the bottom layer of the Socket, not based on the protocol layer. The default value is less than 0, and heartbeat detection is not enabled. If you need to enable it, it is more appropriate to set 60_000, in milliseconds.
(继承自 TcpNetCommunication。)
公共属性TighteningResultMessages
拧紧结果消息的操作属性
公共属性TimeMessages
时间消息的属性
公共属性ToolMessages
工具消息的操作属性
Top
方法
  名称说明
公共方法静态成员BuildOpenProtocolMessage
构建一个读取的初始报文
公共方法静态成员BuildReadCommand
构建一个读取的初始报文
公共方法静态成员CheckRequestReplyMessages
检查请求返回的消息是否合法的
公共方法代码示例ConnectClose
手动断开与远程服务器的连接,如果当前是长连接模式,那么就会切换到短连接模式
Manually disconnect from the remote server, if it is currently in long connection mode, it will switch to short connection mode
(继承自 TcpNetCommunication。)
公共方法代码示例ConnectCloseAsync
手动断开与远程服务器的连接,如果当前是长连接模式,那么就会切换到短连接模式
Manually disconnect from the remote server, if it is currently in long connection mode, it will switch to short connection mode
(继承自 TcpNetCommunication。)
公共方法代码示例ConnectServer
尝试连接远程的服务器,如果连接成功,就切换短连接模式到长连接模式,后面的每次请求都共享一个通道,使得通讯速度更快速
Try to connect to a remote server. If the connection is successful, switch the short connection mode to the long connection mode. Each subsequent request will share a channel, making the communication speed faster.
(继承自 TcpNetCommunication。)
公共方法代码示例ConnectServerAsync
尝试连接远程的服务器,如果连接成功,就切换短连接模式到长连接模式,后面的每次请求都共享一个通道,使得通讯速度更快速
Try to connect to a remote server. If the connection is successful, switch the short connection mode to the long connection mode. Each subsequent request will share a channel, making the communication speed faster.
(继承自 TcpNetCommunication。)
受保护的方法DecideWhetherQAMessage
决定当前的消息是否是用于问答机制返回的消息,默认直接返回 true, 实际的情况需要根据协议进行重写方法
To determine whether the current message is the message returned by the question answering mechanism, the default is true. In actual cases, the rewriting method needs to be performed according to the protocol
(重写 BinaryCommunicationDecideWhetherQAMessage(CommunicationPipe, OperateResultByte).)
公共方法Equals
Determines whether the specified object is equal to the current object.
(继承自 Object。)
受保护的方法ExtraAfterReadFromCoreServer
和服务器交互完成的时候调用的方法,可以根据读写结果进行一些额外的操作,具体的操作需要根据实际的需求来重写实现
The method called when the interaction with the server is completed can perform some additional operations based on the read and write results. The specific operations need to be rewritten according to actual needs.
(继承自 BinaryCommunication。)
受保护的方法代码示例ExtraOnDisconnect
根据实际的协议选择是否重写本方法,有些协议在断开连接之前,需要发送一些报文来关闭当前的网络通道
Select whether to rewrite this method according to the actual protocol. Some protocols need to send some packets to close the current network channel before disconnecting.
(重写 BinaryCommunicationExtraOnDisconnect.)
受保护的方法代码示例ExtraOnDisconnectAsync
根据实际的协议选择是否重写本方法,有些协议在断开连接之前,需要发送一些报文来关闭当前的网络通道
Select whether to rewrite this method according to the actual protocol. Some protocols need to send some packets to close the current network channel before disconnecting.
(继承自 BinaryCommunication。)
受保护的方法Finalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(继承自 Object。)
公共方法静态成员GetErrorText
根据错误代码来获取到错误文本信息
公共方法GetHashCode
Serves as the default hash function.
(继承自 Object。)
受保护的方法GetLogTextFromBinary
获取当前的报文进行日志记录的时候,是否使用二进制的格式记录,默认返回 LogMsgFormatBinary,重写可以根据session对象分别返回不同记录模式
Whether to log the current packet in binary format, the default return is LogMsgFormatBinary. If you want to override it, different recording modes can be returned according to session
(继承自 BinaryCommunication。)
受保护的方法GetNewNetMessage
获取一个新的消息对象的方法,需要在继承类里面进行重写
The method to get a new message object needs to be overridden in the inheritance class
(重写 BinaryCommunicationGetNewNetMessage.)
公共方法GetType
Gets the Type of the current instance.
(继承自 Object。)
受保护的方法代码示例InitializationOnConnect
根据实际的协议选择是否重写本方法,有些协议在创建连接之后,需要进行一些初始化的信号握手,才能最终建立网络通道。
Whether to rewrite this method is based on the actual protocol. Some protocols require some initial signal handshake to establish a network channel after the connection is created.
(重写 BinaryCommunicationInitializationOnConnect.)
受保护的方法代码示例InitializationOnConnectAsync
根据实际的协议选择是否重写本方法,有些协议在创建连接之后,需要进行一些初始化的信号握手,才能最终建立网络通道。
Whether to rewrite this method is based on the actual protocol. Some protocols require some initial signal handshake to establish a network channel after the connection is created.
(重写 BinaryCommunicationInitializationOnConnectAsync.)
公共方法IpAddressPing
对当前设备的IP地址进行PING的操作,返回PING的结果,正常来说,返回Success
PING the IP address of the current device and return the PING result. Normally, it returns Success
(继承自 TcpNetCommunication。)
受保护的方法LogRevcMessage(Byte)
使用日志记录一个接收的报文信息
` Logs are used to record information about a received packet
(继承自 BinaryCommunication。)
受保护的方法LogRevcMessage(Byte, PipeSession)
使用日志记录一个接收的报文信息
` Logs are used to record information about a received packet
(继承自 BinaryCommunication。)
受保护的方法LogSendMessage(Byte)
使用日志记录一个发送的报文信息
Logs are used to record information about a send packet
(继承自 BinaryCommunication。)
受保护的方法LogSendMessage(Byte, PipeSession)
使用日志记录一个发送的报文信息
Logs are used to record information about a send packet
(继承自 BinaryCommunication。)
受保护的方法MemberwiseClone
Creates a shallow copy of the current Object.
(继承自 Object。)
公共方法PackCommandWithHeader
对当前的命令进行打包处理,通常是携带命令头内容,标记当前的命令的长度信息,需要进行重写,否则默认不打包
The current command is packaged, usually carrying the content of the command header, marking the length of the current command, and it needs to be rewritten, otherwise it is not packaged by default
(继承自 BinaryCommunication。)
公共方法ReadCustomer
使用自定义的命令读取数据,需要指定每个参数信息,然后返回字符串数据内容,根据实际的功能码,解析出实际的数据信息
To use a custom command to read data, you need to specify each parameter information, then return the string data content, and parse the actual data information according to the actual function code
公共方法ReadCustomerAsync
使用自定义的命令读取数据,需要指定每个参数信息,然后返回字符串数据内容,根据实际的功能码,解析出实际的数据信息
To use a custom command to read data, you need to specify each parameter information, then return the string data content, and parse the actual data information according to the actual function code
公共方法代码示例ReadFromCoreServer(Byte)
将二进制的数据发送到管道中去,然后从管道里接收二进制的数据回来,并返回是否成功的结果对象。
Send binary data to the pipeline, and then receive binary data back from the pipeline, and return whether the success of the result object
(继承自 BinaryCommunication。)
公共方法ReadFromCoreServer(IEnumerableByte) (继承自 BinaryCommunication。)
公共方法代码示例ReadFromCoreServer(Byte, Boolean, Boolean)
将二进制的数据发送到管道中去,然后从管道里接收二进制的数据回来,并返回是否成功的结果对象。
Send binary data to the pipeline, and then receive binary data back from the pipeline, and return whether the success of the result object
(继承自 BinaryCommunication。)
公共方法ReadFromCoreServer(CommunicationPipe, Byte, Boolean, Boolean)
使用指定的管道来进行数据通信,发送原始数据到管道,然后从管道接收相关的数据返回,本方法无锁
(继承自 BinaryCommunication。)
公共方法代码示例ReadFromCoreServerAsync(Byte)
将二进制的数据发送到管道中去,然后从管道里接收二进制的数据回来,并返回是否成功的结果对象。
Send binary data to the pipeline, and then receive binary data back from the pipeline, and return whether the success of the result object
(继承自 BinaryCommunication。)
公共方法ReadFromCoreServerAsync(IEnumerableByte) (继承自 BinaryCommunication。)
公共方法代码示例ReadFromCoreServerAsync(Byte, Boolean, Boolean)
将二进制的数据发送到管道中去,然后从管道里接收二进制的数据回来,并返回是否成功的结果对象。
Send binary data to the pipeline, and then receive binary data back from the pipeline, and return whether the success of the result object
(继承自 BinaryCommunication。)
公共方法ReadFromCoreServerAsync(CommunicationPipe, Byte, Boolean, Boolean)
使用指定的管道来进行数据通信,发送原始数据到管道,然后从管道接收相关的数据返回,本方法无锁
(继承自 BinaryCommunication。)
公共方法SetDtuPipe
将当前的通信对象设置DTU模式,允许传入现成的管道,并返回初始化结果,如果该设备重写了握手报文,就是返回握手结果
Set the current communication object to DTU mode, allow the existing pipe to be passed in, and return the initialization result, if the device rewrites the handshake packet, the handshake result is returned
(继承自 BinaryCommunication。)
公共方法SetDtuPipeAsync
将当前的通信对象设置DTU模式,允许传入现成的管道,并返回初始化结果,如果该设备重写了握手报文,就是返回握手结果
Set the current communication object to DTU mode, allow the existing pipe to be passed in, and return the initialization result, if the device rewrites the handshake packet, the handshake result is returned
(继承自 BinaryCommunication。)
公共方法ToString
Returns a string that represents the current object.
(重写 TcpNetCommunicationToString.)
公共方法UnpackResponseContent
根据对方返回的报文命令,对命令进行基本的拆包,例如各种Modbus协议拆包为统一的核心报文,还支持对报文的验证
According to the message command returned by the other party, the command is basically unpacked, for example, various Modbus protocols are unpacked into a unified core message, and the verification of the message is also supported
(继承自 BinaryCommunication。)
Top
事件
  名称说明
公共事件OnReceivedOpenMessage
当接收到OpenProtocol协议消息触发的事件
Top
字段
  名称说明
受保护的字段LogMsgFormatBinary
设置日志记录报文是否二进制,如果为False,那就使用ASCII码
Set whether the log message is binary, if it is False, then use ASCII code
(继承自 BinaryCommunication。)
Top
扩展方法
  名称说明
公共扩展器方法ToJsonString
获取当前对象的JSON格式表示的字符串。
Gets the string represented by the JSON format of the current object.
(由 HslExtension 定义。)
Top
备注
自定义的读取使用ReadCustomer(Int32, Int32, Int32, Int32, ListString)来实现,如果是订阅的数据,使用OnReceivedOpenMessage绑定自己的方法触发。更详细的示例参考:http://api.hslcommunication.cn
Custom reads are implemented using ReadCustomer(Int32, Int32, Int32, Int32, ListString), and if it is subscribed data, use OnReceivedOpenMessage bind your own method to trigger it. For a more detailed example, refer to: http://api.hslcommunication.cn
示例
连接及自定义读取使用
// 实例化对象,指定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( );
便捷的读取示例
// 当然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 );
}
订阅事件处理操作
// 实例化对象,指定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 );
}
参见