JLChen
2021-03-16 3617e6eecac94965554487afc50d39b0584324fb
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
390
391
392
393
394
395
396
397
398
399
400
package android.serialport.api.sample;
 
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import android.os.Message;
import android.widget.Toast;
import sendData.Commands;
import sendData.SendDatas;
 
import java.security.InvalidParameterException;
 
public class ReceiveAndSend {
 
    public static List<byte[]> SendDatasList = new ArrayList<byte[]>();
 
    public static void Start() {
 
        getSerialPort();
 
        new SendThread().start();
 
        new SendDatasThread().start();
 
        new ReadThread().start();
    }
 
    /**
     * Open the serial port, input and output streams
     */
    public static void getSerialPort() {
 
        Global.mApplication = new Application();
 
        try {
 
            Global.mSerialPort = Global.mApplication.getSerialPort();
            Global.mOutputStream = Global.mSerialPort.getOutputStream();
            Global.mInputStream = Global.mSerialPort.getInputStream();
            if (MainActivity.CuttentActivity.mFragment_BUS != null) {
                Global.BUS bus = new Global.BUS();
                Message msg = new Message();
                msg.obj = bus;
 
                bus.index = 11;
                bus.value = 1;
                bus.value2 = 1;
 
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
            }
        } catch (SecurityException e) {
            if (MainActivity.CuttentActivity.mFragment_BUS != null) {
                Global.BUS bus = new Global.BUS();
                Message msg = new Message();
                msg.obj = bus;
 
                bus.index = 11;
                bus.value = 0;
                bus.value2 = 0;
 
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
                msg = new Message();
                bus.index = -1;
                bus.str = "You do not have read/write permission to the serial port.";
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
            }
        } catch (IOException e) {
 
            if (MainActivity.CuttentActivity.mFragment_BUS != null) {
                Global.BUS bus = new Global.BUS();
                Message msg = new Message();
                msg.obj = bus;
 
                bus.index = 11;
                bus.value = 0;
                bus.value2 = 0;
 
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
                msg = new Message();
                bus.index = -1;
                bus.str = "The serial port can not be opened for an unknown reason.";
 
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
            }
        } catch (InvalidParameterException e) {
 
            if (MainActivity.CuttentActivity.mFragment_BUS != null) {
                Global.BUS bus = new Global.BUS();
                Message msg = new Message();
                msg.obj = bus;
 
                bus.index = 11;
                bus.value = 0;
                bus.value2 = 0;
 
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
                msg = new Message();
                bus.index = -1;
                bus.str = "error_configuration";
 
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
            }
        }
    }
 
    /**
     * Read data thread
     * 
     * @author 2017年12月19日
     */
    private static class ReadThread extends Thread {
 
        @Override
        public void run() {
            super.run();
 
            while (!isInterrupted()) {
                int size = 0;
 
                try {
                    byte[] buffer = new byte[64];
                    if (Global.mInputStream == null) {
                        return;
                    }
                    size = Global.mInputStream.read(buffer);
 
                    // Log.i("asdfg", Integer.toHexString(((buffer[7] & 0xFF) *
                    // 256) + (buffer[8] & 0xFF)) + "");
 
                    for (int i = 0; i < size - 1; i++) {
 
                        if (i != 0) {
                            if ((buffer[i] & 0xFF) == 0xAA && (buffer[i + 1] & 0xFF) == 0xAA) {
                                //
                                byte[] _bf = new byte[i];
                                System.arraycopy(buffer, 0, _bf, 0, i);
 
                                if (_bf.length >= 13) {
                                    int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF);
                                    if (command == Commands.ReadTimeFeedback.getCommand()
                                            || command == Commands.DryContactInitiativeToSend.getCommand()
                                            || command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand()
                                            || command == Commands.InductorState.getCommand()
                                            || command == Commands.SensorBack.getCommand()
                                            || command == Commands.ClickState.getCommand()
                                            || command == Commands.ReadID_ACK.getCommand()
                                            || command == Commands.WriteID_ACK.getCommand()
                                            || command == Commands.DataRequest.getCommand()
                                            || command == Commands.readRemark_ACK.getCommand()) {
                                        onDataReceived(buffer, size);
                                    }
                                }
 
                                byte[] _bf2 = new byte[size - i];
                                System.arraycopy(buffer, i, _bf2, 0, size - i);
                                if (_bf2.length >= 13) {
                                    int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF);
                                    if (command == Commands.ReadTimeFeedback.getCommand()
                                            || command == Commands.DryContactInitiativeToSend.getCommand()
                                            || command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand()
                                            || command == Commands.InductorState.getCommand()
                                            || command == Commands.SensorBack.getCommand()
                                            || command == Commands.ClickState.getCommand()
                                            || command == Commands.ReadID_ACK.getCommand()
                                            || command == Commands.WriteID_ACK.getCommand()
                                            || command == Commands.DataRequest.getCommand()
                                            || command == Commands.readRemark_ACK.getCommand()) {
                                        onDataReceived(buffer, size);
                                    }
                                }
                            }
                        } else {
                            if (size >= 13) {
                                int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF);
                                if (command == Commands.ReadTimeFeedback.getCommand()
                                        || command == Commands.DryContactInitiativeToSend.getCommand()
                                        || command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand()
                                        || command == Commands.InductorState.getCommand()
                                        || command == Commands.SensorBack.getCommand()
                                        || command == Commands.ClickState.getCommand()
                                        || command == Commands.ReadID_ACK.getCommand()
                                        || command == Commands.WriteID_ACK.getCommand()
                                        || command == Commands.DataRequest.getCommand()
                                        || command == Commands.readRemark_ACK.getCommand()) {
                                    onDataReceived(buffer, size);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }
 
    private static void onDataReceived(final byte[] buffer, final int size) {
 
        if (MainActivity.CuttentActivity.mFragment_BUS == null)
            return;
 
        Global.BUS bus = new Global.BUS();
        Message msg = new Message();
        try {
 
            int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF);
 
            if (command == Commands.ReadTimeFeedback.getCommand()) {
                //
            } else if (command == Commands.DryContactInitiativeToSend.getCommand()) {
 
                try {
                    Global.mOutputStream
                            .write(SendDatas.AddSendData(Commands.DryContactInitiativeToSendFeedback.getCommand(),
                                    Global.SubNet_Id, Global.Device_Id, new byte[] { (byte) 0xf8 }));
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
                bus.index = 1;
                bus.contact1 = buffer[11] & 0xFF;
                bus.contact2 = buffer[12] & 0xFF;
                bus.contact3 = buffer[13] & 0xFF;
                bus.contact4 = buffer[14] & 0xFF;
                bus.contact5 = buffer[15] & 0xFF;
                bus.contact6 = buffer[16] & 0xFF;
 
                msg.obj = bus;
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
            } else if (command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand()) {// 读取温度
 
                int value = (int) (buffer[11] & 0xFF);
 
                if ((buffer[13] & 0xFF) == 0) {
                    // 单位摄氏度
                    bus.index = 7;
                    bus.value = 0;
                    bus.value2 = value;
 
                    msg.obj = bus;
                    MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
                } else {
                    // 华氏度
                    bus.index = 7;
                    bus.value = 1;
                    bus.value2 = value;
 
                    msg.obj = bus;
                    MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
                }
 
            } else if (command == Commands.SensorBack.getCommand()) {
                // 传感器灵敏度
                bus.index = 8;
                bus.value = (int) (buffer[12] & 0xFF);
                bus.value2 = (int) (buffer[12] & 0xFF);
 
                msg.obj = bus;
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
            } else if (command == Commands.InductorState.getCommand()) {
 
                bus.index = 9;
                bus.value = 0;
                bus.value2 = 0;
 
                msg.obj = bus;
                MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
 
            } else if (command == Commands.ClickState.getCommand()) {
                int key = buffer[12] & 0xFF;
                switch (key) {
                case 0:// 弹起
                    bus.index = 109;
                    bus.value = 0;
                    bus.value2 = 0;
 
                    msg.obj = bus;
                    MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
                    break;
                case 1:// 按下
                    bus.index = 10;
                    bus.value = 1;
                    bus.value2 = 0;
 
                    msg.obj = bus;
                    MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
                    break;
                case 2:// 长按
                    bus.index = 10;
                    bus.value = 2;
                    bus.value2 = 0;
 
                    msg.obj = bus;
                    MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg);
                    break;
                }
            } else if (command == Commands.SET_TEMP_COMPENSATE_VALUE_ACK.getCommand()) {
                if ((buffer[11] & 0xFF) == 0xF5) {
                    Toast.makeText(MainActivity.CuttentActivity, "set compensate value failed", Toast.LENGTH_SHORT)
                            .show();
                }
            } else if (command == Commands.WriteID_ACK.getCommand()) {
                // 写入ID返回
                if ((buffer[11] & 0xFF) == 0xF5) {
                    NavigationDrawerFragment.mNavigationDrawerFragment.mHandler.sendEmptyMessage(0);
                } else if ((buffer[11] & 0xFF) == 0xF8) {
                    NavigationDrawerFragment.mNavigationDrawerFragment.mHandler.sendEmptyMessage(1);
                }
 
            } else if (command == Commands.ReadID_ACK.getCommand()) {
                // 读取返回
                bus.value = buffer[11] & 0xFF;
                bus.value2 = buffer[12] & 0xFF;
 
                msg.obj = bus;
                NavigationDrawerFragment.mNavigationDrawerFragment.mHandler_Data.sendMessage(msg);
            } else if (command == Commands.DataRequest.getCommand()) {
                // 读取备注返回 / 设备升级 0x15FE buffer[11] buffer[12] 都为0的话,就是备注
                // Log.i("asdfg", "设备升级 0x15FE");
                int index = (buffer[11] & 0xFF) * 256 + (buffer[12] & 0xFF);// 请求的是第几条包
                Fragment_Update.mFragment_Update.mHandler.sendEmptyMessage(index);
            }
 
        } catch (Exception e) {
            e.getMessage();
        }
    }
 
    /**
     * 2015.12.10 读取温度
     */
    private static class SendThread extends Thread {
 
        @Override
        public void run() {
            super.run();
            while (true) {
                try {
                    byte[] sendBytes = SendDatas.AddSendData(Commands.READ_TEMP_WITH_DRY1.getCommand(),
                            Global.SubNet_Id, Global.Device_Id, new byte[] {});
                    Global.mOutputStream.flush();
                    Global.mOutputStream.write(sendBytes);
                    sleep(1000);
                } catch (Exception e) {
                    //
                    continue;
                }
            }
        }
    }
 
    private static class SendDatasThread extends Thread {
 
        @Override
        public void run() {
            super.run();
 
            while (true) {
 
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.getMessage();
                }
 
                for (int i = 0; i < SendDatasList.size(); i++) {
 
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
 
                    }
                    try {
                        byte[] sendBytes = SendDatasList.get(i);
                        try {
 
                            Global.mOutputStream.flush();
                            Global.mOutputStream.write(sendBytes);
 
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        SendDatasList.remove(i);
                        i--;
                    } catch (Exception ex) {
                        ex.getMessage();
                    }
                }
            }
        }
    }
}