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
package com.util;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
public class FBS_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 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(FBS_ComBase.changeIntToShort(0));
        //System.out.println(ComFn.bytesToHexString(bf.array(), bf.array().length));;
        int crc1 = FBS_Crc16.CalCRC16(tmpbuf, tmpbuf.limit());
        //int crc1 = createCrc16(bf);
        if(crc0 != crc1) {
            System.err.println(crc0+"==="+crc1);
            return false;
        }
        tmpbuf.position(0);
        for(int n=0; n<4; n++)
            SYNCode[n] = FBS_ComBase.changeByteToInt(tmpbuf.get());
        ByteLen = FBS_ComBase.changeShortToInt(tmpbuf.getShort());                                        //字节总数
        CMD    = FBS_ComBase.changeShortToInt(tmpbuf.getShort());                                            //命令码
        RecState = FBS_ComBase.changeByteToInt(tmpbuf.get());                                            //响应信息RTN
        Type = FBS_ComBase.changeByteToInt(tmpbuf.get());                                                //响应信息CID1
        WorkState = FBS_ComBase.changeByteToInt(tmpbuf.get());                                            //响应信息CID2
        Alarm = FBS_ComBase.changeByteToInt(tmpbuf.get());                                                //响应告警值ALM
        Db1 = FBS_ComBase.changeByteToInt(tmpbuf.get());                                                //预留1
        Db2    = FBS_ComBase.changeByteToInt(tmpbuf.get());                                                //预留2
        Db3    = FBS_ComBase.changeByteToInt(tmpbuf.get());                                                //预留3
        Db4    = FBS_ComBase.changeByteToInt(tmpbuf.get());                                                //预留4
        CRC = FBS_ComBase.changeShortToInt(tmpbuf.getShort());
        tmpbuf.compact();
        tmpbuf.flip();
 
        return true;
    }
 
    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(FBS_ComBase.changeIntToShort(0));
        return FBS_Crc16.CalCRC16(temp, BYTE_LEN);
    }
 
    /**
     *     构造基础发送数据
     * @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(FBS_ComBase.changeIntToByte(SYNCode[n]));
 
        bytebuffer.putShort(FBS_ComBase.changeIntToShort(BYTE_LEN+dataBuffer.limit()));
        bytebuffer.putShort(FBS_ComBase.changeIntToShort(CMD));
        bytebuffer.put(FBS_ComBase.changeIntToByte(RecState));
        bytebuffer.put(FBS_ComBase.changeIntToByte(Type));
        bytebuffer.put(FBS_ComBase.changeIntToByte(WorkState));
        bytebuffer.put(FBS_ComBase.changeIntToByte(Alarm));
        bytebuffer.put(FBS_ComBase.changeIntToByte(Db1));
        bytebuffer.put(FBS_ComBase.changeIntToByte(Db2));
        bytebuffer.put(FBS_ComBase.changeIntToByte(Db3));
        bytebuffer.put(FBS_ComBase.changeIntToByte(Db4));
        int position = bytebuffer.position();
        bytebuffer.putShort(FBS_ComBase.changeIntToShort(0));
        bytebuffer.put(dataBuffer.array());
        CRC = FBS_Crc16.CalCRC16(bytebuffer, bytebuffer.position());
        bytebuffer.position(position);
        bytebuffer.putShort(FBS_ComBase.changeIntToShort(CRC));
        //bytebuffer.putShort((short)0xA094);
        bytebuffer.position(bytebuffer.limit());
        bytebuffer.flip();
        return bytebuffer;
    }
}