panlili2024
2024-09-19 071a8328823a2861f93ce556a4da3e4119cab1a3
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.hdl.sdk.ttl.Utils.HDLUtlis;
 
import android.os.SystemClock;
import android.util.Log;
 
import com.hdl.sdk.ttl.Config.Global;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.ReceiveData;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.SendDatas;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.UdpDataBean;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.Crc;
import com.hdl.sdk.ttl.HDLDeviceManger.Core.HDLSerialPortCore;
 
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Calendar;
 
/**
 * Created by hxb on 2023/10/18.
 */
public class SerialPortSendAndReceiveUtil {
    private static volatile SerialPortSendAndReceiveUtil instance = null;
 
    /**
     * 发送线程
     */
    private Thread sendThread;
 
    /**
     * 接收线程
     */
    private Thread receiveThread;
 
 
    private SerialPortSendAndReceiveUtil() {
        sendThread = new Thread(new SendRunnable());
        receiveThread = new Thread(new ReceiveRunnable());
    }
 
    public static SerialPortSendAndReceiveUtil getInstance() {
        if (instance == null) {
            synchronized (SerialPortSendAndReceiveUtil.class) {
                if (instance == null) {
                    instance = new SerialPortSendAndReceiveUtil();
                }
            }
        }
        return instance;
    }
 
    public void start() {
        sendThread.start();
        receiveThread.start();
 
        Global.mReceiveData = data;
    }
 
    /**
     * 获取封装数据
     *
     * @param body        发送数据内容
     * @param writeOrRead 读写标识,1表示写,2表示读取
     * @return
     */
    private static byte[] getSendByte(byte[] body, int writeOrRead) {
        byte[] crcBytes = new byte[2 + 1 + 1 + body.length + 2];
        crcBytes[0] = (byte) 0XFE;
        crcBytes[1] = (byte) 0xFE;
        crcBytes[2] = (byte) writeOrRead;
        crcBytes[3] = (byte) body.length;
        System.arraycopy(body, 0, crcBytes, 4, body.length);
 
        //Check CRC
        Crc.CRC16_MODBUS(crcBytes, crcBytes.length);
 
        return crcBytes;
    }
 
 
    static class SendRunnable implements Runnable {
        /**
         * 发送一条后休眠时间
         */
        private int sleepTime = 30;
        /**
         * 重发间隔时间
         */
        private int intervalTime = 500;
 
        /**
         * 重发次数
         */
        private int reSendCount = 3;
 
        /**
         * 线程运行状态
         */
        private boolean run = true;
 
        @Override
        public void run() {
            while (run) {
                try {
                    SystemClock.sleep(sleepTime);
                    if (HDLSerialPortCore.mSerialPort == null) {
                        Log.i("串口-发送->", "串口没有初始化");
                        continue;
                    }
 
                    for (int i = 0; i < SendDatas.sendDataArrayList.size(); i++) {
                        SendDatas sendDatas = SendDatas.sendDataArrayList.get(i);
 
                        //第一次发送,时间先初始化
                        if (sendDatas.StartCalendar == null) {
                            sendDatas.StartCalendar = Calendar.getInstance();
                        } else {
                            //距离上次发送的时间如还在重发时间内,先不发
                            if (Calendar.getInstance().getTimeInMillis() - sendDatas.StartCalendar.getTimeInMillis() < intervalTime) {
                                continue;
                            }
                        }
                        //超出特重发数次就不发送,并移除出列表
                        if (sendDatas.SendCount >= reSendCount) {
                            synchronized (SendDatas.sendDataArrayList) {
                                SendDatas.sendDataArrayList.remove(i--);
                            }
                            continue;
                        }
 
                        sendDatas.StartCalendar = Calendar.getInstance();//重置发送时间
                        sendDatas.SendCount++;
 
                        byte[] sendBytes = SerialPortSendAndReceiveUtil.getSendByte(sendDatas.GetSerialPortSendBytes(), 1);
 
                        HDLSerialPortCore.mSerialPort.getOutputStream().write(sendBytes);
                        HDLSerialPortCore.mSerialPort.getOutputStream().flush();
                        SystemClock.sleep(sleepTime);
                    }
 
                    //发送完成后通知读取缓存数据
                    read();
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
 
 
        }
 
        /**
         * 通知读取总线缓存数据
         */
        void read() {
            try {
                byte[] body = new byte[]{(byte) 0xFE, (byte) 0xFE, 0x02, 0x01, 0x00, (byte) 0x9C, 0x6C};
                // 发送Data
                HDLSerialPortCore.mSerialPort.getOutputStream().write(body);
                HDLSerialPortCore.mSerialPort.getOutputStream().flush();
                Log.d("串口-发送->", "发送读取");
            } catch (Exception ignored) {
            }
        }
    }
 
    private static ReceiveData data = new ReceiveData() {
 
        @Override
        public void receiveData(int command, int desSubnetID, int desDeviceID, int subnetID, int deviceID, byte[] usefulBytes) {
            // 更新发送是否成功的信息
            SendDatas.ReceiveBytes(command, subnetID, deviceID, usefulBytes);
            //2019-8-20 增加目标子网号设备号判断
            if ((desSubnetID == 0xFF && desSubnetID == 0xFF)
                    || (desSubnetID == Crc.localSubnetID && desSubnetID == Crc.localDeviceID)) {
                //参数
                UdpDataBean udpDataBean = new UdpDataBean();
                udpDataBean.sourceSubnetID = subnetID;
                udpDataBean.sourceDeviceID = deviceID;
                udpDataBean.desSubnetID = desSubnetID;
                udpDataBean.desDeviceID = desDeviceID;
                udpDataBean.command = command;
 
                udpDataBean.addBytes = usefulBytes;
                HDLSerialPortCore.HandleInsideData(udpDataBean);
 
            }
 
        }
    };
 
    static class ReceiveRunnable implements Runnable {
        /**
         * 线程运行状态
         */
        private boolean run = true;
 
        private byte[] head = new byte[]{(byte) 0xFE, (byte) 0xFE};
 
        /**
         * 缓存区
         */
        private final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
 
        @Override
        public void run() {
            while (run) {
                try {
                    if (HDLSerialPortCore.mSerialPort == null) {
                        Log.i("串口-读取->", "串口没有初始化");
                        continue;
                    }
                    byte[] data = new byte[1024];
                    int size = HDLSerialPortCore.mSerialPort.getInputStream().read(data);
                    if (size <= 0) {
                        continue;
                    }
 
                    try {
                        byteBuffer.put(data, 0, size);
                        Log.d("串口->回复", byteToHex(data, 0, size) + " position:" + byteBuffer.position());
                    } catch (Exception e) {
                        byteBuffer.flip();
                        byteBuffer.clear();
                    }
 
                    while (true) {
                        removeInVoidBytes();
 
                        //没有新的数据,返回
                        if (!haveData()) {
                            break;
                        }
 
 
                        int bodyIndex = 2 + 1 + 1;
                        int lenght = byteBuffer.get(3) & 0xFF;
 
                        byte[] packet = getBody(0, bodyIndex + lenght + 2);
                        //是否已经获取完整所有的数据
                        byte[] tempBytes = getBody(bodyIndex, lenght);
                        remove(bodyIndex + lenght + 2);
 
                        if (!Crc.CRC16_MODBUS_MATCH(packet, packet.length)) {
                            Log.d("串口->回复", "CRC不对");
                            continue;
                        }
                        if (tempBytes == null) {
                            Log.d("串口->回复", "当前数据还没有接收完成");
                            //当前数据还没有接收完成
                            continue;
                        }
 
                        if (lenght <= 1) {
                            //设备回复的协议数据
                            continue;
                        }
 
                        int command = (tempBytes[7] & 0xFF) * 256 + (tempBytes[8] & 0xFF);
 
                        int subnetID = tempBytes[3] & 0xFF;
                        int deviceID = tempBytes[4] & 0xFF;
                        int desSubnetID = tempBytes[9] & 0xFF;
                        int desDeviceID = tempBytes[10] & 0xFF;
                        //如果不是发给自己的数据或者不是广播的数据,不处理
                        if (desSubnetID != Global.subnetID || desDeviceID != Global.deviceID) {
                            if (desSubnetID != 0xFF || desDeviceID != 0xFF) {
                                Log.d("串口->回复", "过滤掉=={目标子网:" + desSubnetID + ",目标设备:" + desDeviceID + "}");
                                continue;
                            }
                        }
 
                        byte[] usefulBytes = new byte[(tempBytes[2] & 0xFF) - 11];
                        // 复制附加数据
                        System.arraycopy(tempBytes, 11, usefulBytes, 0, usefulBytes.length);
                        if (Global.mReceiveData != null) {
                            Global.mReceiveData.receiveData(command, desSubnetID, desDeviceID, subnetID, deviceID, usefulBytes);
                        }
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
 
        private String byteToHex(byte[] bytes, int index, int length) {
            StringBuilder sb = new StringBuilder();
            for (int i = index; i < length; i++) {
                sb.append(String.format(" %02X", bytes[i]));
            }
            return sb.toString();
        }
 
        /**
         * 获取头部数据
         *
         * @return
         */
        boolean haveData() {
 
            int length = 2 + 1 + 1 + (byteBuffer.get(3) & 0xFF) + 2;
 
            //数据还没有接收全
            if (byteBuffer.position() < length) {
                return false;
            }
 
            return true;
        }
 
        /**
         * 获取数据内容
         *
         * @param lenght
         * @return
         */
        byte[] getBody(int index, int lenght) {
            //是否已经获取完整所有的数据
            byte[] bodyBytes = new byte[lenght];
            if (index < 0 || byteBuffer.position() < index + lenght) {
                //当前数据还没有接收完成
                return null;
            }
 
            for (int i = 0; i < bodyBytes.length; i++) {
                bodyBytes[i] = byteBuffer.get(index + i);
            }
            return bodyBytes;
        }
 
        /**
         * 移除可能存在的无效数据
         */
        void removeInVoidBytes() {
            for (int i = 0; i < dddd.length; i++) {
                dddd[i] = byteBuffer.get(i);
            }
//            Log.d("串口->回复", "position:"+byteBuffer.position()+" 处理前的数据"+byteToHex(dddd, 0, 100));
            int index = 0;
            boolean isMatch = false;
            for (; index < byteBuffer.position() - head.length; index++) {
                isMatch = true;
                for (int j = 0; j < head.length; j++) {
                    if (head[j] != byteBuffer.get(index + j)) {
                        isMatch = false;
                        break;
                    }
                }
                if (isMatch) {
                    break;
                }
            }
            int endIndex = 0;
            if (0 < index && isMatch) {
                endIndex = byteBuffer.position();
                byteBuffer.clear();
                for (int i = index; i < endIndex; i++) {
                    byteBuffer.put(byteBuffer.get(i));
                }
            }
 
            for (int i = 0; i < dddd.length; i++) {
                dddd[i] = byteBuffer.get(i);
            }
        }
 
        byte[] dddd = new byte[100];
 
        /**
         * 移除到指定位置前面的数据
         *
         * @param position 指定位置
         */
        void remove(int position) {
            int endIndex = byteBuffer.position();
            byteBuffer.clear();
            //TODO 清空之前的数据
            for (int i = position; i < endIndex; i++) {
                byteBuffer.put(byteBuffer.get(i));
            }
 
            for (int i = 0; i < dddd.length; i++) {
                dddd[i] = byteBuffer.get(i);
            }
 
//            Log.d("串口->回复", "position:"+byteBuffer.position()+" 移除后的数据"+byteToHex(dddd, 0, 100));
        }
    }
 
}