FBS9600通信程序,带了均衡电压版本
Administrator
2021-08-12 8467d3be4ce3be340d36f45c83d370def9e7e50f
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
package com.dev.idce8200;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
 
import com.base.Com;
import com.base.Crc16;
import com.battdata_rt.BattData_RT;
import com.battdata_rt.BattData_RT_Array;
 
public class Socket_Server_IDCE8200 extends Thread {
    ServerSocket server = null;
    Socket sk = null;
 
    BattData_RT_Array m_Data;
 
    public Socket_Server_IDCE8200(BattData_RT_Array mdata, int port)
    {
        m_Data = mdata;
        try
        {
            server = new ServerSocket(port);  
        }
        catch (IOException e)  
        {
            e.printStackTrace();  
        }
    }
    
    public void run()
    {
        System.out.println(this.getName() 
                            + " - Socket_Server_IDCE8200 Started, Listenning For IDCE8200 At PortNum " 
                            + server.getLocalPort());
        
        IDCE8200_MonitPro_Thread MonitPro = new IDCE8200_MonitPro_Thread(m_Data);
        MonitPro.start();
        
        while (true) {
            try {
                sk = server.accept();  
                ServerThread th = new ServerThread(sk);
                th.start();
                byte[] ip = sk.getInetAddress().getAddress();
                System.out.println( this.getName() + " - Socket_Server_IDCE8200: "
                                    + String.format("New IDCE8200 Client Connection, IP: %d.%d.%d.%d - ", 
                                                     ip[0]&0xFF,ip[1]&0xFF,ip[2]&0xFF,ip[3]&0xFF)
                                    + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) );
                sleep(50);
            } catch (Exception e) {
                //e.printStackTrace();
                System.err.println(e.getMessage());
                try {
                    sleep(5000);
                } catch (InterruptedException e1) {
                    //e1.printStackTrace();
                }
            }
        }
    }
 
    class ServerThread extends Thread
    {
        Socket sk = null;
        private InputStream mInputStream = null;
        private OutputStream mOutputStream = null;
        
        BattData_RT targetData = null;
        
        public ServerThread(Socket sk)
        {
            this.sk = sk;
            
            try {
                this.sk.setSoTimeout(10000);
                mInputStream = sk.getInputStream();
                mOutputStream = sk.getOutputStream();
                //FBI_Ip = this.sk.getInetAddress().getAddress();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        private void close()
        {
            try {
                sk.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /*
        private boolean read_data(InputStream is, byte[] buf) throws InterruptedException, IOException
        {
            final int rx_sum = buf.length;
            int rx_count = 0;
            
            boolean comm_ok = true;
            for(rx_count=0; rx_count<rx_sum; rx_count++)
            {
                int time_out_count = 0;
                while(is.available() < 1)
                {
                    Thread.sleep(10);
                    time_out_count++;
                    if(time_out_count > 1000)
                    {
                        comm_ok = false;
                        break;
                    }
                }
                
                if(false == comm_ok)
                    break;
                
                buf[rx_count] = (byte) is.read();
                if(rx_count == 1)
                {
                    if(((byte)0xAA != buf[0]) || ((byte)0xAA != buf[1]))
                    {
                        rx_count = 0;
                        continue;
                    }
                }
            }
            
            if(rx_count > 4)
                comm_ok = true;
            
            return comm_ok;
        }
        */
        public void getData(byte[] bf) throws IOException
        {
            mInputStream.read(bf);
        }
        
        public void run()
        {
            try {
                CommData data_in = new CommData();
                CommData data_out = new CommData();
                
                byte[] Station_Ip;
                
                while(true)
                {
                    /*****************************************************************************/
                    /*
                    if(false == read_data(mInputStream, data_in.data_byte))
                        break;
                    */
                    //-----------------------------------------------------//
                    getData(data_in.data_byte);
                    //-----------------------------------------------------//
                    
                    if(false == data_in.getDataFromByteArray())
                    {
                        continue;
                    }
                    if((0xAA != (data_in.syn_bytes[0]&0xFF)) || (0xAA != (data_in.syn_bytes[1]&0xFF)))
                    {
                        continue;
                    }
                    /*****************************************************************************/
                    Station_Ip = data_in.ipAddr;
                    if(null == targetData)
                    {
                        for(int index=0; index<m_Data.getItemCount(); index++)
                        {
                            BattData_RT tmp_targetData = m_Data.getItem(index);
                            if((Station_Ip[0] == tmp_targetData.StationIp[0]) 
                                    && (Station_Ip[1] == tmp_targetData.StationIp[1]) 
                                    && (Station_Ip[2] == tmp_targetData.StationIp[2])
                                    && (Station_Ip[3] == tmp_targetData.StationIp[3])
                                    && (data_in.battGroupNum == tmp_targetData.BattGroupNum)
                                    && (false == tmp_targetData.isMonDataUseForIdce8200()))
                            {
                                targetData = tmp_targetData;
                                targetData.setMonDataUseForIdce8200(true);
                                
                                System.out.println(String.format("IDCE8200 get data ip is %d.%d.%d.%d", 
                                        Station_Ip[0]&0xFF, Station_Ip[1]&0xFF, Station_Ip[2]&0xFF, Station_Ip[3]&0xFF));
                                break;
                            }
                        }
                    }
                    /*****************************************************************************/
                    if(null != targetData)
                    {
                        for(int n=0; n<targetData.MonCount; n++)
                        {
                            targetData.updateMonVolFromIDCE8200(n, ((float)data_in.battData[n])/1000);
                        }
                    }
                    /*****************************************************************************/
                    /*************************** socket output ***********************************/
                    data_out.makeDataToByteArray();
                    mOutputStream.write(data_out.data_byte);
                    mOutputStream.flush();
                }
            } catch (IOException e) {
                System.out.println(this.getName() 
                            + " - Socket_Server_IDCE8200: " 
                            + e.getMessage() + " - "
                            + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
            } finally {
                if(null != targetData) {
                    targetData.setMonDataUseForIdce8200(false);
                }
                close();
                System.out.println(this.getName() 
                                    + " - Socket_Server_IDCE8200: Comm Timeout And Break Down - "
                                    + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
            }
        }
    }
    
    public class CommData {
        final static public int MonCountMax = 300;
        /*****************************************************/
        byte[] syn_bytes = new byte[2];
        short modbus_type;
        short byte_count;
        byte tag;
        byte cmd;
        byte modbus_readcount;
        short battGroupNum;
        byte[] ipAddr = new byte[4];
        short[] battData = new short[480];
        short crc = 0;
        /*****************************************************/
        short mon_count = 0;
        /*****************************************************/
        byte[] data_byte = new byte[1024];
        ByteBuffer data_bf = ByteBuffer.allocate(2048);
        /*****************************************************/
        public CommData()
        {
            syn_bytes[0] = (byte) 0xAA;
            syn_bytes[1] = (byte) 0xAA;
            data_bf.order(ByteOrder.BIG_ENDIAN);
        }
        /*****************************************************/
        
        void copyDataByte(byte[] databyte)
        {
            for(int n=0; n<data_byte.length; n++)
                data_byte[n] = databyte[n];
            
            getDataFromByteArray();
        }
        
        void makeDataToByteArray()
        {
            data_bf.clear();
            
            for(int n=0; n<syn_bytes.length; n++)
                data_bf.put(syn_bytes[n]);
            
            data_bf.putShort(modbus_type);
            data_bf.putShort(byte_count);
            data_bf.put(tag);
            data_bf.put(cmd);
            data_bf.put(modbus_readcount);
            
            for(int n=0; n<ipAddr.length; n++)
                data_bf.put(ipAddr[n]);
            
            data_bf.putShort(battGroupNum);
            
            data_bf.putShort(crc);
            
            data_bf.position(0);
            data_bf.get(data_byte);
            
            crc = (short) Crc16.CalCRC16(data_byte, data_byte.length-2);
            data_byte[data_byte.length-2] = (byte)(crc);
            data_byte[data_byte.length-1] = (byte)(crc>>8);
        }
        /*****************************************************/
        boolean getDataFromByteArray()
        {
            boolean ck_crc_res = false;
            
            data_bf.clear();
            data_bf.put(data_byte);
            data_bf.flip();
            
            for(int n=0; n<syn_bytes.length; n++)
                syn_bytes[n] = data_bf.get();
            
            modbus_type = (short) (data_bf.getShort()&0xFFFF);
            byte_count = (short) (data_bf.getShort()&0xFFFF);
            tag = data_bf.get();
            cmd = data_bf.get();
            modbus_readcount = data_bf.get();
            
            for(int n=0; n<ipAddr.length; n++)
                ipAddr[n] = data_bf.get();
            
            battGroupNum = (short) (data_bf.getShort()&0xFFFF);
            
            for(int n=0; n<MonCountMax; n++)
            {
                battData[n] = (short) (data_bf.getShort()&0xFFFF);
            }
            /*
            if((short)Crc16.CalCRC16(data_byte, data_byte.length-2) == crc)
            {
                ck_crc_res = true;
            }
            */
            ck_crc_res = true;
            return ck_crc_res;
        }
        /*****************************************************/
    }
}