Blame view

sys/sys_HSL/Hh.Mes.HSL/Core/IMessage/S7Message.cs 1.83 KB
赖素文 authored
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
namespace HslCommunication.Core.IMessage
{

    /// <summary>
    /// 西门子S7协议的消息解析规则
    /// </summary>
    public class S7Message : INetMessage
    {
        /// <summary>
        /// 西门子头字节的长度
        /// </summary>
        public int ProtocolHeadBytesLength
        {
            get { return 4; }
        }


        /// <summary>
        /// 头子节的数据
        /// </summary>
        public byte[] HeadBytes { get; set; }


        /// <summary>
        /// 内容字节的数据
        /// </summary>
        public byte[] ContentBytes { get; set; }


        /// <summary>
        /// 检查头子节是否合法的判断
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns>是否合法的</returns>
        public bool CheckHeadBytesLegal(byte[] token)
        {
            if (HeadBytes == null) return false;

            if (HeadBytes[0] == 0x03 && HeadBytes[1] == 0x00)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 获取剩余的内容长度
        /// </summary>
        /// <returns>数据内容长度</returns>
        public int GetContentLengthByHeadBytes()
        {
            if (HeadBytes?.Length >= 4)
            {
                return HeadBytes[2] * 256 + HeadBytes[3] - 4;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取消息号,此处无效
        /// </summary>
        /// <returns>消息标识</returns>
        public int GetHeadBytesIdentity()
        {
            return 0;
        }


        /// <summary>
        /// 发送的字节信息
        /// </summary>
        public byte[] SendBytes { get; set; }
    }
}