充放电一体机FGCD通信程序【二期初版】
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
package com.dev.fgcd.data;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
 
public class FGCD_Cmd {
 
    public final int BYTE_LEN = 18;
 
    private int SYNCode[] = {0xAA, 0xAA, 0xAA, 0xAA};            //ͬ²½Âë
    public int ByteLen;                                            //×Ö½Ú×ÜÊý
    public int CMD;                                                //ÃüÁîÂë
    public int RecState;                                        //ÏìÓ¦ÐÅÏ¢RTN
    public int Type;                                            //ÏìÓ¦ÐÅÏ¢CID1
    public int WorkState;                                        //ÏìÓ¦ÐÅÏ¢CID2
    public int Alarm;                                            //ÏìÓ¦¸æ¾¯ÖµALM
    public int Db1= 0xFF;                                        //Ô¤Áô1
    public int Db2;                                                //Ô¤Áô2
    public int Db3;                                                //Ô¤Áô3
    public int Db4;                                                //Ô¤Áô4
    private int CRC;
 
    public FGCD_Cmd() {
        // TODO Auto-generated constructor stub
    }
    
    public FGCD_Cmd(int CMD) {
        this.CMD = CMD;
    }
 
    public void makeCmd(int cmd, int byteLen)
    {
        ByteLen = byteLen;
        CMD = cmd;
    }
 
    public boolean putByteBuffer(final ByteBuffer bf)
    {
        if(bf.limit() < BYTE_LEN) {
            return false;
        }
        ByteBuffer tmpbuf = bf;
        int crc0 = tmpbuf.getShort(BYTE_LEN-2) & 0xFFFF;
        tmpbuf.position(BYTE_LEN-2);
        tmpbuf.putShort(FGCD_ComBase.changeIntToShort(0));
        //System.out.println(ComFn.bytesToHexString(bf.array(), bf.array().length));
        int crc1 = FGCD_Crc16.CalCRC16(tmpbuf, tmpbuf.limit());
        if(crc0 != crc1) {
            System.err.println(crc0+"==="+crc1);
            return false;
        }
        tmpbuf.position(0);
        for(int n=0; n<4; n++)
            SYNCode[n] = FGCD_ComBase.changeByteToInt(tmpbuf.get());
        ByteLen = FGCD_ComBase.changeShortToInt(tmpbuf.getShort());                                        //×Ö½Ú×ÜÊý
        CMD    = FGCD_ComBase.changeShortToInt(tmpbuf.getShort());                                            //ÃüÁîÂë
        RecState = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                            //ÏìÓ¦ÐÅÏ¢RTN
        Type = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                                //ÏìÓ¦ÐÅÏ¢CID1
        WorkState = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                            //ÏìÓ¦ÐÅÏ¢CID2
        Alarm = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                                //ÏìÓ¦¸æ¾¯ÖµALM
        Db1 = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                                //Ô¤Áô1
        Db2    = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                                //Ô¤Áô2
        Db3    = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                                //Ô¤Áô3
        Db4    = FGCD_ComBase.changeByteToInt(tmpbuf.get());                                                //Ô¤Áô4
        CRC = FGCD_ComBase.changeShortToInt(tmpbuf.getShort());
        tmpbuf.compact();
        tmpbuf.flip();
 
        return true;
    }
    
    /**
     *     ¶ÁÈ¡µ±Ç°ÏÂÔØ³É¹¦Êý¾Ý¿éºÅ
     * @return
     */
    public int getNowDownLoadIndex() {
        return this.Db3*256 + Db4;
    }
    
    /**
     *     ¶ÁÈ¡µ±Ç°×ܵÄÊý¾Ý¿éÊýÄ¿
     * @return
     */
    public int getTotalDownLoadBlock() {
        int total = this.Db1*256 + this.Db2;
        System.err.println(total);
        return total;
    }
 
    public int createCrc16(ByteBuffer bf) {
        ByteBuffer temp = ByteBuffer.allocate(BYTE_LEN);
        bf.position(0);
        for(int i=0;i<BYTE_LEN-2;i++) {
            temp.put(bf.get());
        }
        temp.putShort(FGCD_ComBase.changeIntToShort(0));
        return FGCD_Crc16.CalCRC16(temp, BYTE_LEN);
    }
 
    public void setByteLen(int byteLen) {
        ByteLen = byteLen;
    }
 
    /**
     *     ¹¹Ôì»ù´¡·¢ËÍÊý¾Ý
     * @return
     */
    public ByteBuffer getByteBuffer(ByteBuffer dataBuffer)
    {
        ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN+dataBuffer.limit());
        bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
        bytebuffer.position(0);
        for(int n=0; n<4; n++)
            bytebuffer.put(FGCD_ComBase.changeIntToByte(SYNCode[n]));
 
        bytebuffer.putShort(FGCD_ComBase.changeIntToShort(BYTE_LEN+dataBuffer.limit()));
        bytebuffer.putShort(FGCD_ComBase.changeIntToShort(CMD));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(RecState));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(Type));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(WorkState));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(Alarm));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(Db1));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(Db2));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(Db3));
        bytebuffer.put(FGCD_ComBase.changeIntToByte(Db4));
        int position = bytebuffer.position();
        bytebuffer.putShort(FGCD_ComBase.changeIntToShort(0));
        bytebuffer.put(dataBuffer.array());
        CRC = FGCD_Crc16.CalCRC16(bytebuffer, bytebuffer.position());
        bytebuffer.position(position);
        bytebuffer.putShort(FGCD_ComBase.changeIntToShort(CRC));
        //bytebuffer.putShort((short)0xA094);
        bytebuffer.position(bytebuffer.limit());
        bytebuffer.flip();
        return bytebuffer;
    }
 
    public int getCheck_dev_id() {
        System.out.println("Db2:"+Db1+"==Db2:"+Db2);
        int check_dev_id = 8059*100000 + (this.Db1<<8) +(this.Db2);
        return check_dev_id;
    }
 
    public int getBYTE_LEN() {
        return BYTE_LEN;
    }
 
    public int[] getSYNCode() {
        return SYNCode;
    }
 
    public int getByteLen() {
        return ByteLen;
    }
 
    public int getCMD() {
        return CMD;
    }
 
    public int getRecState() {
        return RecState;
    }
 
    public int getType() {
        return Type;
    }
 
    public int getWorkState() {
        return WorkState;
    }
 
    public int getAlarm() {
        return Alarm;
    }
 
    public int getDb1() {
        return Db1;
    }
 
    public int getDb2() {
        return Db2;
    }
 
    public int getDb3() {
        return Db3;
    }
 
    public int getDb4() {
        return Db4;
    }
 
    public int getCRC() {
        return CRC;
    }
 
    public void setSYNCode(int[] sYNCode) {
        SYNCode = sYNCode;
    }
 
    public void setCMD(int cMD) {
        CMD = cMD;
    }
 
    public void setRecState(int recState) {
        RecState = recState;
    }
 
    public void setType(int type) {
        Type = type;
    }
 
    public void setWorkState(int workState) {
        WorkState = workState;
    }
 
    public void setAlarm(int alarm) {
        Alarm = alarm;
    }
 
    public void setDb1(int db1) {
        Db1 = db1;
    }
 
    public void setDb2(int db2) {
        Db2 = db2;
    }
 
    public void setDb3(int db3) {
        Db3 = db3;
    }
 
    public void setDb4(int db4) {
        Db4 = db4;
    }
 
    public void setCRC(int cRC) {
        CRC = cRC;
    }
 
    @Override
    public String toString() {
        return "FGCD_Cmd [BYTE_LEN=" + BYTE_LEN + ", SYNCode=" + Arrays.toString(SYNCode) + ", ByteLen=" + ByteLen
                + ", CMD=" + CMD + ", RecState=" + RecState + ", Type=" + Type + ", WorkState=" + WorkState + ", Alarm="
                + Alarm + ", Db1=" + Db1 + ", Db2=" + Db2 + ", Db3=" + Db3 + ", Db4=" + Db4 + ", CRC=" + CRC + "]";
    }
}