mxpopstar
2022-05-03 e75ef5f04f61aa5fbd89fd5c413dcee1819b7a91
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
package com.dev.nrf_lora_tester;
 
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Date;
 
import javax.swing.JOptionPane;
 
import org.apache.logging.log4j.Logger;
 
import sp_comm.CommSerialPort;
import sp_comm.Comm_Socket;
import main.page_debug_inf;
 
import com.Com;
import com.dev.ntm.cmd.NTM_Crc16;
 
public class SPCommLora implements Runnable/*, SerialPortEventListener*/ {
    public static final int COMM_PORT_TYPE_Serial = 0;
    public static final int COMM_PORT_TYPE_Socket = 1;
    
    public static final short CMD_Read_Data = 0x00A0;
    public static final short CMD_Set_ID = 0x00A2;
    public static final short CMD_Get_Ver = 0x00A4;
    public static final short CMD_Set_RFCH = 0x00A8;
    public static final short CMD_Cal_VolOffset = 0x00B1;
    public static final short CMD_Cal_VolSlope = 0x00B2;
    
    private CommSerialPort m_SP_Comm = null;
    private Comm_Socket m_SocketComm = null;
    public ArrayList<LoraModuleData> alLoraData = new ArrayList<LoraModuleData>(0);
    public int[] m_SetModuleRFCH = new int[300];
    public boolean CommThreadRunning = false;
    private ByteBuffer CommRxBuffer = null; 
    private ByteBuffer CommTxBuffer = null;
    
    public short comm_tx_cnt = 0;
    public short comm_rx_cnt = 0;
    
    public int dt_target_addr = 0;
    public int dt_target_ver = 0;
    public int dt_target_ver2 = 0;
    public int dt_target_ver3 = 0;
    
    public int lora_start_addr = 1;
    public int lora_module_cnt = 2;
    public int lora_rf_ch_start = 0x17;
    public int lora_rf_ch = lora_rf_ch_start;
    public int lora_comm_time = 200;
    private int lora_comm_time_origin = 200;
    private int m_comm_error_count = 0;
    
    private short dt_usr_cmd = 0;
    private short dt_usr_cmd_data = 0;
    
    private boolean dt_normal_comm_en = true;
    int dt_multy_comm_type = CommSerialPort.mutycomm_Type_Samd09;
    
    private page_debug_inf dt_debug_inf;
    private int m_COMM_PORT_TYPE = 0;
    
    private Logger m_Log = null;
    
    public SPCommLora(int startaddr_t, int module_cnt, int rf_ch, int comm_time,
                        int bitrate, int multy_com_type, page_debug_inf debug, Logger log) {
        
        lora_start_addr = startaddr_t & 0xFFFF;
        dt_target_addr = lora_start_addr;
        lora_module_cnt = module_cnt;
        lora_rf_ch_start = rf_ch;
        lora_comm_time = comm_time/module_cnt;
        if(lora_comm_time < 50) {
            lora_comm_time = 50;
        }
        lora_comm_time_origin = lora_comm_time;
        
        dt_multy_comm_type = multy_com_type;
        dt_debug_inf = debug;
        
        if(lora_module_cnt < 1) {
            lora_module_cnt = 1;
        }
        if(lora_module_cnt > 80) {
            lora_module_cnt = 80;
        }
        for(int n=0; n<(lora_module_cnt+2); n++) {
            alLoraData.add(new LoraModuleData());
        }
        
        m_Log = log;
    }
    
    public void clearSetModuleRFCH() {
        for(int n=0; n<m_SetModuleRFCH.length; n++) {
            m_SetModuleRFCH[n] = 0;
        }
    }
    
    public void setCommBautrate(int bt_rate) {
        if(COMM_PORT_TYPE_Serial != m_COMM_PORT_TYPE) {
            return;
        }
        
        try {
            m_SP_Comm.serialPort.setSerialPortParams(bt_rate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //³õʼ»¯´®¿Ú
    public boolean OpenCommPort(int comm_port_type, String comm_name_or_ip, int bitrate_or_comm_port) {
        boolean res = false;
        m_COMM_PORT_TYPE = comm_port_type;
        if(COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
            m_SP_Comm = new CommSerialPort(bitrate_or_comm_port, m_Log);
            res = m_SP_Comm.OpenCommPort(comm_name_or_ip);
        } else {
            m_SocketComm = new Comm_Socket(comm_name_or_ip, bitrate_or_comm_port);
            res = true;
        }
        
        if(true == res) {
            CommTxBuffer = ByteBuffer.allocate(120);
            CommRxBuffer = ByteBuffer.allocate(120);
            CommTxBuffer.order(ByteOrder.LITTLE_ENDIAN);
            CommRxBuffer.order(ByteOrder.LITTLE_ENDIAN);
            
            comm_tx_cnt = 0;
            comm_rx_cnt = 0;
            CommThreadRunning = true;
        }
        
        return res;
    }
    
    public void setCommErrCnt(int val) {
        m_comm_error_count = val;
    }
    public int getCommErrCnt() {
        return m_comm_error_count;
    }
    
    public void setModuleCommTimeout(int t_out) {
        lora_comm_time = t_out;
        if(lora_comm_time < 50) {
            lora_comm_time = 50;
        }
    }
    public void setModuleCommTimeoutToOrigin() {
        lora_comm_time = lora_comm_time_origin;
    }
    
    private int makeCommTxData() {
        int a_tar_addr = 0;
        CommTxBuffer.clear();
        
        ByteBuffer tmp_bf = ByteBuffer.allocate(64);
        tmp_bf.order(ByteOrder.LITTLE_ENDIAN);
        
        {
            a_tar_addr = dt_target_addr;
            dt_target_addr += 1;
            if(dt_target_addr >= (lora_module_cnt+lora_start_addr)) {
                dt_target_addr = lora_start_addr;
            }
            
            CommTxBuffer.put((byte) ((a_tar_addr>>8)&0xFF));
            CommTxBuffer.put((byte) (a_tar_addr&0xFF));
            lora_rf_ch = a_tar_addr+lora_rf_ch_start;
            if(lora_rf_ch > 0) {
                lora_rf_ch -= 1;
            }
            CommTxBuffer.put((byte) (lora_rf_ch&0xFF));
            
            int index = a_tar_addr-lora_start_addr;
            if(index < 0) {
                index = 0;
            }
            LoraModuleData lrData = alLoraData.get(index%alLoraData.size());
            lrData.tx_cnt += 1;
            
            short cmd = CMD_Read_Data;
            if(0 == dt_target_ver) {
                cmd = CMD_Get_Ver;
            }
            if(dt_usr_cmd > CMD_Read_Data) {
                cmd = dt_usr_cmd;
                dt_usr_cmd = CMD_Read_Data;
            }
            
            tmp_bf.put((byte) 0xAB);
            tmp_bf.put((byte) 0xCD);
            tmp_bf.put((byte) 0x52);//ÉÏλ»úµ÷ÊÔÈí¼þ¹Ì¶¨ÎªµÚ82 loraÐŵÀ
            tmp_bf.put((byte) 0xF0);
            tmp_bf.putShort(cmd);
            
            if((CMD_Read_Data==cmd) || (CMD_Get_Ver==cmd)) {
                for(int n=0; n<4; n++) {
                    tmp_bf.putShort((short) 0x00);
                }
            } else {
                switch(cmd) {
                    case CMD_Set_ID:
                    case CMD_Set_RFCH: {
                        tmp_bf.putShort((short) 0xCDAB);
                        tmp_bf.putShort((short) ((dt_usr_cmd_data<<8) | (dt_usr_cmd_data>>8)));
                        tmp_bf.putShort((short) 0x00);
                        tmp_bf.putShort((short) 0x00);
                    }
                    break;
                    
                    case CMD_Cal_VolOffset:
                    case CMD_Cal_VolSlope: {
                        tmp_bf.putShort((short) (dt_usr_cmd_data));
                        tmp_bf.putShort((short) 0x00);
                        tmp_bf.putShort((short) 0x00);
                        tmp_bf.putShort((short) 0x00);
                    }
                    break;
                }
            }
        } 
        
        int crc = NTM_Crc16.CalCRC16(tmp_bf, tmp_bf.position()) & 0xFFFF;
        tmp_bf.putShort((short)crc);
        tmp_bf.flip();
        CommTxBuffer.put(tmp_bf);
        
        CommTxBuffer.flip();
        
        return a_tar_addr;
    }
    
    private boolean checkCommBuf(ByteBuffer buf_t) {
        boolean res = false;
        if(buf_t.limit() >= 5) {
            buf_t.position(buf_t.limit()-2);
            int crc = buf_t.getShort() & 0xFFFF;
            //crc = ((crc>>8)|(crc<<8)) & 0xFFFF;
            int crc_cal = NTM_Crc16.CalCRC16(buf_t, buf_t.limit()-2) & 0xFFFF;
            
            /*************************************************/
            if(crc_cal == crc) {
                res = true;
            }
            /*************************************************/
        } else {
            res = false;
        }
        
        return res;
    }
    
    public void tr_Msg(ByteBuffer bbf_tx, ByteBuffer bbf_rx, boolean showdat) {
        if(++comm_tx_cnt > 32500) {
            comm_tx_cnt = 0;
        }
        if(COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
            m_SP_Comm.SP_Comm(bbf_tx, bbf_rx, dt_multy_comm_type, dt_debug_inf, showdat);
        } else {
            m_SocketComm.SocketComm(bbf_tx, bbf_rx, dt_debug_inf, showdat);
        }
    }
    
    private void closeCommPort() {
        if(COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
            m_SP_Comm.serialPort.close();
        } else {
            m_SocketComm.socketClose();
        }
    }
    
    public void exitCommPortThread() {
        CommThreadRunning = false;
    }
    
    public void setCommCmd(short cmd, short cmd_data) {
        dt_usr_cmd = cmd;
        dt_usr_cmd_data = cmd_data;
    }
    
    public void setNormalCommState(boolean stat) {
        dt_normal_comm_en = stat;
    }
    
    public void setModule_Addr_Count(int addr, int rfch, int cnt) {
        lora_start_addr = addr & 0xFFFF;
        dt_target_addr = lora_start_addr;
        
        lora_rf_ch_start = rfch;
        lora_rf_ch = lora_rf_ch_start;
        
        lora_module_cnt = cnt;
    }
    
    private void processRxData(ByteBuffer bbf_rx, int start_addr, int soruce_addr) {
        if(false == checkCommBuf(bbf_rx)) {
            if(m_comm_error_count < 100) {
                m_comm_error_count += 1;
            }
            if((1==lora_module_cnt) && (m_comm_error_count>=3)) {
                dt_target_ver = 0;
            }
            
            return;
        }
        @SuppressWarnings("unused")
        byte ab,cd;
        if(bbf_rx.limit() > 12) {
            bbf_rx.position(0);
            ab = bbf_rx.get();
            cd = bbf_rx.get();
            short addr = (short) (bbf_rx.getShort()&0xFFFF);
            addr = (short) ((addr>>8) | (addr<<8));
            short cmd = (short) (bbf_rx.getShort()&0xFFFF);
            
            int index = addr-start_addr;
            if(index >= 0) {
                LoraModuleData lrData = alLoraData.get(index%alLoraData.size());
                if((SPCommLora.CMD_Read_Data == cmd) && (soruce_addr == addr)) {
                    for(int n=0; n<4; n++) {
                        int dat = bbf_rx.getShort()&0xFFFF;
                        //dat = (short) ((dat>>8)|(dat<<8));
                        lrData.vol[n] = ((float)(dat&0xFFFF)) / 1000;
                    }
                    lrData.rx_cnt += 1;
                } else if((SPCommLora.CMD_Get_Ver == cmd) && (soruce_addr == addr)) {
                    dt_target_ver = bbf_rx.getShort()&0xFFFF;
                    dt_target_ver2 = bbf_rx.getShort()&0xFFFF;
                    dt_target_ver3 = bbf_rx.getShort()&0xFFFF;
                    lrData.rx_cnt += 1;
                }
            }
            
            if(SPCommLora.CMD_Set_RFCH == cmd) {
                m_SetModuleRFCH[addr] = SPCommLora.CMD_Set_RFCH;
            } else if(SPCommLora.CMD_Set_ID == cmd) {
                m_SetModuleRFCH[0] = SPCommLora.CMD_Set_ID;
            }
            
            if(++comm_rx_cnt > 32500) {
                comm_rx_cnt = 0;
            }
            m_comm_error_count = 0;
        }
    }
    
    public void run() {
        while(true == CommThreadRunning) {
            try {
                if(false == dt_normal_comm_en) {
                    Thread.sleep(100);
                    continue;
                }
                
                //---------------------- make tx data --------------------------//
                int tar_addr = makeCommTxData();
                //----------------------- tx rx data ---------------------------//
                tr_Msg(CommTxBuffer, CommRxBuffer, true);
                //----------------------- process data -------------------------//
                processRxData(CommRxBuffer, lora_start_addr, tar_addr);
                //-------------------------------------------------------------//
                
                Thread.sleep(lora_comm_time);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                exitCommPortThread();
                JOptionPane.showMessageDialog(null, "Monomer CommPort ´®¿ÚÒì³£: " + e.getMessage());
            }
        }
        /******************************************************************************/
        closeCommPort();
        /******************************************************************************/
        dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                    + " CommPort is quit"
                                    + ", COMM_PORT_TYPE: " + m_COMM_PORT_TYPE + "\n");
        /******************************************************************************/
    }
}