package com.dev.fbs9100;
|
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.util.Arrays;
|
|
public class FBS9100_Cmd
|
{
|
public final int BYTE_LEN = 10;
|
|
private int SYNCode[] = {0xAA, 0xAA, 0xAA, 0xAA};
|
private int ByteCount;
|
public int CMD;
|
public int RES_Index;
|
private int CRC;
|
|
public void makeCmd(int addr, int cmd, int bytecount)
|
{
|
ByteCount = 4 + bytecount;
|
CMD = cmd;
|
RES_Index = addr;
|
}
|
|
public boolean putByteBuffer(final ByteBuffer bf)
|
{
|
if(bf.limit() < BYTE_LEN)
|
return false;
|
|
ByteBuffer tmpbuf = bf;
|
int crc0 = tmpbuf.getShort(BYTE_LEN-2) & 0xFFFF;
|
int crc1 = FBS9100_Crc16.CalCRC16(tmpbuf, BYTE_LEN-2);
|
if(crc0 != crc1)
|
return false;
|
|
tmpbuf.position(0);
|
for(int n=0; n<4; n++){
|
SYNCode[n] = FBS9100_ComBase.changeByteToInt(tmpbuf.get());
|
|
}
|
ByteCount = FBS9100_ComBase.changeShortToInt(tmpbuf.getShort());
|
CMD = FBS9100_ComBase.changeByteToInt(tmpbuf.get());
|
RES_Index = FBS9100_ComBase.changeByteToInt(tmpbuf.get());
|
CRC = FBS9100_ComBase.changeShortToInt(tmpbuf.getShort());
|
|
tmpbuf.compact();
|
tmpbuf.flip();
|
|
return true;
|
}
|
|
public ByteBuffer getByteBuffer()
|
{
|
ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN);
|
bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
|
|
for(int n=0; n<4; n++){
|
//System.out.println(FBS9100_ComBase.changeIntToByte(SYNCode[n]));
|
bytebuffer.put(FBS9100_ComBase.changeIntToByte(SYNCode[n]));
|
}
|
bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(ByteCount));
|
bytebuffer.put(FBS9100_ComBase.changeIntToByte(CMD));
|
bytebuffer.put(FBS9100_ComBase.changeIntToByte(RES_Index));
|
|
CRC = FBS9100_Crc16.CalCRC16(bytebuffer, bytebuffer.position());
|
bytebuffer.putShort(FBS9100_ComBase.changeIntToShort(CRC));
|
bytebuffer.flip();
|
|
return bytebuffer;
|
}
|
|
@Override
|
public String toString() {
|
return "FBS9100_Cmd [BYTE_LEN=" + BYTE_LEN + ", SYNCode=" + Arrays.toString(SYNCode) + ", ByteCount="
|
+ ByteCount + ", CMD=" + CMD + ", RES_Index=" + RES_Index + ", CRC=" + CRC + "]";
|
}
|
|
|
|
}
|
/***************************************************************************************
|
******************************** end of file (FBS_Cmd) *********************************
|
***************************************************************************************/
|