826220679@qq.com
9 天以前 6d5ff381cafca9b82e11407dc67bf6b74f1397ee
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package publicsWay;
 
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
 
// ÖØÃüÃûΪPacketParser£¬±íʾÊý¾Ý°ü½âÎöÆ÷
public class PacketParser {
    // Ìí¼ÓÊ®Áù½øÖÆ×Ö·ûÊý×é³£Á¿
    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    // »º³åÇø×Ö½ÚÊý×é
    private byte[] buffer = new byte[4096];
    // µ±Ç°»º³åÇøÓÐЧÊý¾Ý³¤¶È
    private int bufferLength = 0;
 
    // ×·¼ÓÐÂÊý¾Ýµ½»º³åÇø
    public void appendData(byte[] newData, int length) {
        // ¼ì²é»º³åÇøÊÇ·ñ×ã¹»
        if (bufferLength + length > buffer.length) {
            // ´´½¨Ðµĸü´ó»º³åÇø
            byte[] newBuffer = new byte[bufferLength + length];
            // ¸´ÖÆÔ­ÓÐÊý¾Ý
            System.arraycopy(buffer, 0, newBuffer, 0, bufferLength);
            buffer = newBuffer;
        }
        // ×·¼ÓÐÂÊý¾Ý
        System.arraycopy(newData, 0, buffer, bufferLength, length);
        // ¸üлº³åÇø³¤¶È
        bufferLength += length;
    }
 
    // ÐÂÔö·½·¨£º×Ö½ÚÊý×éתʮÁù½øÖÆ×Ö·û´®
    public static String bytesToHexString(byte[] bytes) {
        if (bytes == null) return "";
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }
 
    // ½âÎö»º³åÇøÖеÄËùÓÐÍêÕûÊý¾Ý°ü
    public List<DataPacket> parsePackets() {
        List<DataPacket> packets = new ArrayList<>();
        int index = 0;  // µ±Ç°½âÎöλÖÃË÷Òý
 
        // ±éÀú»º³åÇø²éÕÒÍêÕûÊý¾Ý°ü
        while (index <= bufferLength - 4) { // È·±£ÓÐ×ã¹»Êý¾Ý¼ì²é°üÍ·
            // ¼ì²é°üÍ·±êʶ 0x55 0xAA
            if (buffer[index] == 0x55 && (buffer[index + 1] & 0xFF) == 0xAA) {
                // »ñÈ¡°üÀàÐÍ£¨µÚ3×Ö½Ú£©
                int packetType = buffer[index + 2] & 0xFF;
                // »ñÈ¡Êý¾Ý³¤¶È£¨µÚ4×Ö½Ú£©
                int dataLenField = buffer[index + 3] & 0xFF;
                // ¼ÆËãÍêÕû°ü³¤¶È£¨°üÍ·+Êý¾Ý+УÑ飩
                int totalPacketLen = 2 + dataLenField + 2;  // Ôö¼Ó2×Ö½ÚУÑéλ
 
                // ¼ì²éÊÇ·ñÓÐ×ã¹»Êý¾Ý¹¹³ÉÍêÕû°ü
                if (index + totalPacketLen > bufferLength) {
                    break; // Êý¾Ý²»×㣬µÈ´ý¸ü¶àÊý¾Ý
                }
 
                // ÌáÈ¡ÍêÕûÊý¾Ý°ü
                byte[] packetData = new byte[totalPacketLen];
                System.arraycopy(buffer, index, packetData, 0, totalPacketLen);
 
                // Ñé֤УÑéºÍ
                if (verifyChecksum(packetData)) {
                    // ½âÎöµ¥¸öÊý¾Ý°ü
                    DataPacket packet = parseSinglePacket(packetType, packetData);
                    if (packet != null) {
                        packets.add(packet); // Ìí¼Óµ½½á¹ûÁбí
                    }
                    index += totalPacketLen; // Òƶ¯µ½ÏÂÒ»¸ö°ü
                } else {
                    index++; // Ð£Ñéʧ°Ü£¬Ìø¹ýµ±Ç°×Ö½Ú
                }
            } else {
                index++; // ·Ç°üÍ·±êʶ£¬¼ÌÐø²éÕÒ
            }
        }
 
        // ÇåÀíÒÑ´¦ÀíÊý¾Ý
        if (index > 0) {
            int remaining = bufferLength - index;
            if (remaining > 0) {
                // Òƶ¯Ê£ÓàÊý¾Ýµ½»º³åÇø¿ªÍ·
                System.arraycopy(buffer, index, buffer, 0, remaining);
            }
            bufferLength = remaining; // ¸üлº³åÇø³¤¶È
        }
 
        return packets;
    }
 
    // Ð£ÑéÊý¾Ý°ü
    private boolean verifyChecksum(byte[] packet) {
        int len = packet.length;
        if (len < 4) return false;
 
        int sum = 0;
        // ´ÓÊý¾ÝÀàÐÍ¿ªÊ¼µ½Ð£ÑéÂëǰ½áÊø (°üÍ·2×Ö½ÚÒÑÌø¹ý)
        for (int i = 2; i < len - 2; i++) {
            sum += packet[i] & 0xFF;
        }
        sum = ~sum & 0xFFFF; // È¡·´²¢±£Áô16λ
 
        // »ñÈ¡°üÖеÄУÑéÂë (С¶Ëģʽ)
        int receivedChecksum = ((packet[len - 1] & 0xFF) << 8) | (packet[len - 2] & 0xFF);
 
        return sum == receivedChecksum;
    }
 
 
 
    // ½âÎöµ¥¸öÊý¾Ý°ü
    private DataPacket parseSinglePacket(int packetType, byte[] packet) {        
 
        // ´´½¨Êý¾Ý°ü¶ÔÏ󣨰üº¬°üÀàÐÍ£©
        return new DataPacket(packetType, packet);
    }
 
    // Êý¾Ý°üÄÚ²¿Àà
    public static class DataPacket {
        private final int packetType;  // °üÀàÐÍ
        private final byte[] packet;//°üÊý¾Ý
 
 
        public DataPacket(int packetType, byte[] packet) {
            this.packetType = packetType;
            this.packet = packet;
        }
 
        // »ñÈ¡°üÀàÐÍ
        public int getPacketType() {
            return packetType;
        }
 
        // °üÊý¾Ý
        public byte[] getPacket() {
            return packet;
        }
 
    }
 
    // HEX×Ö·û´®×ª×Ö½ÚÊý×é
    public static byte[] hexStringToBytes(String hex) {
        // ÒƳý¿Õ¸ñ
        hex = hex.replaceAll("\\s", "");
        int len = hex.length();
        // ´´½¨½á¹ûÊý×é
        byte[] data = new byte[len / 2];
        // Ã¿Á½¸ö×Ö·ûת»»Ò»¸ö×Ö½Ú
        for (int i = 0; i < len; i += 2) {
            // ¸ßλת»»
            int high = Character.digit(hex.charAt(i), 16) << 4;
            // µÍλת»»
            int low = Character.digit(hex.charAt(i + 1), 16);
            data[i / 2] = (byte) (high | low);
        }
        return data;
    }
 
    
    
 
    /**Êä³öУÑéÂë*/
    public static String calculateChecksum(String input) {
        // ¼ì²éÊäÈëÊÇ·ñΪ¿Õ
        if (input == null) {
            throw new IllegalArgumentException("ÊäÈë²»ÄÜΪnull");
        }
 
        // ÒƳýËùÓпոñ
        String cleanInput = input.replaceAll("\\s", "");
 
        // ÑéÖ¤´¦ÀíºóµÄ×Ö·û´®
        if (cleanInput.length() < 8 || !cleanInput.startsWith("55AA")) {
            throw new IllegalArgumentException("ÊäÈë×Ö·û´®±ØÐëÒÔ55AA¿ªÍ·ÇÒ³¤¶ÈÖÁÉÙΪ8£¨È¥³ý¿Õ¸ñºó£©");
        }
 
        // È¥µô°üÍ·(55AA)ºÍ×îºó4¸ö×Ö·û
        String dataPart = cleanInput.substring(4, cleanInput.length() - 4);
 
        // ¼ì²éÖм䲿·Ö³¤¶ÈÊÇ·ñΪżÊý
        if (dataPart.length() % 2 != 0) {
            throw new IllegalArgumentException("Öм䲿·Ö³¤¶È±ØÐëÊÇżÊý£¨È¥³ý¿Õ¸ñºó£©");
        }
 
        int sum = 0;
        // Ã¿Á½¸ö×Ö·û½âÎöΪһ¸ö×Ö½Ú
        for (int i = 0; i < dataPart.length(); i += 2) {
            String byteStr = dataPart.substring(i, i + 2);
            int byteValue = Integer.parseInt(byteStr, 16);
            sum = (sum + byteValue) & 0xFFFF; // ±£³Ö16λ·¶Î§
        }
 
        // È¡·´²¢±£³Ö16λ
        int checksum = (~sum) & 0xFFFF;
 
        // ÏÔʽ´¦Àí¸ßλÔÚǰ¸ñʽ
        int  lowByte= (checksum >>> 8) & 0xFF;  // ¸ß8λ
        int  highByte= checksum & 0xFF;           // µÍ8λ
 
        // ¸ñʽ»¯Îª4λʮÁù½øÖÆ×Ö·û´®£¨´óд£©£¬¸ßλÔÚǰ
        System.out.println(String.format("%02X%02X", highByte, lowByte));
        return String.format("%02X%02X", highByte, lowByte);
    }
 
    /** ½«×Ö½ÚÊý×éתΪʮÁù½øÖÆ×Ö·û´®£¬ÀýÈç [55 AA 01 02 ...] */
    private static String bytesToHex(byte[] bytes) {
        if (bytes == null) return "null";
        StringBuilder sb = new StringBuilder(bytes.length * 3);
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b & 0xFF));
        }
        // È¥µô×îºóÒ»¸ö¿Õ¸ñ
        return sb.substring(0, sb.length() - 1);
    }
}