DELL
2025-04-28 e6eb7fb0af366e370f125668d62e89eb0004f517
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
package com.dev.fbs9600_groupvol;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
 
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
 
import javax.swing.JOptionPane;
import javax.swing.JTextField;
 
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.Crc16;
 
public class SPCommGroupVol implements Runnable/*, SerialPortEventListener*/ {
    public static final int COMM_PORT_TYPE_Serial = 0;
    public static final int COMM_PORT_TYPE_Socket = 1;
    
    public static final byte CMD_TYPE_READ = 0x03;
    public static final byte CMD_TYPE_WRITE_Multy = 0x10;
    public static final short UART_MonomerSetAddr_RegAddr = 0001;
    public static final short UART_MonomerAdjOffSet_RegAddr = 0002;    //Æ«ÒÆÁ¿
    public static final short UART_MonomerAdjSlope_RegAddr = 0003;    //бÂÊ
    
    public static final short UART_MonomerVer_RegAddr = 1001;
    
    public static final int DEV_TYPE_MON_SAMD09 = 0;
    public static final int DEV_TYPE_MON_MSP430 = 1;
    public static final int DEV_TYPE_MON_MSP430_RF = 2;
    public static final int DEV_TYPE_MON_SWM = 3;        //»ªÐ¾ÍþÌØmcu
    public static final int DEV_TYPE_Curr = 100;
    public static final int DEV_TYPE_GroupVol = 110;
    public static final int DEV_TYPE_FCF651GroupVol = 111;
    public static final int DEV_TYPE_KW8 = 120;
    public static final int DEV_TYPE_BYBB = 130;
    
    public int m_DEV_TYPE = DEV_TYPE_FCF651GroupVol;
 
    private CommSerialPort m_SP_Comm = null;
    private Comm_Socket m_SocketComm = null;
    // ÊäÈëÊä³öÁ÷
    public InputStream inputStream;
    public OutputStream outputStream;
    // RS-232µÄ´®ÐпÚ
    public SerialPort serialPort;
    
    public boolean CommThreadRunning = false;
    private ByteBuffer CommRxBuffer = ByteBuffer.allocate(512); 
    private ByteBuffer CommTxBuffer = ByteBuffer.allocate(512);
    
    public short comm_tx_cnt = 0;
    public short comm_rx_cnt = 0;
    
    public short comm_OK_cnt = 0;
    public short comm_Err_cnt = 0;
    public boolean dt_new_tag = false;
    
    public int dt_target_addr = 0;
    public short dt_addr = 0;
    public int dt_vol = 0;
    public int dt_version = 0;
    public int dt_volcal_offset = 0;
    public int dt_volcal_slope = 0;
    //----------------------------------------------------------//
    //----------------------------------------------------------//
    
    private short dt_cmd = CMD_TYPE_READ;
    private short dt_cmd_regaddr = 0;
    private short dt_cmd_regcnt = 0;
    private short dt_cmd_data = 0;
    private short dt_cmd_data1 = 0;
    int dt_multy_comm = CommSerialPort.mutycomm_Type_Samd09;
    private short dt_auto_read_regaddr = UART_MonomerVer_RegAddr;
    
    private JTextField addr_TextField = null;
    
    
    private page_debug_inf dt_debug_inf;
    private int m_COMM_PORT_TYPE = 0;
    
    private Logger m_Log = null;
    
    public SPCommGroupVol(int addr_t, int bitrate, int dev_type, 
                            int multy_com_type, page_debug_inf debug, Logger log) {
        dt_target_addr = addr_t & 0xFFFF;
        dt_multy_comm = multy_com_type;
        dt_debug_inf = debug;
        //-------------------------------------------------------------//
        m_DEV_TYPE = dev_type;
        //-------------------------------------------------------------//
        m_Log = log;
    }
    
    public SPCommGroupVol(JTextField addr_t, int bitrate, int dev_type, 
                            int multy_com_type, page_debug_inf debug) {
        //dt_target_addr = addr_t & 0xFFFF;
        addr_TextField = addr_t;
        dt_multy_comm = multy_com_type;
        dt_debug_inf = debug;
        //-------------------------------------------------------------//
        m_DEV_TYPE = dev_type;
        //-------------------------------------------------------------//
    }
    
    public void setCommAddr(int addr_t) {
        dt_target_addr = addr_t & 0xFFFF;
    }
    
    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.order(ByteOrder.BIG_ENDIAN);
            CommRxBuffer.order(ByteOrder.BIG_ENDIAN);
            comm_tx_cnt = 0;
            comm_rx_cnt = 0;
            CommThreadRunning = true;
        }
        
        return res;
    }
    
    private void makeVolModule_CommTxData() {
        CommTxBuffer.clear();
        
        if(null != addr_TextField) {
            dt_target_addr = (short) Integer.parseInt(addr_TextField.getText()) & 0xFFFF;
        }
        
        if(CMD_TYPE_READ == dt_cmd) {
            if(UART_MonomerVer_RegAddr == dt_auto_read_regaddr) {
                dt_auto_read_regaddr = UART_MonomerSetAddr_RegAddr;
            } else {
                dt_auto_read_regaddr = UART_MonomerVer_RegAddr;
            }
            
            CommTxBuffer.put((byte) dt_target_addr);
            CommTxBuffer.put((byte) CMD_TYPE_READ);
            CommTxBuffer.put((byte) (dt_auto_read_regaddr>>8));
            CommTxBuffer.put((byte) dt_auto_read_regaddr);
            CommTxBuffer.put((byte) 0x00);
            CommTxBuffer.put((byte) 0x03);
        } else {
            byte datlen = (byte) (dt_cmd_regcnt*2);
            CommTxBuffer.put((byte) dt_target_addr);
            CommTxBuffer.put((byte) CMD_TYPE_WRITE_Multy);
            CommTxBuffer.put((byte) (dt_cmd_regaddr>>8));
            CommTxBuffer.put((byte) dt_cmd_regaddr);
            CommTxBuffer.put((byte) (dt_cmd_regcnt>>8));
            CommTxBuffer.put((byte) dt_cmd_regcnt);
            CommTxBuffer.put((byte) datlen);
            CommTxBuffer.put((byte) (dt_cmd_data>>8));
            CommTxBuffer.put((byte) dt_cmd_data);
            if(dt_cmd_regcnt > 1) {
                CommTxBuffer.put((byte) (dt_cmd_data1>>8));
                CommTxBuffer.put((byte) dt_cmd_data1);
            }
            dt_cmd_regcnt = 0;
            dt_cmd = CMD_TYPE_READ;
        }
        
        int crc = Crc16.CalCRC16(CommTxBuffer, CommTxBuffer.position()) & 0xFFFF;
        CommTxBuffer.putShort((short) crc);
        CommTxBuffer.flip();
    }
    
    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 = 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, 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_reg_addr, short cmd_reg_cnt, 
                            short cmd_data, short cmd_data1) {
        dt_cmd = cmd;
        dt_cmd_regaddr = cmd_reg_addr;
        dt_cmd_regcnt = cmd_reg_cnt;
        dt_cmd_data = cmd_data;
        dt_cmd_data1 = cmd_data1;
    }
    
    private void processVolModule_RxData(ByteBuffer bbf_rx, short addr_reg) {
        if(false == checkCommBuf(bbf_rx)) {
            return;
        }
        
        if(bbf_rx.limit() >= 7) {
            bbf_rx.position(0);
            dt_addr = (short) (bbf_rx.get() & 0xFF);
            byte cmd_t = bbf_rx.get();//cmd
            bbf_rx.get();//bytes
            if(CMD_TYPE_READ == cmd_t) {
                if(UART_MonomerVer_RegAddr == addr_reg) {
                    dt_version = bbf_rx.getShort() & 0xFFFF;
                    dt_vol = bbf_rx.getShort() & 0xFFFF;
                    dt_vol = (dt_vol) | (((int)(bbf_rx.getShort() & 0xFFFF))<<16);
                } else {
                    bbf_rx.getShort();
                    dt_volcal_offset = bbf_rx.getShort();
                    dt_volcal_slope = bbf_rx.getShort() & 0xFFFF;
                }
            }
            
            if(++comm_rx_cnt > 32500) {
                comm_rx_cnt = 0;
            }
            if(comm_OK_cnt < 1000) {
                comm_OK_cnt += 1;
            }
        }
    }
    
    public void run() {
        while(true == CommThreadRunning) {
            try {
                //---------------------- make tx data --------------------------//
                makeVolModule_CommTxData();
                //----------------------- tx rx data ---------------------------//
                tr_Msg(CommTxBuffer, CommRxBuffer, true);
                //----------------------- process data -------------------------//
                processVolModule_RxData(CommRxBuffer, dt_auto_read_regaddr);
                //-------------------------------------------------------------//
                
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                exitCommPortThread();
                JOptionPane.showMessageDialog(null, "FCF651 GroupVol Module CommPort ´®¿ÚÒì³£: " + e.getMessage());
            }
        }
        /******************************************************************************/
        closeCommPort();
        /******************************************************************************/
        dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                    + " CommPort is quit, Dev Id: " + m_DEV_TYPE 
                                    + ", COMM_PORT_TYPE: " + m_COMM_PORT_TYPE + "\n");
        /******************************************************************************/
    }
}