package com.dev.fbs9600;
|
|
import com.base.Com;
|
import com.base.Crc16;
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.util.Date;
|
|
public class FBS9600_CommData {
|
public static final short REGADDR_NULL = 0;
|
public static final short ResTestState_REGADDR = 4160;
|
public static final short GROUPVOL_REGADDR_START = 4;
|
public static final short ONLINEVOL_REGADDR_START = 8197;
|
public static final short ONLINEVOL_REGADDR_END = 8200;
|
public static final short GROUPCUR_REGADDR_START = 6;
|
public static final short BATTSTATE_REGADDR_START = 8206;
|
public static final short BATTSTATE_REGADDR_END = 8209;
|
public static final short GROUPTMP_REGADDR_START = 8214;
|
public static final short GROUPTMP_REGADDR_END = 8217;
|
public static final short MONVOL_REGADDR_START = 28;
|
public static final short MONVOL_REGADDR_END = 327;
|
public static final short MONTMP_REGADDR_START = 632;
|
public static final short MONTMP_REGADDR_END = 931;
|
public static final short MONRES_REGADDR_START = 1236;
|
public static final short MONRES_REGADDR_END = 1535;
|
public static final short TEST_MONRES_REGADDR = 4087;
|
public static final byte MB_CMD_NULL = 0;
|
public static final byte MB_CMD_READ = 3;
|
public static final byte MB_CMD_WRITE = 6;
|
public static final int CURR_DIR_CHARGE = 0;
|
public static final int CURR_DIR_DISCHARGE = 1;
|
public static final int BATTSTATE_NULL = 0;
|
public static final int BATTSTATE_FLOAT = 1;
|
public static final int BATTSTATE_CHARGE = 2;
|
public static final int BATTSTATE_DISCHARGE = 3;
|
public static final int CMD_GET_BATT_VOL = 1;
|
public static final int CMD_GET_BATT_STATE = 2;
|
public static final int BATT_GROUP_COUNT_MAX = 4;
|
public static final int BATT_MON_COUNT_MAX = 512;
|
private boolean m_UsrWriteCmdAck = false;
|
|
public static final int CMD_StartResTest = 0x51; //启动内阻测试
|
public static final int CMD_StartResTestAck = 0x52; //启动内阻测试成功
|
public static final int CMD_StopResTest = 0x53; //停止内阻测试
|
public static final int CMD_StopResTestAck = 0x54; //停止内阻测试成功
|
|
boolean battres_test_state = false;
|
|
float[] groupvol = new float[4];
|
float[] groupvols = new float[4];
|
|
float[] onlinevol = new float[4];
|
float[] battcurr = new float[4];
|
float[] battcurrs = new float[4];
|
int[] battstate = new int[4];
|
int[] battcurrdir = new int[4];
|
float[] grouptmp = new float[4];
|
|
float[] battvol = new float[512];
|
float[] batttmp = new float[512];
|
float[] battres = new float[512];
|
byte[] data_byte_rx;
|
ByteBuffer data_bf;
|
|
public FBS9600_CommData() {
|
this.data_byte_rx = new byte[256];
|
this.data_bf = ByteBuffer.allocate(512);
|
this.data_bf.order(ByteOrder.BIG_ENDIAN);
|
}
|
|
public boolean getUsrWriteCmdAck() {
|
return this.m_UsrWriteCmdAck;
|
}
|
|
public byte[] makeDataToByteArray(boolean tcp_modbus, byte addr, byte cmd, short reg_addr, short reg_count) {
|
this.data_bf.clear();
|
|
if (tcp_modbus) {
|
this.data_bf.putShort((short) -21846);
|
this.data_bf.putShort(reg_addr);
|
this.data_bf.putShort((short) 8);
|
this.data_bf.put((byte) 0);
|
this.data_bf.put(cmd);
|
this.data_bf.putShort(reg_addr);
|
} else {
|
this.data_bf.put(addr);
|
this.data_bf.put(cmd);
|
this.data_bf.putShort(reg_addr);
|
}
|
|
if (3 == cmd) {
|
this.data_bf.putShort(reg_count);
|
} else if (6 == cmd) {
|
this.m_UsrWriteCmdAck = false;
|
this.data_bf.putShort((short) 1);
|
}
|
int crc = Crc16.CalCRC16(this.data_bf, this.data_bf.position()) & 0xFFFF;
|
|
this.data_bf.putShort((short) crc);
|
|
byte[] d_byte_tx = new byte[this.data_bf.position()];
|
|
this.data_bf.position(0);
|
this.data_bf.get(d_byte_tx);
|
|
return d_byte_tx;
|
}
|
|
boolean getDataFromByteArray(boolean tcp_modbus, short reg_addr, short reg_count, short packet_len,
|
int reg_count_last) {
|
this.data_bf.clear();
|
this.data_bf.put(this.data_byte_rx);
|
this.data_bf.flip();
|
|
this.data_bf.position(0);
|
int crc = Crc16.CalCRC16(this.data_bf, packet_len - 2) & 0xFFFF;
|
|
this.data_bf.position(packet_len - 2);
|
int tmp_crc = this.data_bf.getShort() & 0xFFFF;
|
|
if (crc != tmp_crc) {
|
return false;
|
}
|
|
this.data_bf.position(0);
|
byte cmd = 0;
|
if (tcp_modbus) {
|
if (-21846 != this.data_bf.getShort()) {
|
return false;
|
}
|
if (this.data_bf.getShort() != reg_addr % 4096) {
|
return false;
|
}
|
this.data_bf.getShort();
|
this.data_bf.get();
|
cmd = this.data_bf.get();
|
this.data_bf.get();
|
} else {
|
this.data_bf.get();
|
cmd = this.data_bf.get();
|
this.data_bf.get();
|
}
|
|
if (3 == cmd) {
|
if (4 == reg_addr % 4096) {
|
int battgroup_num = (int) Math.floor(reg_addr / 4096);
|
for (int n = 0; n < 4; n++) {
|
this.groupvols[n] = ((this.data_bf.getShort() & 0xFFFF) / 10.0F);
|
if (n == 0)
|
this.groupvol[battgroup_num] = this.groupvols[0];
|
|
}
|
|
for (int n = 0; n < 4; n++) {
|
this.battcurrs[n] = ((this.data_bf.getShort() & 0xFFFF) / 10.0F);
|
if (n == 0)
|
this.battcurr[battgroup_num] = this.battcurrs[0];
|
|
}
|
|
}
|
|
if ((reg_addr % 4096 >= 28) && (reg_addr % 4096 <= 327)) {
|
String str = Com.getDateTimeFormat(new Date(), "yyyy-MM-dd HH:mm:ss") + " - ";
|
for (int n = reg_count_last; n < reg_count_last + reg_count; n++) {
|
this.battvol[(reg_addr % 4096 - 28 + n)] = ((this.data_bf.getShort() & 0xFFFF) / 1000.0F);
|
}
|
for (int n = 0; n < 24; n++) {
|
str = str + "#" + (n + 1) + ": " + String.format("%1.3f; ",
|
new Object[] { Float.valueOf(this.battvol[(reg_addr % 4096 - 28 + n)]) });
|
}
|
|
} else if ((reg_addr % 4096 >= 632) && (reg_addr % 4096 <= 931)) {
|
for (int n = reg_count_last; n < reg_count_last + reg_count; n++) {
|
this.batttmp[(reg_addr % 4096 - 632 + n)] = ((this.data_bf.getShort() & 0xFFFF) / 10.0F - 10.0F);
|
}
|
|
} else if ((reg_addr % 4096 >= 1236) && (reg_addr % 4096 <= 1535)) {
|
for (int n = reg_count_last; n < reg_count_last + reg_count; n++) {
|
this.battres[(reg_addr % 4096 - 1236 + n)] = ((this.data_bf.getShort() & 0xFFFF) / 1000.0F);
|
}
|
}
|
|
} else if (6 == cmd) {
|
this.m_UsrWriteCmdAck = true;
|
}
|
|
return true;
|
}
|
}
|