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 + "]";
|
}
|
}
|