package com.dev.fbs9009;
|
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
import com.base.ComBase;
|
import com.base.Crc16;
|
import com.dev.fbs9100.ComFn;
|
|
public class BaseData {
|
public final int BYTE_LEN_03 = 8;
|
public final int BYTE_LEN_10 = 9;
|
|
private int dev_addr=255; //É豸µØÖ·
|
private int ByteCount; //¶ÁÈ¡¼Ä´æÆ÷ÊýÁ¿
|
public int CMD; //ÃüÁî
|
public int RES_Index; //Êý¾ÝÆðʼµØÖ·
|
|
|
public LD_param ld_param;
|
|
public ConcentratorParam concentratorparam; //»ã¼¯Æ÷²ÎÊý
|
private int CRC; //CRCУÑéÂë
|
|
public void makeCmd(int res_index, int cmd, int bytecount)
|
{
|
//this.dev_addr = dev_addr;
|
this.ByteCount = bytecount;
|
this.CMD = cmd;
|
this.RES_Index = res_index;
|
}
|
|
//¶ÁÈ¡»ñÈ¡µÄÊý¾Ý
|
public boolean putByteBuffer(final ByteBuffer bf)
|
{
|
ByteBuffer tmpbuf = bf;
|
int crc0 = tmpbuf.getShort(bf.limit() - 2)& 0xFFFF;
|
int crc1 = Crc16.CalCRC16(tmpbuf, bf.limit()-2)& 0xFFFF;
|
|
if(crc0 != crc1) {
|
//System.err.println("crc0:"+crc0+"\t crc1:"+crc1);
|
return false;
|
}
|
tmpbuf.position(0);
|
int dev_real_addr = ComBase.changeByteToInt(tmpbuf.get()); //É豸µÄʵ¼ÊµØÖ·
|
this.CMD = ComBase.changeByteToInt(tmpbuf.get()); //ÃüÁî¶ÔÓ¦µÄ¶ÁдÃüÁî
|
this.RES_Index = ComBase.changeShortToInt(tmpbuf.getShort()); //»ñÈ¡¼Ä´æÆ÷µÄÆðʼλÖÃ
|
this.ByteCount = ComBase.changeByteToInt(tmpbuf.get()); //Êý¾ÝÇø×Ö½ÚÊý
|
//tmpbuf.position(0);
|
//this.dev_addr = FBS9100_ComBase.changeShortToInt(tmpbuf.get());
|
//CMD = FBS9100_ComBase.changeByteToInt(tmpbuf.get());
|
|
//CRC = FBS9100_ComBase.changeShortToInt(tmpbuf.getShort(bf.limit()-2));
|
//tmpbuf.compact();
|
//tmpbuf.flip();
|
return true;
|
}
|
|
//½«¶ÁÈ¡µÄÊý¾Ýת³ÉByteBufferÊý¾Ý
|
public ByteBuffer getByteBuffer()
|
{ ByteBuffer bytebuffer = ByteBuffer.allocate(0);;
|
if(CMD == MyModBusCom.CMD_READ_MODBUS){
|
bytebuffer=getByteBuffer_03();
|
}else if(CMD == MyModBusCom.CMD_MULTI_WRITE_MODBUS){
|
bytebuffer=getByteBuffer_10();
|
}
|
return bytebuffer;
|
}
|
//½«¶ÁÈ¡µÄÊý¾Ýת³ÉByteBufferÊý¾Ý£¨03ÃüÁ
|
public ByteBuffer getByteBuffer_03()
|
{
|
ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN_03);
|
|
bytebuffer.put(ComBase.changeIntToByte(this.dev_addr));
|
bytebuffer.put(ComBase.changeIntToByte(CMD));
|
bytebuffer.putShort(ComBase.changeIntToShort(RES_Index));
|
bytebuffer.putShort(ComBase.changeIntToShort(this.ByteCount));
|
|
CRC = Crc16.CalCRC16(bytebuffer, bytebuffer.position());
|
bytebuffer.putShort((short)CRC);
|
bytebuffer.flip();
|
|
return bytebuffer;
|
}
|
//½«¶ÁÈ¡µÄÊý¾Ýת³ÉByteBufferÊý¾Ý(10ÃüÁî)
|
public ByteBuffer getByteBuffer_10()
|
{
|
ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN_10+ByteCount*2);
|
bytebuffer.put(ComBase.changeIntToByte(this.dev_addr));
|
bytebuffer.put(ComBase.changeIntToByte(this.CMD));
|
bytebuffer.putShort(ComBase.changeIntToShort(this.RES_Index));
|
|
bytebuffer.putShort(ComBase.changeIntToShort(this.ByteCount));
|
bytebuffer.put(ComBase.changeIntToByte(this.ByteCount*2));
|
if(RES_Index == MyModBusCom.LD_PARM_ADDR){
|
System.out.println(this.ld_param);
|
//ÉèÖÃÉ豸µÄ·Åµç²ÎÊýÖµ
|
bytebuffer.putShort(ComBase.changeIntToShort(ld_param.getParam_teststart_batt_num()));
|
bytebuffer.putShort(ComBase.changeIntToShort(ld_param.getParam_cap()));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_discharge_curr()*1000)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_mon_curr_low()*1000)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_charge_curr()*1000)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_mon_curr_high()*1000)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_online_vol()*100)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_charge_curr_low()*1000)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_charge_cap_low()));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_online_vol_recover()*100)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_module_address()));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_junheng_model()));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_cap_low()*10)));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_datasource()));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_res_args()));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)ld_param.getParam_tmp_high()));
|
bytebuffer.putShort(ComBase.changeIntToShort((int)(ld_param.getParam_res_high()*10)));
|
System.out.println((int)(ld_param.getParam_cap_low()*10));
|
}else if(RES_Index==MyModBusCom.LD_CMD_START_TEST_ADDR||RES_Index==MyModBusCom.LD_CMD_STOP_TEST_ADDR
|
|| RES_Index==MyModBusCom.LD9_CMD_STARTRESTEST_ARRR || RES_Index==MyModBusCom.LD9_CMD_STOPRESTEST_ADDR
|
|| RES_Index==MyModBusCom.LD9_CMD_RESTART_ADDR){
|
System.out.println("Æô¶¯»òÍ£Ö¹²âÊÔ");
|
//ÉèÖÃÉ豸Æô¶¯·ÅµçºÍÍ£Ö¹·Åµç|ÖØÆôϵͳ
|
bytebuffer.putShort(ComBase.changeIntToShort(MyModBusCom.LD_CMD_STARTSTOP_VALUE));
|
}else if(RES_Index == MyModBusCom.LD_DATE_TIME_ADDR){
|
Calendar now = Calendar.getInstance();
|
bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.YEAR)%100));
|
bytebuffer.put(ComBase.changeIntToByte((now.get(Calendar.MONTH) + 1)));
|
bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.DAY_OF_MONTH)));
|
bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.HOUR_OF_DAY)));
|
bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.MINUTE)));
|
bytebuffer.put(ComBase.changeIntToByte(now.get(Calendar.SECOND)));
|
}else if(RES_Index == MyModBusCom.LD_BATT_COLLECTIO_PARAM_ADDR) {
|
concentratorparam.putByteBuffer(bytebuffer);
|
}
|
CRC = Crc16.CalCRC16(bytebuffer, bytebuffer.position()) & 0xFFFF;
|
bytebuffer.putShort((short)CRC);
|
bytebuffer.flip();
|
System.out.println(ComFn.bytesToHexString(bytebuffer.array(), bytebuffer.array().length));
|
return bytebuffer;
|
}
|
|
public static short toBigEndian(int n){
|
return (short) ((n<<8) + (n>>8));
|
}
|
|
|
public LD_param getLd_param() {
|
return ld_param;
|
}
|
|
public void setLd_param(LD_param ld_param) {
|
this.ld_param = ld_param;
|
}
|
|
|
|
public int getByteCount() {
|
return ByteCount;
|
}
|
|
public void setByteCount(int byteCount) {
|
ByteCount = byteCount;
|
}
|
|
|
|
public int getRES_Index() {
|
return RES_Index;
|
}
|
|
public void setRES_Index(int rES_Index) {
|
RES_Index = rES_Index;
|
}
|
|
|
|
public ConcentratorParam getConcentratorparam() {
|
return concentratorparam;
|
}
|
|
public void setConcentratorparam(ConcentratorParam concentratorparam) {
|
this.concentratorparam = concentratorparam;
|
}
|
|
@Override
|
public String toString() {
|
return "BaseData [BYTE_LEN_03=" + BYTE_LEN_03 + ", BYTE_LEN_10=" + BYTE_LEN_10 + ", dev_addr=" + dev_addr
|
+ ", ByteCount=" + ByteCount + ", CMD=" + CMD + ", RES_Index=" + RES_Index + ", ld_param="
|
+ ld_param + ", CRC=" + CRC + "]";
|
}
|
|
public static void main(String[] args) {
|
//System.out.println(toBigEndian(0x201B));
|
|
//BaseData data = new BaseData();
|
//data.makeCmd(0x201B, 0x03,0x05);
|
//System.out.println(ComFn.bytesToHexString(data.getByteBuffer().array(), data.getByteBuffer().array().length));
|
|
//System.out.println(ComFn.bytesToHexString(src, len));
|
}
|
|
}
|