MelsecQnA3EAsciiMessage.cs
2.18 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Text;
namespace HslCommunication.Core.IMessage
{
/// <summary>
/// 基于MC协议的Qna兼容3E帧协议的ASCII通讯消息机制
/// </summary>
public class MelsecQnA3EAsciiMessage : INetMessage
{
/// <summary>
/// 消息头的指令长度
/// </summary>
public int ProtocolHeadBytesLength
{
get
{
return 18;
}
}
/// <summary>
/// 从当前的头子节文件中提取出接下来需要接收的数据长度
/// </summary>
/// <returns>返回接下来的数据内容长度</returns>
public int GetContentLengthByHeadBytes()
{
byte[] buffer = new byte[4];
buffer[0] = HeadBytes[14];
buffer[1] = HeadBytes[15];
buffer[2] = HeadBytes[16];
buffer[3] = HeadBytes[17];
return Convert.ToInt32(Encoding.ASCII.GetString(buffer), 16);
}
/// <summary>
/// 检查头子节的合法性
/// </summary>
/// <param name="token">特殊的令牌,有些特殊消息的验证</param>
/// <returns>是否成功的结果</returns>
public bool CheckHeadBytesLegal(byte[] token)
{
if (HeadBytes == null) return false;
if (HeadBytes[0] == (byte)'D' && HeadBytes[1] == (byte)'0' && HeadBytes[2] == (byte)'0' && HeadBytes[3] == (byte)'0')
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 获取头子节里的消息标识
/// </summary>
/// <returns>消息标识</returns>
public int GetHeadBytesIdentity()
{
return 0;
}
/// <summary>
/// 消息头字节
/// </summary>
public byte[] HeadBytes { get; set; }
/// <summary>
/// 消息内容字节
/// </summary>
public byte[] ContentBytes { get; set; }
/// <summary>
/// 发送的字节信息
/// </summary>
public byte[] SendBytes { get; set; }
}
}