package com.dev.fbs9009;
|
|
import java.nio.ByteBuffer;
|
import java.util.Arrays;
|
|
import com.base.ComBase;
|
import com.dev.fbs9100.ComFn;
|
import com.dev.modbus4j.MyModbusMaster;
|
import com.dev.modbus4j.MyModbusUtils;
|
import com.serotonin.modbus4j.BatchRead;
|
import com.serotonin.modbus4j.BatchResults;
|
import com.serotonin.modbus4j.code.DataType;
|
|
public class LD_batt_states {
|
public static int BYTE_LEN=72; //¼Ä´æÆ÷¸öÊý
|
public static int MONDATA_BYTE_LEN = 27;
|
|
private int cmd; //·¢ËÍÊý¾ÝµÄÃüÁî
|
private int cmd_addr; //Ä£¿éµØÖ·
|
private int cmd_addr_back; //¼Ä´æÆ÷µÄÆðʼµØÖ·
|
private int dataCount; //Êý¾ÝµÄ³¤¶È
|
|
private LD_batt_state[] ld_batts=new LD_batt_state[9]; //µç³ØÊý¾Ý
|
private double max_mon_vol; //×î´óµ¥Ìåµçѹ
|
private int max_mon_num; //×î´óµ¥ÌåµçѹµÄ±àºÅ
|
private double min_mon_vol; //×îµÍµ¥Ìåµçѹ
|
private double min_mon_num; //×îµÍµ¥ÌåµçѹµÄ±àºÅ
|
|
public double[] mon_vols = new double[9]; //µ¥Ìåµçѹ
|
public double[] mon_tmps = new double[9]; //µ¥ÌåζÈ
|
public double[] mon_ress = new double[9]; //µ¥ÌåÄÚ×è
|
|
|
public boolean setLD_batt_state(ByteBuffer bf){
|
boolean flag = false;
|
ByteBuffer tmpbuf = bf;
|
//System.out.println("pos:"+bf.position()+"\tlimit:"+bf.limit()+"\tcap"+bf.capacity());
|
tmpbuf.position(0);
|
|
try {
|
this.cmd_addr = ComBase.changeByteToInt(tmpbuf.get()); //»ñÈ¡É豸µØÖ·
|
this.cmd = ComBase.changeByteToInt(tmpbuf.get());
|
this.cmd_addr_back = ComBase.changeShortToInt(tmpbuf.getShort()); //»ñÈ¡¼Ä´æÆ÷µÄÆðʼµØÖ·
|
//this.dataCount = ComBase.changeByteToInt(tmpbuf.get()); //Êý¾ÝÇø×Ö½ÚÊý
|
this.dataCount = ComBase.changeByteToInt(tmpbuf.get());
|
//System.out.println("Êý¾ÝÇø×Ö½ÚÊýµÄ³¤¶È:"+this.dataCount);
|
for (int i = 0; i < LD_batt_states.BYTE_LEN/8; i++) {
|
LD_batt_state ld=new LD_batt_state();
|
ld.setBatt_num(i+1);
|
if(tmpbuf.remaining() < 16) {
|
System.out.println("setLD_batt_state:"+ComFn.bytesToHexString(tmpbuf.array(), tmpbuf.limit()));
|
System.out.println("setLD_batt_state pos:"+tmpbuf.position()+"\tlimit:"+tmpbuf.limit()+"\tcap"+tmpbuf.capacity());
|
break;
|
}
|
ld.setBatt_state(ComBase.changeShortToInt(tmpbuf.getShort()));
|
ld.setBatt_vol(ComBase.changeShortToDouble(tmpbuf.getShort())*0.001);
|
ld.setBatt_curr(ComBase.changeShortToDouble(tmpbuf.getShort())*0.001);
|
ld.setBatt_cap(ComBase.changeShortToDouble(tmpbuf.getShort())*0.01);
|
ld.setStop_reason(ComBase.changeShortToInt(tmpbuf.getShort()));
|
ld.setTest_time_hour(ComBase.changeShortToInt(tmpbuf.getShort()));
|
ld.setTest_time_minute(ComBase.changeShortToInt(tmpbuf.getShort()));
|
ld.setTest_time_second(ComBase.changeShortToInt(tmpbuf.getShort()));
|
this.ld_batts[i]=ld;
|
if(i == 0){
|
this.max_mon_vol = ld.getBatt_vol();
|
this.max_mon_num = ld.getBatt_num();
|
this.min_mon_vol = ld.getBatt_vol();
|
this.min_mon_num = ld.getBatt_num();
|
}
|
if(ld.getBatt_vol() > max_mon_vol){
|
max_mon_vol = ld.getBatt_vol();
|
max_mon_num = ld.getBatt_num();
|
}
|
if(ld.getBatt_vol() < min_mon_vol){
|
min_mon_vol = ld.getBatt_vol();
|
min_mon_num = ld.getBatt_num();
|
}
|
}
|
flag = true;
|
} catch (Exception e) {
|
System.out.println(ComFn.bytesToHexString(tmpbuf.array(), tmpbuf.limit()));
|
System.err.println("Exception pos:"+tmpbuf.position()+"\tlimit:"+tmpbuf.limit()+"\tcap"+tmpbuf.capacity());
|
e.printStackTrace();
|
}
|
return flag;
|
}
|
|
public boolean setLD_battmon_state(ByteBuffer bf){
|
boolean flag = false;
|
ByteBuffer tmpbuf = bf;
|
//System.out.println("pos:"+bf.position()+"\tlimit:"+bf.limit()+"\tcap"+bf.capacity());
|
tmpbuf.position(0);
|
|
try {
|
this.cmd_addr = ComBase.changeByteToInt(tmpbuf.get()); //»ñÈ¡É豸µØÖ·
|
this.cmd = ComBase.changeByteToInt(tmpbuf.get());
|
this.cmd_addr_back = ComBase.changeShortToInt(tmpbuf.getShort()); //»ñÈ¡¼Ä´æÆ÷µÄÆðʼµØÖ·
|
//this.dataCount = ComBase.changeByteToInt(tmpbuf.get()); //Êý¾ÝÇø×Ö½ÚÊý
|
this.dataCount = ComBase.changeByteToInt(tmpbuf.get());
|
//System.out.println("Êý¾ÝÇø×Ö½ÚÊýµÄ³¤¶È:"+this.dataCount);
|
|
if(this.dataCount >= 9*3) {
|
//System.out.print("µ¥Ìåµçѹ:");
|
for(int i=0;i<this.mon_vols.length;i++) {
|
this.mon_vols[i] = (ComBase.changeShortToInt(tmpbuf.getShort())*0.001);
|
//System.out.print(mon_vols[i]+"\t");
|
}
|
//System.out.println();
|
//System.out.print("µ¥ÌåζÈ:");
|
for(int i=0;i<this.mon_tmps.length;i++) {
|
this.mon_tmps[i] = (ComBase.changeShortToInt(tmpbuf.getShort())*0.1) - 10.0f;
|
//System.out.print(mon_tmps[i]+"\t");
|
}
|
//System.out.println();
|
//System.out.print("µ¥ÌåÄÚ×è:");
|
for(int i=0;i<this.mon_ress.length;i++) {
|
this.mon_ress[i] = (ComBase.changeShortToInt(tmpbuf.getShort())*0.001);
|
//System.out.print(mon_ress[i]+"\t");
|
}
|
flag = true;
|
}else {
|
flag = false;
|
}
|
} catch (Exception e) {
|
System.out.println(ComFn.bytesToHexString(tmpbuf.array(), tmpbuf.limit()));
|
System.err.println("Exception pos:"+tmpbuf.position()+"\tlimit:"+tmpbuf.limit()+"\tcap"+tmpbuf.capacity());
|
e.printStackTrace();
|
}
|
return flag;
|
}
|
|
//Åжϵ¥ÌåÊÇ·ñ´¦ÓÚºËÈݲâÊÔ
|
public boolean isDischarge(){
|
for(int i=0;i<this.ld_batts.length;i++){
|
if(this.ld_batts[i] != null && this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
|
return true;
|
}
|
}
|
return false;
|
}
|
|
//»ñÈ¡µ±Ç°ÕýÔÚ½øÐкËÈݲâÊԵĵ¥ÌåµÄ±àºÅ
|
public int getNowTestMonnum(){
|
int mon_num = 0;
|
for(int i=0;i<this.ld_batts.length;i++){
|
if(this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
|
return (i+1);
|
}
|
}
|
return mon_num;
|
}
|
|
/**
|
* »ñÈ¡µ¥ÌåµÄ²âÊÔµçÁ÷ ÓÅÏȼ¶¹ØÏµ ·ÅµçµçÁ÷ >> ³äµçµçÁ÷ >> 0
|
* µ±Óе¥ÌåÔڷŵçµÄʱºò,×é¶ËµÄµçÁ÷ÏÔʾ·ÅµçµÄµçÁ÷(²»¹ÜÊÇ·ñÓÐûÓе¥ÌåÔÚ³äµç)
|
* µ±Ã»Óе¥ÌåÔڷŵç,Óе¥ÌåÔÚ³äµçµÄʱºòÏÔʾ³äµçµÄ¶îʱºòµÄµçÁ÷
|
* ¼Èûµ¥Ìå·Åµç,ҲûÓе¥Ìå³äµçµÄʱºòÏÔʾ0
|
* @return
|
*/
|
public double getBattCurr(){
|
double battcurr = 0;
|
for(int i = 0 ; i < this.ld_batts.length ; i++){
|
if(this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
|
battcurr = this.ld_batts[i].batt_curr;
|
break;
|
}
|
if(this.ld_batts[i].batt_state == MyModBusCom.BATT_CHARGE && battcurr == 0){
|
battcurr = this.ld_batts[i].batt_curr;
|
}
|
}
|
return battcurr;
|
}
|
|
|
/**
|
* »ñÈ¡µ¥ÌåµÄ²âÊÔÈÝÁ¿ ÓÅÏȼ¶¹ØÏµ ·ÅµçµçÁ÷ >> ³äµçµçÁ÷ >> 0
|
* µ±Óе¥ÌåÔڷŵçµÄʱºò,×é¶ËµÄµçÁ÷ÏÔʾ·ÅµçµÄ²âÊÔÈÝÁ¿(²»¹ÜÊÇ·ñÓÐûÓе¥ÌåÔÚ³äµç)
|
* µ±Ã»Óе¥ÌåÔڷŵç,Óе¥ÌåÔÚ³äµçµÄʱºòÏÔʾ³äµçµÄ¶îʱºòµÄÈÝÁ¿
|
* ¼Èûµ¥Ìå·Åµç,ҲûÓе¥Ìå³äµçµÄʱºòÏÔʾ0
|
* @return
|
*/
|
public double getBattTestCap(){
|
double test_cap = 0;
|
for(int i = 0 ; i < this.ld_batts.length ; i++){
|
if(this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
|
test_cap = this.ld_batts[i].batt_cap;
|
break;
|
}
|
if(this.ld_batts[i].batt_state == MyModBusCom.BATT_CHARGE && test_cap == 0){
|
test_cap = this.ld_batts[i].batt_cap;
|
}
|
}
|
return test_cap;
|
}
|
|
public double getMax_mon_vol() {
|
return max_mon_vol;
|
}
|
|
public void setMax_mon_vol(double max_mon_vol) {
|
this.max_mon_vol = max_mon_vol;
|
}
|
|
public int getMax_mon_num() {
|
return max_mon_num;
|
}
|
|
public void setMax_mon_num(int max_mon_num) {
|
this.max_mon_num = max_mon_num;
|
}
|
|
public double getMin_mon_vol() {
|
return min_mon_vol;
|
}
|
|
public void setMin_mon_vol(double min_mon_vol) {
|
this.min_mon_vol = min_mon_vol;
|
}
|
|
public double getMin_mon_num() {
|
return min_mon_num;
|
}
|
|
public void setMin_mon_num(double min_mon_num) {
|
this.min_mon_num = min_mon_num;
|
}
|
|
public LD_batt_state[] getLd_batts() {
|
return ld_batts;
|
}
|
|
public void setLd_batts(LD_batt_state[] ld_batts) {
|
this.ld_batts = ld_batts;
|
}
|
|
|
|
class LD_batt_state{
|
private int batt_num; //µç³Ø±àºÅ
|
private int batt_state; //µç³Ø×´Ì¬
|
private double batt_vol; //µç³Øµçѹ
|
private double batt_curr; //µç³ØµçÁ÷
|
private double batt_cap; //µç³ØÈÝÁ¿
|
private int stop_reason; //Í£Ö¹ÔÒò
|
private int test_time_hour; //²âÊÔʱ¼ä¡ª¡ªÐ¡Ê±
|
private int test_time_minute; //²âÊÔʱ¼ä¡ª¡ª·ÖÖÓ
|
private int test_time_second; //²âÊÔʱ¼ä¡ª¡ªÃë
|
|
public int getBatt_num() {
|
return batt_num;
|
}
|
public void setBatt_num(int batt_num) {
|
this.batt_num = batt_num;
|
}
|
public int getBatt_state() {
|
return batt_state;
|
}
|
public void setBatt_state(int batt_state) {
|
this.batt_state = batt_state;
|
}
|
public double getBatt_vol() {
|
return batt_vol;
|
}
|
public void setBatt_vol(double batt_vol) {
|
this.batt_vol = batt_vol;
|
}
|
public double getBatt_curr() {
|
return batt_curr;
|
}
|
public void setBatt_curr(double batt_curr) {
|
this.batt_curr = batt_curr;
|
}
|
public double getBatt_cap() {
|
return batt_cap;
|
}
|
public void setBatt_cap(double batt_cap) {
|
this.batt_cap = batt_cap;
|
}
|
public int getStop_reason() {
|
return stop_reason;
|
}
|
public void setStop_reason(int stop_reason) {
|
this.stop_reason = stop_reason;
|
}
|
public int getTest_time_hour() {
|
return test_time_hour;
|
}
|
public void setTest_time_hour(int test_time_hour) {
|
this.test_time_hour = test_time_hour;
|
}
|
public int getTest_time_minute() {
|
return test_time_minute;
|
}
|
public void setTest_time_minute(int test_time_minute) {
|
this.test_time_minute = test_time_minute;
|
}
|
public int getTest_time_second() {
|
return test_time_second;
|
}
|
public void setTest_time_second(int test_time_second) {
|
this.test_time_second = test_time_second;
|
}
|
@Override
|
public String toString() {
|
return "LD_batt_state [batt_num=" + batt_num + " batt_vol=" + batt_vol
|
+ ", batt_curr=" + batt_curr + ", batt_cap=" + batt_cap + ", stop_reason=" + stop_reason + "]";
|
}
|
|
}
|
|
@Override
|
public String toString() {
|
return "LD_batt_states [cmd=" + cmd + ", cmd_addr=" + cmd_addr + ", dataCount=" + dataCount + ", ld_batts="
|
+ Arrays.toString(ld_batts) + "]";
|
}
|
|
public BatchRead<Integer> createLDBattStateBatchRead(MyModbusMaster master, int index) {
|
int offset = MyModBusData.BATT_STATE_ADDR_INDEX[index];
|
int count = 0;
|
BatchRead<Integer> batch = new BatchRead<Integer>();
|
for(int i=0;i<ld_batts.length;i++) {
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master)); //µç³Ø×´Ì¬
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master)); //µç³Øµçѹ
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master)); //µç³ØµçÁ÷
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master)); //µç³ØÈÝÁ¿
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master)); //Í£Ö¹ÔÒò
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master)); //²âÊÔʱ³¤(H)
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master)); //²âÊÔʱ³¤(M)
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master)); //²âÊÔʱ³¤(S)
|
}
|
return batch;
|
}
|
|
public boolean putLDBattStateBatchResult(BatchResults<Integer> res) {
|
if(null != res) {
|
int index = 0;
|
for(int i = 0;i<ld_batts.length;i++) {
|
LD_batt_state ld = ld_batts[i];
|
ld.setBatt_num(i+1);
|
|
ld.setBatt_state(res.getIntValue(index++));
|
ld.setBatt_vol((double)res.getIntValue(index++)*0.001);
|
ld.setBatt_curr((double)res.getIntValue(index++)*0.001);
|
ld.setBatt_cap((double)res.getIntValue(index++)*0.01);
|
ld.setStop_reason(res.getIntValue(index++));
|
ld.setTest_time_hour(res.getIntValue(index++));
|
ld.setTest_time_minute(res.getIntValue(index++));
|
ld.setTest_time_second(res.getIntValue(index++));
|
|
if(i == 0){
|
this.max_mon_vol = ld.getBatt_vol();
|
this.max_mon_num = ld.getBatt_num();
|
this.min_mon_vol = ld.getBatt_vol();
|
this.min_mon_num = ld.getBatt_num();
|
}
|
if(ld.getBatt_vol() > max_mon_vol){
|
max_mon_vol = ld.getBatt_vol();
|
max_mon_num = ld.getBatt_num();
|
}
|
if(ld.getBatt_vol() < min_mon_vol){
|
min_mon_vol = ld.getBatt_vol();
|
min_mon_num = ld.getBatt_num();
|
}
|
}
|
return true;
|
}
|
return false;
|
}
|
|
public BatchRead<Integer> createLDMonDataBatchRead(MyModbusMaster master, int index) {
|
int offset = MyModBusData.BATT_MON_DATA_ADDR_INDEX[index];
|
int count = 0;
|
BatchRead<Integer> batch = new BatchRead<Integer>();
|
for(int i=0;i<mon_vols.length;i++) {
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master)); //µç³Ø×´Ì¬
|
}
|
for(int i=0;i<mon_tmps.length;i++) {
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master)); //µç³Ø×´Ì¬
|
}
|
for(int i=0;i<mon_ress.length;i++) {
|
batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master)); //µç³Ø×´Ì¬
|
}
|
return batch;
|
}
|
|
public boolean putLDMonDataBatchResult(BatchResults<Integer> res) {
|
if(null != res) {
|
int index = 0;
|
//System.out.print("µ¥Ìåµçѹ:");
|
for(int i=0;i<this.mon_vols.length;i++) {
|
this.mon_vols[i] = (double)res.getIntValue(index++)*0.001;
|
//System.out.print(mon_vols[i]+"\t");
|
}
|
//System.out.println();
|
//System.out.print("µ¥ÌåζÈ:");
|
for(int i=0;i<this.mon_tmps.length;i++) {
|
this.mon_tmps[i] = ((double)res.getIntValue(index++)*0.1) - 10.0f;
|
//System.out.print(mon_tmps[i]+"\t");
|
}
|
//System.out.println();
|
//System.out.print("µ¥ÌåÄÚ×è:");
|
for(int i=0;i<this.mon_ress.length;i++) {
|
this.mon_ress[i] = ((double)res.getIntValue(index++)*0.001);
|
//System.out.print(mon_ress[i]+"\t");
|
}
|
return true;
|
}
|
return false;
|
}
|
}
|