wxr
2023-08-23 96e9ff2e5a54f54cdbb6b8b2f049fdf6a837de3c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.Entity;
using HDL_ON.UI;
using Shared;
 
namespace HDL_ON.DriverLayer
{
    /// <summary>
    /// bus数据包
    /// </summary>
    public class Packet
    {
        /// <summary>
        /// 缓冲区大小
        /// Link协议现在一个包的数据比较大,缓冲区太小存不完全部数据 2023-07-14 16:03:56  wxr
        /// </summary>
        public const int Size = 1024 * 10;
 
        /// <summary>
        /// 接收到的数据
        /// </summary>
        public byte[] Bytes;
 
        /// <summary>
        /// 数据发送IP地址
        /// </summary>
        public System.Net.EndPoint RemoteEndPoint;
 
        public Packet()
        {
            this.Bytes = new byte[Size];
            RemoteEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
        }
        public Packet(byte[] data, System.Net.EndPoint remoteEndPoint)
        {
            this.Bytes = data;
            this.RemoteEndPoint = remoteEndPoint;
        }
 
        /// <summary>
        /// 记录已经发送数据出去的时间
        /// </summary>
        public DateTime FlagDateTime;
 
        /// <summary>
        /// 已经发送了多少数
        /// </summary>
        public int HaveSendCount;
 
        /// <summary>
        /// 处理接收到的数据
        /// </summary>
        public virtual void Manager()
        {
            //对于操作数据库的时间比较长的,可以创建另一个线程处理
            if (!"HDLMIRACLE".Equals(Encoding.ASCII.GetString(Bytes, 4, 10)))
            {
                return;
            }
 
            byte subnetID = this.Bytes[17]; //源子网号
            byte deviceID = this.Bytes[18]; //源设备号
 
            //源设备类型
            int deviceType = this.Bytes[19] * 256 + this.Bytes[20];
 
            byte targetSubnetID = this.Bytes[23];
            byte targetDeviceID = this.Bytes[24];
 
            //不是要接收的指令就返回
            if (!((targetSubnetID == 0 && targetDeviceID == 252) || (targetSubnetID == 0xff && targetDeviceID == 0xff)))
            {
                return;
            }
            byte[] usefulBytes = null;
            if (this.Bytes[16] == 0xFF)
            {
                usefulBytes = new byte[Bytes.Length - 16 - 11];
                Array.Copy(Bytes, 27, usefulBytes, 0, usefulBytes.Length);
            }
            else
            {
                //有用的附加数据
                usefulBytes = new byte[this.Bytes[16] - 11];
                Array.Copy(Bytes, 25, usefulBytes, 0, usefulBytes.Length);
            }
        }
 
        /// <summary>
        /// byte转16进制字符串
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        string ByteToHex16(byte b)
        {
            string s = Convert.ToString(b, 16).ToUpper();
            if (s.Length <= 1)
            {
                return "0" + s;
            }
            return s;
        }
 
    }
}