package com.dev.fbs9600;
|
|
import com.battdata_rt.BattData_RT;
|
import com.battdata_rt.BattData_RT_Array;
|
import com.battdata_rt.BattData_RT_SQL;
|
import com.battdata_rt.BattStatData;
|
import com.battdata_rt.MonVolData;
|
import com.dev.fbs9100.ComFn;
|
import com.sql.MysqlConnPool;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.InetSocketAddress;
|
import java.net.Socket;
|
import java.util.ArrayList;
|
|
public class FBS9600_SocketClient extends Thread {
|
private static final short MB_READ_REG_COUNT = 120;
|
private static final int FBS9600_Socket_Port = 9600;
|
private String socketServerIp = "192.168.0.66";
|
private FBS9600_State m_FBS9600_State;
|
private FBS9600_CommData m_CommDataOut = new FBS9600_CommData();
|
private FBS9600_CommData m_CommDataIn = new FBS9600_CommData();
|
|
private BattData_RT[] m_RTData = new BattData_RT[4];
|
public int m_FBSDevId = 0;
|
private final boolean m_IsTcpModbus;
|
private int m_BattMonCount = 0;
|
|
private byte m_ModbusCmd_Usr = 0;
|
private short m_ReadRegAddr_Usr = 0;
|
|
private short m_ReadRegAddr = 4;
|
private short m_ReadRegCount = 0;
|
private MysqlConnPool conn = null;
|
private ArrayList<FBS9600_State> al_stat;
|
private int[] a_BattMonCount = new int[4]; //每组电池的单体个数
|
private int batt_Group_Num = 0;
|
private int data_Num = 0; //电池组组数
|
private int[] reg_count_arr = new int[4];
|
private int reg_count_last = 0;
|
|
public FBS9600_SocketClient(FBS9600_State f_stat, BattData_RT_Array AL_RTdata, MysqlConnPool mConnPool) {
|
this.m_FBS9600_State = f_stat;
|
this.m_FBSDevId = this.m_FBS9600_State.dev_id;
|
this.socketServerIp = this.m_FBS9600_State.ip_addr;
|
|
this.conn = mConnPool;
|
this.al_stat = new ArrayList();
|
this.al_stat.add(this.m_FBS9600_State);
|
if (9611 == this.m_FBSDevId / 100000)
|
this.m_IsTcpModbus = false;
|
else {
|
this.m_IsTcpModbus = true;
|
}
|
|
int group_max = 4;
|
for (int n = 0; n < group_max; n++)
|
this.m_RTData[n] = null;
|
for (int n = 0; n < AL_RTdata.getItemCount(); n++) {
|
if (AL_RTdata.getItem(n).FBSDeviceId == this.m_FBSDevId) {
|
this.m_RTData[(AL_RTdata.getItem(n).GroupIndexInFBSDevice % group_max)] = AL_RTdata.getItem(n);
|
this.data_Num = (AL_RTdata.getItem(n).GroupIndexInFBSDevice % group_max);
|
|
for (int k = 0; k < 4; k++) {
|
this.a_BattMonCount[(AL_RTdata.getItem(n).GroupIndexInFBSDevice % group_max)] = AL_RTdata
|
.getItem(n).MonCount;
|
}
|
}
|
}
|
|
if (this.m_BattMonCount > 392) {
|
this.m_BattMonCount = 392;
|
}
|
this.m_ReadRegAddr = getbattGroupAddr((short) 4, this.batt_Group_Num);
|
this.m_ReadRegCount = 0;
|
}
|
|
private void checkAndSetUsrCmdAndRegAddr() {
|
try {
|
FBS9600_State_SQL.queryFbs9600UsrCmd(this.conn, this.al_stat);
|
Thread.sleep(100L);
|
} catch (InterruptedException localInterruptedException) {
|
|
}
|
if ((81 == this.m_FBS9600_State.getFBS9600Cmd()) && (!this.m_FBS9600_State.getFBS9600CmdAckFromDev())) {
|
this.m_ModbusCmd_Usr = 6;
|
|
this.m_ReadRegAddr_Usr = getbattGroupAddr((short) 4087, this.batt_Group_Num);
|
}
|
}
|
|
private void clearInputStream(InputStream is) throws InterruptedException, IOException {
|
for (int n = 0; n < 20; n++) {
|
Thread.sleep(10L);
|
while (is.available() > 0) {
|
is.read();
|
}
|
}
|
}
|
|
private boolean read_data(InputStream is, byte[] buf, int rx_cnt) throws InterruptedException, IOException {
|
int rx_sum = rx_cnt;
|
if (rx_sum > buf.length) {
|
rx_sum = buf.length;
|
}
|
int rx_count = 0;
|
|
boolean comm_ok = true;
|
for (rx_count = 0; rx_count < rx_sum; rx_count++) {
|
int time_out_count = 0;
|
while (is.available() < 1) {
|
Thread.sleep(40L);
|
time_out_count++;
|
if (time_out_count > 100) {
|
comm_ok = false;
|
break;
|
}
|
}
|
|
if (!comm_ok) {
|
break;
|
}
|
buf[rx_count] = ((byte) is.read());
|
}
|
System.out.println("接收数据" + ComFn.bytesToHexString(buf, buf.length));
|
return comm_ok;
|
}
|
|
public boolean lanchSocket(byte cmd_t, short reg_count, short rx_datacount, int reg_count_last) {
|
boolean comm_ok = false;
|
for (int cnt = 0; cnt < 10; cnt++) {
|
Socket socket = new Socket();
|
InputStream mInStream = null;
|
OutputStream mOutStream = null;
|
try {
|
socket.connect(new InetSocketAddress(this.socketServerIp, 9600), 5000);
|
socket.setSoTimeout(5000);
|
mInStream = socket.getInputStream();
|
mOutStream = socket.getOutputStream();
|
|
byte[] tx_buf = this.m_CommDataOut.makeDataToByteArray(this.m_IsTcpModbus, (byte) 1, cmd_t,
|
this.m_ReadRegAddr, reg_count);
|
|
mOutStream.write(tx_buf);
|
mOutStream.flush();
|
System.out.println("发送数据" + ComFn.bytesToHexString(tx_buf, tx_buf.length));
|
if (read_data(mInStream, this.m_CommDataIn.data_byte_rx, rx_datacount))
|
comm_ok = this.m_CommDataIn.getDataFromByteArray(this.m_IsTcpModbus, this.m_ReadRegAddr, reg_count,
|
rx_datacount, reg_count_last);
|
else {
|
comm_ok = false;
|
}
|
clearInputStream(mInStream);
|
} catch (IOException | InterruptedException e) {
|
comm_ok = false;
|
try {
|
socket.close();
|
mInStream.close();
|
mOutStream.close();
|
} catch (IOException localIOException) {
|
}
|
if (comm_ok)
|
break;
|
try {
|
Thread.sleep(1000L);
|
} catch (InterruptedException e1) {
|
e1.printStackTrace();
|
}
|
} finally {
|
try {
|
socket.close();
|
mInStream.close();
|
mOutStream.close();
|
} catch (IOException localIOException1) {
|
}
|
if (comm_ok)
|
break;
|
try {
|
Thread.sleep(1000L);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
return comm_ok;
|
}
|
|
public void updateVersion_Infor(MysqlConnPool mConnPool) {
|
BattData_RT_SQL.updateVersion_Infor_Table(mConnPool);
|
}
|
|
public void updateThread_Time(MysqlConnPool mConnPool) {
|
BattData_RT_SQL.updateThread_Time_Table(mConnPool);
|
}
|
|
private short getbattGroupAddr(short addr, int num) {
|
int all_addr = addr + num * 4096;
|
short batt_group_addr = (short) all_addr;
|
return batt_group_addr;
|
}
|
|
private void getBattMonCount() {
|
if (this.batt_Group_Num > this.data_Num)
|
this.batt_Group_Num = 0;
|
int h = 0;
|
if (h < 4) {
|
if (this.a_BattMonCount[(h + this.batt_Group_Num)] != 0)
|
this.m_BattMonCount = this.a_BattMonCount[(h + this.batt_Group_Num)];
|
}
|
}
|
|
public void run() {
|
updateVersion_Infor(this.conn);
|
while (true) {
|
try {
|
getBattMonCount();
|
updateThread_Time(this.conn);
|
|
checkAndSetUsrCmdAndRegAddr();
|
|
byte cmd_t = 3;
|
short reg_count = this.m_ReadRegCount;
|
if (4160 == this.m_ReadRegAddr)
|
reg_count = 1;
|
else if ((getbattGroupAddr((short) 4, this.batt_Group_Num) == this.m_ReadRegAddr)
|
|| (getbattGroupAddr((short) 6, this.batt_Group_Num) == this.m_ReadRegAddr)) {
|
reg_count = 1;
|
}
|
short rx_datacount = (short) (reg_count * 2 + 3 + 2);
|
if (this.m_IsTcpModbus) {
|
rx_datacount = (short) (reg_count * 2 + 11);
|
}
|
if (this.m_ModbusCmd_Usr != 0) {
|
cmd_t = this.m_ModbusCmd_Usr;
|
this.m_ReadRegAddr = this.m_ReadRegAddr_Usr;
|
reg_count = 1;
|
rx_datacount = 8;
|
if (this.m_IsTcpModbus) {
|
rx_datacount = 14;
|
}
|
|
}
|
|
if ((this.m_ReadRegAddr < getbattGroupAddr((short) 1236, this.batt_Group_Num) + this.m_BattMonCount)
|
|| (this.m_ReadRegAddr == getbattGroupAddr((short) 4087, this.batt_Group_Num))) {
|
if (lanchSocket(cmd_t, reg_count, rx_datacount, this.reg_count_last))
|
this.m_FBS9600_State.setComCountInc();
|
else {
|
this.m_FBS9600_State.setComErrCountInc();
|
}
|
}
|
|
if (getbattGroupAddr((short) 4, this.batt_Group_Num) == this.m_ReadRegAddr) {
|
this.m_ReadRegCount = 0;
|
}
|
|
if (getbattGroupAddr((short) 4087, this.batt_Group_Num) == this.m_ReadRegAddr) {
|
if (this.m_CommDataIn.getUsrWriteCmdAck()) {
|
this.m_FBS9600_State.setFBS9600CmdAckFromDev(true);
|
this.m_ModbusCmd_Usr = 0;
|
this.m_ReadRegAddr_Usr = 0;
|
}
|
this.m_ReadRegAddr = 4160;
|
this.m_ReadRegCount = 0;
|
} else {
|
if (4160 == this.m_ReadRegAddr) {
|
this.m_ReadRegAddr = getbattGroupAddr((short) 4, this.batt_Group_Num);
|
this.m_ReadRegCount = 0;
|
continue;
|
}
|
|
this.m_ReadRegAddr = ((short) (this.m_ReadRegAddr + this.m_ReadRegCount));
|
this.m_ReadRegCount = ((short) this.m_BattMonCount);
|
|
if (getbattGroupAddr((short) 4, this.batt_Group_Num) == this.m_ReadRegAddr) {
|
this.m_ReadRegAddr = getbattGroupAddr((short) 6, this.batt_Group_Num);
|
}
|
if ((this.m_ReadRegAddr >= getbattGroupAddr((short) 6, this.batt_Group_Num))
|
&& (this.m_ReadRegAddr <= getbattGroupAddr((short) 6, this.batt_Group_Num)
|
+ this.m_BattMonCount)) {
|
if (this.m_ReadRegAddr < getbattGroupAddr((short) 6, this.batt_Group_Num) + this.m_BattMonCount)
|
this.m_ReadRegCount = ((short) (getbattGroupAddr((short) 6, this.batt_Group_Num)
|
+ this.m_BattMonCount - this.m_ReadRegAddr));
|
else {
|
this.m_ReadRegAddr = getbattGroupAddr((short) 28, this.batt_Group_Num);
|
}
|
|
}
|
|
if ((this.m_ReadRegAddr >= getbattGroupAddr((short) 28, this.batt_Group_Num))
|
&& (this.m_ReadRegAddr < getbattGroupAddr((short) 327, this.batt_Group_Num))) {
|
if (this.m_ReadRegAddr < getbattGroupAddr((short) 28, this.batt_Group_Num)
|
+ this.m_BattMonCount)
|
this.m_ReadRegCount = ((short) (getbattGroupAddr((short) 28, this.batt_Group_Num)
|
+ this.m_BattMonCount - this.m_ReadRegAddr));
|
else {
|
this.m_ReadRegAddr = getbattGroupAddr((short) 632, this.batt_Group_Num);
|
}
|
}
|
|
if ((this.m_ReadRegAddr >= getbattGroupAddr((short) 632, this.batt_Group_Num))
|
&& (this.m_ReadRegAddr < getbattGroupAddr((short) 931, this.batt_Group_Num))) {
|
if (this.m_ReadRegAddr < getbattGroupAddr((short) 632, this.batt_Group_Num)
|
+ this.m_BattMonCount)
|
this.m_ReadRegCount = ((short) (getbattGroupAddr((short) 632, this.batt_Group_Num)
|
+ this.m_BattMonCount - this.m_ReadRegAddr));
|
else {
|
this.m_ReadRegAddr = getbattGroupAddr((short) 1236, this.batt_Group_Num);
|
}
|
}
|
|
if ((this.m_ReadRegAddr >= getbattGroupAddr((short) 1236, this.batt_Group_Num))
|
&& (this.m_ReadRegAddr < getbattGroupAddr((short) 1535, this.batt_Group_Num))) {
|
if (this.m_ReadRegAddr < getbattGroupAddr((short) 1236, this.batt_Group_Num)
|
+ this.m_BattMonCount) {
|
this.m_ReadRegCount = ((short) (getbattGroupAddr((short) 1236, this.batt_Group_Num)
|
+ this.m_BattMonCount - this.m_ReadRegAddr));
|
} else {
|
if ((this.batt_Group_Num < this.data_Num) && (this.batt_Group_Num >= 0)) {
|
this.reg_count_arr[this.batt_Group_Num] = reg_count;
|
int n = 0;
|
// continue;
|
|
if (this.reg_count_arr[n] != 0)
|
this.reg_count_last += this.reg_count_arr[n];
|
n++;
|
if (n < this.reg_count_arr.length) {
|
continue;
|
}
|
|
}
|
|
this.batt_Group_Num += 1;
|
this.m_ReadRegAddr = getbattGroupAddr((short) 4, this.batt_Group_Num);
|
|
if (this.batt_Group_Num > this.data_Num) {
|
getFBS9600MonData(this.m_CommDataIn);
|
this.reg_count_arr = null;
|
this.reg_count_arr = new int[4];
|
this.reg_count_last = 0;
|
try {
|
Thread.sleep(500L);
|
} catch (InterruptedException localInterruptedException) {
|
}
|
this.batt_Group_Num = 0;
|
this.m_ReadRegAddr = getbattGroupAddr((short) 4, this.batt_Group_Num);
|
}
|
}
|
|
}
|
|
if (this.m_ReadRegCount > 120) {
|
this.m_ReadRegCount = 120;
|
}
|
}
|
|
Thread.sleep(250L);
|
} catch (Exception e) {
|
this.m_FBS9600_State.setComErrCountInc();
|
try {
|
Thread.sleep(1000L);
|
} catch (InterruptedException localInterruptedException1) {
|
}
|
}
|
}
|
}
|
|
private void getFBS9600MonData(FBS9600_CommData c_data) {
|
int mon_index = 0;
|
for (int n = 0; n < 4; n++) {
|
if (this.m_RTData[n] != null) {
|
float curr = c_data.battcurr[n];
|
|
if (1 == c_data.battcurrdir[n]) {
|
curr *= -1.0F;
|
}
|
|
this.m_RTData[n].mTestData.updateCurrFrom_FBSDev(curr);
|
this.m_RTData[n].mTestData.updateGroupVolFrom_FBSDev(c_data.groupvol[n]);
|
|
for (int index = 0; index < this.m_RTData[n].MonCount; index++) {
|
if (mon_index >= 512)
|
break;
|
MonVolData tmp_m_data = (MonVolData) this.m_RTData[n].al_MonVol.get(index);
|
tmp_m_data.monVol = c_data.battvol[mon_index];
|
tmp_m_data.monTmp = c_data.batttmp[mon_index];
|
|
tmp_m_data.monRes = c_data.battres[mon_index];
|
tmp_m_data.connRes = c_data.battres[mon_index];
|
if ((tmp_m_data.monRes > 0.01D) && (tmp_m_data.monVol > 0.1D)
|
&& (this.m_RTData[n].MonStdVol > 0.1D))
|
tmp_m_data.monSer = (1000.0F / tmp_m_data.monRes * (this.m_RTData[n].MonStdVol / 2.0F));
|
else {
|
tmp_m_data.monSer = 0.0F;
|
}
|
mon_index++;
|
}
|
|
for (int index = 0; index < this.m_RTData[n].MonCount; index++) {
|
((MonVolData) this.m_RTData[n].al_MonVol.get(index)).connRes = 0.0F;
|
}
|
this.m_RTData[n].updata_BattRtData_To_RamDb();
|
this.m_RTData[n].updata_BattRtSate_To_RamDb();
|
}
|
}
|
}
|
}
|