New file |
| | |
| | | package com.concentrator;
|
| | |
|
| | | import android.util.Log;
|
| | |
|
| | | import com.util.ComBase;
|
| | | import com.util.Crc16;
|
| | |
|
| | | import java.nio.ByteBuffer;
|
| | | import java.nio.ByteOrder;
|
| | | import java.util.Arrays;
|
| | |
|
| | |
|
| | | public class Concentrator_Cmd
|
| | | {
|
| | | private final String TAG = "Concentrator_Cmd";
|
| | | public final int BYTE_LEN = 4;
|
| | |
|
| | | public int ADDR = 0xFF; //通讯地址
|
| | | public int ByteCount; //数据区字节数量
|
| | | public int CMD; //功能码
|
| | | public int RES_Index; //数据地址
|
| | | private int CRC;
|
| | |
|
| | | public void makeCmd(int addr, int cmd,int RES_Index)
|
| | | {
|
| | | this.ADDR = addr;
|
| | | this.CMD = cmd;
|
| | | this.RES_Index = RES_Index;
|
| | | }
|
| | |
|
| | | public boolean putByteBuffer(final ByteBuffer bf)
|
| | | {
|
| | | if(bf.limit() < BYTE_LEN)
|
| | | return false;
|
| | |
|
| | | 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("CRC error "+crc0+"==="+crc1);
|
| | | return false;
|
| | | }
|
| | | tmpbuf.position(0);
|
| | | ADDR = ComBase.changeByteToInt(tmpbuf.get());
|
| | | CMD = ComBase.changeByteToInt(tmpbuf.get());
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_READCMD == CMD){
|
| | | //读取数据
|
| | | ByteCount = ComBase.changeByteToInt(tmpbuf.get());
|
| | | }else{
|
| | | //写入数据
|
| | | RES_Index = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | int returnvalue = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | if(Concentrator_ComBase.CMD_CONTROL_VALUE == returnvalue){
|
| | | return true;
|
| | | }else{
|
| | | switch (returnvalue){
|
| | | case 0x0000:
|
| | | Log.e(TAG, "putByteBuffer: 操作失败" );break;
|
| | | case 0x0002:
|
| | | Log.e(TAG, "putByteBuffer: 数据地址错误" );break;
|
| | |
|
| | | }
|
| | | return false;
|
| | | }
|
| | | }
|
| | | tmpbuf.compact();
|
| | | tmpbuf.flip();
|
| | | return true;
|
| | | }
|
| | |
|
| | | public ByteBuffer getByteBuffer()
|
| | | {
|
| | | ByteBuffer bytebuffer = ByteBuffer.allocate(BYTE_LEN);
|
| | | bytebuffer.order(ByteOrder.BIG_ENDIAN);
|
| | | bytebuffer.put(ComBase.changeIntToByte(ADDR));
|
| | | bytebuffer.put(ComBase.changeIntToByte(CMD));
|
| | | bytebuffer.putShort(ComBase.changeIntToShort(RES_Index));
|
| | | bytebuffer.flip();
|
| | | return bytebuffer;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取读取汇集参数时的数据
|
| | | * @return
|
| | | */
|
| | | public ByteBuffer getReadByteBuffer(int reg_count){
|
| | | ByteBuffer buffer = ByteBuffer.allocate(2);
|
| | | buffer.order(ByteOrder.BIG_ENDIAN);
|
| | | buffer.putShort(ComBase.changeIntToShort(reg_count));
|
| | | buffer.flip();
|
| | | return buffer;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public String toString() {
|
| | | return "TmpSensor_Cmd [BYTE_LEN=" + BYTE_LEN + ", ADDR=" + ADDR + ", ByteCount=" + ByteCount + ", CMD=" + CMD
|
| | | + ", RES_Index=" + RES_Index + ", CRC=" + CRC + "]";
|
| | | }
|
| | | }
|
| | | /***************************************************************************************
|
| | | ******************************** end of file (FBS_Cmd) *********************************
|
| | | ***************************************************************************************/ |
New file |
| | |
| | | package com.concentrator;
|
| | |
|
| | | public class Concentrator_ComBase {
|
| | | public static final int CMD_MODEBUS_READCMD = 0x03; //读寄存器
|
| | |
|
| | | public static final int CMD_MODEBUS_SINGLEWRITECMD = 0x05; //单点写寄存器
|
| | |
|
| | | public static final int CMD_MODEBUS_WRITECMD = 0x10; //多点写寄存器
|
| | |
|
| | | public static final int CMD_MODEBUS_SYSTEMPARAM_INDEX = 0X1001; //系统参数
|
| | |
|
| | | public static final int CMD_MODEBUS_DEVICEIDINFO_INDEX = 0x201A; //读取设备id
|
| | |
|
| | | public static final int CMD_MODEBUS_DEVICESTATE_INDEX = 0x2001; //读取设备状态信息、
|
| | |
|
| | | public static final int CMD_MODEBUS_BATTMONVOL_INDEX = 0x201B; //单体电压
|
| | |
|
| | | public static final int CMD_MODEBUS_BATTMONTMP_INDEX = 0x2147; //单体温度
|
| | |
|
| | | public static final int CMD_MODEBUS_BATTMONRES_INDEX = 0x2273; //单体内阻
|
| | |
|
| | | public static final int MonomerCountMax = 300; //最大单体数量
|
| | |
|
| | | public static final int CMD_STARTRESTEST_INDEX = 0x4001; //启动内阻测试寄存器地址
|
| | |
|
| | | public static final int CMD_STOPRESTTEST_INDEX = 0x4002; //停止内阻测试寄存器地址
|
| | |
|
| | | public static final int CMD_RESTART_INDEX = 0x4003; //重启系统
|
| | |
|
| | | public static final int CMD_CONTROL_VALUE = 0x0001; //设备控制传值
|
| | | }
|
New file |
| | |
| | | package com.concentrator;
|
| | |
|
| | | import com.util.ComBase;
|
| | | import com.util.Crc16;
|
| | |
|
| | | import java.nio.ByteBuffer;
|
| | | import java.nio.ByteOrder;
|
| | | import java.util.Random;
|
| | |
|
| | |
|
| | | public class Concentrator_ComBuf {
|
| | | public static Concentrator_Cmd m_CMD = new Concentrator_Cmd();
|
| | |
|
| | | public Concentrator_Cmd getTmpSensorCmd()
|
| | | {
|
| | | return this.m_CMD;
|
| | | }
|
| | |
|
| | | public boolean putByteBuffer(final ByteBuffer bf)
|
| | | {
|
| | | if(false == m_CMD.putByteBuffer(bf))
|
| | | return false;
|
| | |
|
| | | return true;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 构造发送的数据包
|
| | | * @param bf
|
| | | * @return
|
| | | */
|
| | | public static ByteBuffer makeTmpSensorCommBuf(Concentrator_Cmd m_CMD ,ByteBuffer bf)
|
| | | {
|
| | | ByteBuffer buffer = ByteBuffer.allocate(m_CMD.BYTE_LEN + bf.limit()+2); //数据总长度
|
| | | buffer.order(ByteOrder.BIG_ENDIAN); //设置ByteBuffer的字节序
|
| | |
|
| | | //m_CMD.makeCmd(addr, cmd,reg_index,getByteCountByReg(reg_index));
|
| | |
|
| | | buffer.put(m_CMD.getByteBuffer());
|
| | | buffer.put(bf);
|
| | |
|
| | | int CRC = Crc16.CalCRC16(buffer, buffer.position());
|
| | | buffer.putShort(ComBase.changeIntToShort(CRC));
|
| | | buffer.flip();
|
| | | return buffer;
|
| | | }
|
| | |
|
| | | /***************************************************************************************
|
| | | ****************************** end of file (FBS_ComBuf) ********************************
|
| | | ***************************************************************************************/
|
| | | }
|
New file |
| | |
| | | package com.concentrator;
|
| | |
|
| | | import com.util.ComBase;
|
| | |
|
| | | import java.nio.ByteBuffer;
|
| | |
|
| | | /**
|
| | | * 汇集器参数
|
| | | */
|
| | | public class Concentrator_Param {
|
| | | public final static int REG_COUNT = 17; //寄存器数量
|
| | |
|
| | | public int dev_addr; //设备地址
|
| | | public int first_monCount; //第一路单体数目
|
| | | public int second_monCount; //第二路单体数目
|
| | | public int thrid_monCount; //第三路单体数目
|
| | | public int fourth_monCount; //第四路单体数目
|
| | | public int fifth_monCount; //第五路单体数目
|
| | | public int curr_range; //电流钳量程
|
| | | public double float_currLimit; //浮充电流门限(0.1)
|
| | | public int backup1; //备份1
|
| | | public int auto_resTestCount; //内阻自动测试周期(小时)
|
| | | public int backup2; //备份2
|
| | | public int backup3; //备份3
|
| | | public int monStdCap; //标称容量(AH)
|
| | | public int backup4; //备份4
|
| | | public int backup5; //备份5
|
| | | public int backup6; //备份6
|
| | | public int backup7; //备份7
|
| | |
|
| | |
|
| | | /**
|
| | | * 获取读取汇集参数时的数据
|
| | | * @return
|
| | | */
|
| | | public ByteBuffer getReadByteBuffer(){
|
| | | ByteBuffer buffer = ByteBuffer.allocate(2);
|
| | | buffer.putShort(ComBase.changeIntToShort(REG_COUNT));
|
| | | buffer.flip();
|
| | | return buffer;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取设置时的数据
|
| | | * @return
|
| | | */
|
| | | public ByteBuffer getWriteByteBuffer(){
|
| | | ByteBuffer buffer = ByteBuffer.allocate(REG_COUNT*2+3);
|
| | | buffer.putShort(ComBase.changeIntToShort(REG_COUNT));
|
| | | buffer.put(ComBase.changeIntToByte(REG_COUNT));
|
| | | buffer.putShort(ComBase.changeIntToShort(dev_addr)); //设备地址
|
| | | buffer.putShort(ComBase.changeIntToShort(first_monCount)); //第一路单体数目
|
| | | buffer.putShort(ComBase.changeIntToShort(second_monCount)); //第二路单体数目
|
| | | buffer.putShort(ComBase.changeIntToShort(thrid_monCount)); //第三路单体数目
|
| | | buffer.putShort(ComBase.changeIntToShort(fourth_monCount)); //第四路单体数目
|
| | | buffer.putShort(ComBase.changeIntToShort(fifth_monCount)); //第五路单体数目
|
| | | buffer.putShort(ComBase.changeIntToShort(curr_range)); //电流钳量程
|
| | | buffer.putShort(ComBase.changeIntToShort((int)(float_currLimit*10))); //浮充电流门限(0.1)
|
| | | buffer.putShort(ComBase.changeIntToShort(backup1)); //备份1
|
| | | buffer.putShort(ComBase.changeIntToShort(auto_resTestCount)); //内阻自动测试周期(小时)
|
| | | buffer.putShort(ComBase.changeIntToShort(backup2)); //备份2
|
| | | buffer.putShort(ComBase.changeIntToShort(backup3)); //备份3
|
| | | buffer.putShort(ComBase.changeIntToShort(monStdCap)); //标称容量(AH)
|
| | | buffer.putShort(ComBase.changeIntToShort(backup4)); //备份4
|
| | | buffer.putShort(ComBase.changeIntToShort(backup5)); //备份5
|
| | | buffer.putShort(ComBase.changeIntToShort(backup6)); //备份6
|
| | | buffer.putShort(ComBase.changeIntToShort(backup7)); //备份7
|
| | | buffer.flip();
|
| | | return buffer;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 解析读取过来的汇集器参数
|
| | | * @param bf
|
| | | * @return
|
| | | */
|
| | | public boolean putByteBuffer(ByteBuffer bf) {
|
| | | if(bf.limit() < REG_COUNT*2) {
|
| | | return false;
|
| | | }
|
| | | ByteBuffer tmpbuf = bf;
|
| | | tmpbuf.position(0);
|
| | |
|
| | | dev_addr = ComBase.changeShortToByte(tmpbuf.getShort()); //设备地址
|
| | | first_monCount = ComBase.changeShortToByte(tmpbuf.getShort()); //第一路单体数目
|
| | | second_monCount = ComBase.changeShortToByte(tmpbuf.getShort()); //第二路单体数目
|
| | | thrid_monCount = ComBase.changeShortToByte(tmpbuf.getShort()); //第三路单体数目
|
| | | fourth_monCount = ComBase.changeShortToByte(tmpbuf.getShort()); //第四路单体数目
|
| | | fifth_monCount = ComBase.changeShortToByte(tmpbuf.getShort()); //第五路单体数目
|
| | | curr_range = ComBase.changeShortToByte(tmpbuf.getShort()); //电流钳量程
|
| | | float_currLimit = ((double)ComBase.changeShortToByte(tmpbuf.getShort()))/10; //浮充电流门限(0.1)
|
| | | backup1 = ComBase.changeShortToByte(tmpbuf.getShort()); //备份1
|
| | | auto_resTestCount = ComBase.changeShortToByte(tmpbuf.getShort()); //内阻自动测试周期(小时)
|
| | | backup2 = ComBase.changeShortToByte(tmpbuf.getShort()); //备份2
|
| | | backup3 = ComBase.changeShortToByte(tmpbuf.getShort()); //备份3
|
| | | monStdCap = ComBase.changeShortToByte(tmpbuf.getShort()); //标称容量(AH)
|
| | | backup4 = ComBase.changeShortToByte(tmpbuf.getShort()); //备份4
|
| | | backup5 = ComBase.changeShortToByte(tmpbuf.getShort()); //备份5
|
| | | backup6 = ComBase.changeShortToByte(tmpbuf.getShort()); //备份6
|
| | | backup7 = ComBase.changeShortToByte(tmpbuf.getShort()); //备份7
|
| | |
|
| | | return true;
|
| | | }
|
| | |
|
| | | //计算总的单体数目
|
| | | public int countMonCount(){
|
| | | return this.first_monCount+second_monCount+thrid_monCount+fourth_monCount+fifth_monCount;
|
| | | }
|
| | |
|
| | | }
|
New file |
| | |
| | | package com.concentrator;
|
| | |
|
| | | import com.util.ComBase;
|
| | |
|
| | | import java.nio.ByteBuffer;
|
| | | import java.nio.ByteOrder;
|
| | |
|
| | |
|
| | | public class Concentrator_State {
|
| | | public static final int BYTE_LEN = 30;
|
| | | public static final int REG_LEN = 30;
|
| | |
|
| | | public static final int MON_REG_COUNT = 100; //单体数据一次最多的汇集器数量
|
| | | public static final int SYS_REG_COUNT = 15; //系统状态的汇集器数量
|
| | |
|
| | | public static int monCount = 24; //单体数量
|
| | |
|
| | | private double sysversion; //系统版本(0.1)
|
| | | private int sysstate; //系统状态 0-常规模式 1-内阻模式 2-编址模式
|
| | | private double groupvol; //组端电压(0.1)
|
| | | private double groupcurr; //组端电流(0.1)
|
| | | private int battstate; //电池状态 0-浮充 1-放电 2-充电
|
| | | private int currdirection; //电流方向 0-充电 1-放电
|
| | |
|
| | | private int backup1;
|
| | | private int backup2;
|
| | | private int backup3;
|
| | | private int backup4;
|
| | | private int backup5;
|
| | | private int backup6;
|
| | | private int backup7;
|
| | | private int backup8;
|
| | | private int restestcount; //内阻测试次数
|
| | |
|
| | | public double[] mon_vols = new double[Concentrator_ComBase.MonomerCountMax]; //单体电压
|
| | | public double[] mon_tmps = new double[Concentrator_ComBase.MonomerCountMax]; //单体温度
|
| | | public double[] mon_ress = new double[Concentrator_ComBase.MonomerCountMax]; //单体内阻
|
| | |
|
| | |
|
| | | public ByteBuffer getReadBuffer() {
|
| | | ByteBuffer tmp = ByteBuffer.allocate(2);
|
| | | tmp.order(ByteOrder.BIG_ENDIAN);
|
| | | tmp.putShort(ComBase.changeIntToShort(REG_LEN));
|
| | | tmp.flip();
|
| | | return tmp;
|
| | | }
|
| | |
|
| | | public boolean putByteBuffer(ByteBuffer bf) {
|
| | | if(bf.limit() < BYTE_LEN) {
|
| | | return false;
|
| | | }
|
| | | ByteBuffer tmpbuf = bf;
|
| | | tmpbuf.position(0);
|
| | |
|
| | | sysversion = ComBase.changeShortToDouble(tmpbuf.getShort())/10; //系统版本
|
| | | sysstate = ComBase.changeShortToInt(tmpbuf.getShort()); //系统状态
|
| | | groupvol = ComBase.changeShortToDouble(tmpbuf.getShort())/10; //组端电压
|
| | | groupcurr = ComBase.changeShortToDouble(tmpbuf.getShort())/10; //组端电流
|
| | | battstate = ComBase.changeShortToInt(tmpbuf.getShort()); //电池状态
|
| | | currdirection = ComBase.changeShortToInt(tmpbuf.getShort()); //电流方向
|
| | |
|
| | | backup1 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | backup2 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | backup3 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | backup4 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | backup5 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | backup6 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | backup7 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | backup8 = ComBase.changeShortToInt(tmpbuf.getShort());
|
| | | restestcount = ComBase.changeShortToInt(tmpbuf.getShort()); //内阻测试次数
|
| | |
|
| | | return true;
|
| | | }
|
| | |
|
| | | public boolean putMonDataBuffer(ByteBuffer bf,int reg_addr){
|
| | | if(bf.limit() < BYTE_LEN) {
|
| | | return false;
|
| | | }
|
| | | bf.position(0);
|
| | | int index = 0;
|
| | | int dataType = 1;
|
| | | int range = 1000;
|
| | | if(reg_addr >= 0x201B && reg_addr <= 0x2146){
|
| | | //单体电压
|
| | | dataType = 1;
|
| | | index = (reg_addr - 0x201B)/100; //设置当前索引
|
| | | }else if(reg_addr >= 0x2147 && reg_addr<= 0x2272){
|
| | | //单体温度
|
| | | dataType = 2;
|
| | | index = (reg_addr - 0x2147)/100;
|
| | | }else if(reg_addr >= 0x2273 && reg_addr <= 0x239E){
|
| | | //单体内阻
|
| | | dataType = 3;
|
| | | index = (reg_addr - 0x2273)/100;
|
| | | }
|
| | | for(int i=0;i<100;i++){
|
| | | switch (dataType){
|
| | | case 1:{
|
| | | mon_vols[index*100+i] = ComBase.changeShortToDouble(bf.getShort())/1000;
|
| | | }break;
|
| | | case 2:{
|
| | | mon_tmps[index*100+i] = ComBase.changeShortToDouble(bf.getShort())/10;
|
| | | }break;
|
| | | case 3:{
|
| | | mon_ress[index*100+i] = ComBase.changeShortToDouble(bf.getShort())/1000;
|
| | | }break;
|
| | | }
|
| | | }
|
| | | return true;
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | public int getBYTE_LEN() {
|
| | | return BYTE_LEN;
|
| | | }
|
| | |
|
| | |
|
| | | public double getSysversion() {
|
| | | return sysversion;
|
| | | }
|
| | |
|
| | | public int getSysstate() {
|
| | | return sysstate;
|
| | | }
|
| | |
|
| | | public double getGroupvol() {
|
| | | return groupvol;
|
| | | }
|
| | |
|
| | | public double getGroupcurr() {
|
| | | return groupcurr;
|
| | | }
|
| | |
|
| | | public int getBattstate() {
|
| | | return battstate;
|
| | | }
|
| | |
|
| | | public int getCurrdirection() {
|
| | | return currdirection;
|
| | | }
|
| | |
|
| | | public int getBackup1() {
|
| | | return backup1;
|
| | | }
|
| | |
|
| | | public int getBackup2() {
|
| | | return backup2;
|
| | | }
|
| | |
|
| | | public int getBackup3() {
|
| | | return backup3;
|
| | | }
|
| | |
|
| | | public int getBackup4() {
|
| | | return backup4;
|
| | | }
|
| | |
|
| | | public int getBackup5() {
|
| | | return backup5;
|
| | | }
|
| | |
|
| | | public int getBackup6() {
|
| | | return backup6;
|
| | | }
|
| | |
|
| | | public int getBackup7() {
|
| | | return backup7;
|
| | | }
|
| | |
|
| | | public int getBackup8() {
|
| | | return backup8;
|
| | | }
|
| | |
|
| | | public int getRestestcount() {
|
| | | return restestcount;
|
| | | }
|
| | |
|
| | |
|
| | | public void setSysversion(double sysversion) {
|
| | | this.sysversion = sysversion;
|
| | | }
|
| | |
|
| | | public void setSysstate(int sysstate) {
|
| | | this.sysstate = sysstate;
|
| | | }
|
| | |
|
| | | public void setGroupvol(double groupvol) {
|
| | | this.groupvol = groupvol;
|
| | | }
|
| | |
|
| | | public void setGroupcurr(double groupcurr) {
|
| | | this.groupcurr = groupcurr;
|
| | | }
|
| | |
|
| | | public void setBattstate(int battstate) {
|
| | | this.battstate = battstate;
|
| | | }
|
| | |
|
| | | public void setCurrdirection(int currdirection) {
|
| | | this.currdirection = currdirection;
|
| | | }
|
| | |
|
| | | public void setBackup1(int backup1) {
|
| | | this.backup1 = backup1;
|
| | | }
|
| | |
|
| | | public void setBackup2(int backup2) {
|
| | | this.backup2 = backup2;
|
| | | }
|
| | |
|
| | | public void setBackup3(int backup3) {
|
| | | this.backup3 = backup3;
|
| | | }
|
| | |
|
| | | public void setBackup4(int backup4) {
|
| | | this.backup4 = backup4;
|
| | | }
|
| | |
|
| | | public void setBackup5(int backup5) {
|
| | | this.backup5 = backup5;
|
| | | }
|
| | |
|
| | | public void setBackup6(int backup6) {
|
| | | this.backup6 = backup6;
|
| | | }
|
| | |
|
| | | public void setBackup7(int backup7) {
|
| | | this.backup7 = backup7;
|
| | | }
|
| | |
|
| | | public void setBackup8(int backup8) {
|
| | | this.backup8 = backup8;
|
| | | }
|
| | |
|
| | | public void setRestestcount(int restestcount) {
|
| | | this.restestcount = restestcount;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public String toString() {
|
| | | return "Concentrator_State [sysversion=" + sysversion + ", sysstate=" + sysstate
|
| | | + ", groupvol=" + groupvol + ", groupcurr=" + groupcurr + ", battstate=" + battstate
|
| | | + ", currdirection=" + currdirection + ", backup1=" + backup1 + ", backup2=" + backup2 + ", backup3="
|
| | | + backup3 + ", backup4=" + backup4 + ", backup5=" + backup5 + ", backup6=" + backup6 + ", backup7="
|
| | | + backup7 + ", backup8=" + backup8 + ", restestcount=" + restestcount + "]";
|
| | | }
|
| | |
|
| | |
|
| | | }
|
| | |
| | | public void getAllRS485Names(){
|
| | | Toast.makeText(this.mContext,"进入获取串口号",Toast.LENGTH_SHORT).show();
|
| | | SerialPortFinder finder = new SerialPortFinder();
|
| | | String[] b = finder.getAllDevicesPath();
|
| | | String[] b = new String[0];
|
| | | try {
|
| | | b = finder.getAllDevicesPath();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | ServiceModel model = new ServiceModel();
|
| | | model.data = b;
|
| | |
|
| | |
| | | package com.fgkj.action;
|
| | |
|
| | | import android.os.Handler;
|
| | | import android.util.Log;
|
| | |
|
| | | import java.io.Serializable;
|
| | |
|
| | | public class ServiceModel implements Serializable {
|
| | | public Integer code = 0;
|
| | | public Integer code = 0; //返回到前台的消息命令码 0:成功 1:失败
|
| | | public int cmd; //寄存器地址
|
| | | public int cmd_type; //操作类型 0x03 读 0x10 写
|
| | | public String msg;
|
| | | public Object data;
|
| | | public Handler handler;
|
| | | public Handler mhandler;
|
| | |
|
| | | @Override
|
| | | public String toString() {
|
| | | return "ServiceModel{" +
|
| | | "code=" + code +
|
| | | ", cmd=" + cmd +
|
| | | ", cmd_type=" + cmd_type +
|
| | | ", msg='" + msg + '\'' +
|
| | | ", data=" + data +
|
| | | ", handler=" + handler +
|
| | | ", mhandler=" + mhandler +
|
| | | '}';
|
| | | }
|
| | |
|
| | | public ServiceModel clone(){
|
| | | ServiceModel model = new ServiceModel();
|
| | | model.cmd = this.cmd;
|
| | | model.cmd_type = this.cmd_type;
|
| | | model.msg = this.msg;
|
| | | model.mhandler = this.mhandler;
|
| | | model.data = this.data;
|
| | | return model;
|
| | | }
|
| | | // public static void main(String[] args) {
|
| | | // System.out.println("$$$$$$$$$$$$$");
|
| | | // }
|
| | | }
|
| | |
| | | //File[] files = dev.listFiles(); |
| | | File[] files = dev.listFiles(); |
| | | int i; |
| | | for (i=0; i<files.length; i++) { |
| | | if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) { |
| | | Log.d(TAG, "Found new device: " + files[i]); |
| | | mDevices.add(files[i]); |
| | | if(files != null && files.length>0){ |
| | | for (i=0; i<files.length; i++) { |
| | | if (files[i].getAbsolutePath().startsWith(mDeviceRoot)) { |
| | | Log.d(TAG, "Found new device: " + files[i]); |
| | | mDevices.add(files[i]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return mDevices; |
| | | } |
| | | public String getName() { |
New file |
| | |
| | | package com.socket;
|
| | |
|
| | | import android.os.Handler;
|
| | | import android.os.Looper;
|
| | | import android.os.Message;
|
| | | import android.util.Log;
|
| | |
|
| | | import com.concentrator.Concentrator_Cmd;
|
| | | import com.concentrator.Concentrator_ComBase;
|
| | | import com.concentrator.Concentrator_ComBuf;
|
| | | import com.concentrator.Concentrator_Param;
|
| | | import com.concentrator.Concentrator_State;
|
| | | import com.fgkj.action.ServiceModel;
|
| | | import com.google.gson.Gson;
|
| | | import com.serial.SerialPort;
|
| | | import java.io.File;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.io.OutputStream;
|
| | | import java.nio.ByteBuffer;
|
| | | import java.nio.ByteOrder;
|
| | | import java.util.Date;
|
| | |
|
| | | /**
|
| | | * 底层和汇集器通讯线程
|
| | | */
|
| | | public class CommSockClientThread extends Thread{
|
| | | public static final String TAG = "CommSockClientThread";
|
| | | private static final String portName = "/dev/ttyS4"; //串口名称
|
| | | private static final int portrate = 9600; //波特率
|
| | | private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1500);
|
| | | private Concentrator_State concentratorState; //汇集器状态
|
| | | private Concentrator_Param concentratorParam; //汇集器参数
|
| | |
|
| | |
|
| | | private int rx_errcount_for_live_of_thread = 0;
|
| | | private static CommSockClientThread clientThread;
|
| | | private SerialPort socket;
|
| | | public Handler mHandler = null;
|
| | | private boolean thread_run_flag = true;
|
| | | private Gson gson = new Gson();
|
| | | private ServiceModel remodel;
|
| | |
|
| | | private CommSockClientThread(){
|
| | | this.concentratorState = new Concentrator_State();
|
| | | this.concentratorParam = new Concentrator_Param();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 构造当前类的单例对象
|
| | | * @return
|
| | | */
|
| | | public static CommSockClientThread createClientThread(){
|
| | | if(clientThread == null){
|
| | | clientThread = new CommSockClientThread();
|
| | | }
|
| | | return clientThread;
|
| | | }
|
| | |
|
| | |
|
| | | @Override
|
| | | public void run() {
|
| | | this.setName(TAG);
|
| | |
|
| | | //初始化消息循环队列,需要在Handler创建之前
|
| | | Looper.prepare();
|
| | |
|
| | | initConnectDevice();
|
| | |
|
| | | mHandler = new Handler()
|
| | | {
|
| | | @Override
|
| | | public void handleMessage(Message msg)
|
| | | {
|
| | | ServiceModel model = (ServiceModel) (msg.obj);
|
| | | try
|
| | | {
|
| | | if(null != model.mhandler)
|
| | | {
|
| | | //开始构造发送数据包
|
| | | model.msg = null;
|
| | | byte[] basedata = makeComData(model);
|
| | | if(basedata.length > 0 && true == SocketComm(basedata,model))
|
| | | {
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_DEVICESTATE_INDEX == remodel.cmd)
|
| | | {
|
| | | remodel.data = concentratorState;
|
| | | }
|
| | | else if(Concentrator_ComBase.CMD_MODEBUS_SYSTEMPARAM_INDEX == remodel.cmd &&
|
| | | Concentrator_ComBase.CMD_MODEBUS_READCMD == remodel.cmd_type )
|
| | | {
|
| | | //
|
| | | remodel.data = concentratorParam;
|
| | | }
|
| | | else if(remodel.cmd >= 0x201B && remodel.cmd <= 0x239E)
|
| | | {
|
| | | remodel.data = concentratorState;
|
| | | }
|
| | | //-----------------------------------------------
|
| | | }
|
| | | Handler handler = remodel.mhandler;
|
| | | Message toMain = handler.obtainMessage();
|
| | | toMain.obj = remodel;
|
| | | handler.sendMessage(toMain);
|
| | | sleep(100);
|
| | | }
|
| | | }
|
| | | catch (InterruptedException e)
|
| | | {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | };
|
| | |
|
| | | Log.i(TAG, "mhandler is bound to - " + mHandler.getLooper().getThread().getName());
|
| | | //启动子线程消息循环队列
|
| | | Looper.loop();
|
| | | Log.i(TAG, "Stop thread's message queue, thread is exited");
|
| | | }
|
| | |
|
| | | /**
|
| | | * 向指定的socket通道发送数据,以及接收数据
|
| | | * @return
|
| | | * @throws InterruptedException
|
| | | */
|
| | |
|
| | | public boolean SocketComm(byte[] datasource,ServiceModel model) throws InterruptedException
|
| | | {
|
| | | remodel = model.clone();
|
| | | boolean res_t = false;
|
| | | try
|
| | | {
|
| | | if(this.socket != null)
|
| | | {
|
| | | InputStream in = this.socket.getInputStream();
|
| | | OutputStream out = this.socket.getOutputStream();
|
| | | //----------------- clear rx buff for tcp resend packet ----------------//
|
| | | byte[] rx_buf_t = new byte[1024];
|
| | |
|
| | | //----------------------------------------------------------------------//
|
| | | bytebuffer_for_socket_RX.order(ByteOrder.BIG_ENDIAN);
|
| | | bytebuffer_for_socket_RX.clear();
|
| | | //--------------------- socket write -----------------------------------//
|
| | | //--------------------- socket write -----------------------------------//
|
| | | boolean aes_en = true;
|
| | | byte[] plain_tx_t = datasource;
|
| | | byte[] cipher_tx_t = new byte[plain_tx_t.length];
|
| | | Date d1 = new Date();
|
| | | //=====================================================================//
|
| | | /*
|
| | | for(int n=0; n<3; n++) {
|
| | | out.write(b);
|
| | | out.flush();
|
| | | //-----------------------------------------------------------------//
|
| | | try {
|
| | | while(true) {
|
| | | int rx_len_t = in.read(rx_buf_t);
|
| | | if(rx_len_t > 0) {
|
| | | if((bytebuffer_for_socket_RX.position()+rx_len_t)
|
| | | < (bytebuffer_for_socket_RX.capacity()-16)) {
|
| | |
|
| | | bytebuffer_for_socket_RX.put(rx_buf_t, 0, rx_len_t);
|
| | | }
|
| | | }
|
| | | if(bytebuffer_for_socket_RX.position() > 8) {
|
| | | break;
|
| | | }
|
| | | }
|
| | | } catch (SocketTimeoutException soe) {
|
| | | //soe.printStackTrace();
|
| | | }
|
| | | //-----------------------------------------------------------------//
|
| | | }
|
| | | //=====================================================================//
|
| | | */
|
| | | //Log.d(TAG, "SocketComm: ");
|
| | | int rx_read_time_out = 0;
|
| | | //int rx_len = 0;
|
| | | out.write(cipher_tx_t);
|
| | | out.flush();
|
| | | while(true) {
|
| | | if(in.available() > 0) {
|
| | | rx_read_time_out = 0;
|
| | | int rx_len_t = in.read(rx_buf_t);
|
| | | if((bytebuffer_for_socket_RX.position()+rx_len_t)
|
| | | < (bytebuffer_for_socket_RX.capacity()-1)) {
|
| | | bytebuffer_for_socket_RX.put(rx_buf_t, 0, rx_len_t);
|
| | | }
|
| | | //bytebuffer_for_socket_RX.put((byte)in.read());
|
| | | } else {
|
| | | rx_read_time_out++;
|
| | | if((bytebuffer_for_socket_RX.position() >= 8) && (rx_read_time_out>10)){
|
| | | // res = true;
|
| | | break;
|
| | | }
|
| | | if(rx_read_time_out > 220) {
|
| | | break;
|
| | | }
|
| | | }
|
| | | Thread.sleep(10);
|
| | | }
|
| | |
|
| | | bytebuffer_for_socket_RX.flip();
|
| | |
|
| | | Date d2 = new Date();
|
| | | long comm_tms = (d2.getTime() - d1.getTime());
|
| | | if(comm_tms < 200) {
|
| | | Thread.sleep(200 - comm_tms);
|
| | | }
|
| | | /*
|
| | | System.out.println(this.getName()
|
| | | + " dev_id: " + m_StatAndParam.dev_id
|
| | | + " d2-d1:" + (comm_tms)+"ms"
|
| | | + " rx_len:" + bytebuffer_for_socket_RX.limit()
|
| | | + " tx_count:" + m_FBS_VCData.m_SysState.CommCount
|
| | | + " rx_err_sum:" + m_FBS_VCData.m_SysState.ErrCommCount
|
| | | + " rx_err_count:" + rx_errcount_for_live_of_thread
|
| | | + Com.getNowTimeWithAt());
|
| | | */
|
| | | byte[] cipher_buf = new byte[bytebuffer_for_socket_RX.limit()];
|
| | | bytebuffer_for_socket_RX.get(cipher_buf);
|
| | |
|
| | |
|
| | | //Log.e(TAG, "SocketComm: "+ComFn.bytesToHexString(plain_buf, plain_buf.length));
|
| | | if(true == getDataFromCommBuf(cipher_buf,remodel)) {
|
| | | rx_errcount_for_live_of_thread = 0;
|
| | | res_t = true;
|
| | | remodel.code = 1;
|
| | | /*
|
| | | System.err.println(ComFn.bytesToHexString(buffer, buffer.length));
|
| | | System.err.println(this.getName() + " DevId: " + m_StatAndParam.dev_id
|
| | | + "---- cmd:" + cmd + " RxLen: " + buffer.length
|
| | | + " 通信失败" + Com.getNowTimeWithAt());
|
| | | */
|
| | | } else {
|
| | | remodel.code = 0;
|
| | | rx_errcount_for_live_of_thread++;
|
| | | res_t = false;
|
| | | }
|
| | | } else {
|
| | | //System.out.println(this.getName() + "-------socket 异常关闭-------" + Com.getNowTimeWithAt());
|
| | | remodel.code = 0;
|
| | | thread_run_flag = false;
|
| | | }
|
| | | } catch (IOException e) {
|
| | | remodel.code = 0;
|
| | | remodel.msg = e.getMessage();
|
| | | thread_run_flag = false;
|
| | | } finally {
|
| | | //若在通信的过程中出现4次通信异常关闭连接
|
| | | if(rx_errcount_for_live_of_thread >= 5 || !thread_run_flag){
|
| | | if(socket != null){
|
| | | try {
|
| | | socket.close();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | initConnectDevice();
|
| | | rx_errcount_for_live_of_thread = 0;
|
| | | thread_run_flag = true;
|
| | | }
|
| | | }
|
| | | return res_t;
|
| | | }
|
| | |
|
| | | public boolean getDataFromCommBuf(byte[] recebyte,ServiceModel model){
|
| | | boolean isSuccess = false;
|
| | | ByteBuffer bf = ByteBuffer.allocate(recebyte.length);
|
| | | bf.order(ByteOrder.BIG_ENDIAN);
|
| | | bf.put(recebyte);
|
| | | bf.flip();
|
| | | Concentrator_Cmd Ccmd = new Concentrator_Cmd();
|
| | | if(true == Ccmd.putByteBuffer(bf)){
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_READCMD == Ccmd.CMD){
|
| | | //读取数据
|
| | | if(Ccmd.ByteCount == Concentrator_State.BYTE_LEN){
|
| | | //读取系统状态
|
| | | if(concentratorState.putByteBuffer(bf)){
|
| | | isSuccess = true; //读取系统状态成功
|
| | | }
|
| | | }
|
| | |
|
| | | if(Ccmd.ByteCount == Concentrator_Param.REG_COUNT*2){
|
| | | //读取系统参数
|
| | | if(concentratorParam.putByteBuffer(bf)){
|
| | | Concentrator_State.monCount = concentratorParam.countMonCount();
|
| | | isSuccess = true;
|
| | | }
|
| | | }
|
| | |
|
| | | if(Ccmd.ByteCount == Concentrator_State.MON_REG_COUNT){
|
| | | //读取单体电压/内阻/温度
|
| | | if(concentratorState.putMonDataBuffer(bf,model.cmd)) {
|
| | | isSuccess = true;
|
| | | }
|
| | | }
|
| | | }else if(Concentrator_ComBase.CMD_MODEBUS_SINGLEWRITECMD == Ccmd.CMD ||
|
| | | Concentrator_ComBase.CMD_MODEBUS_WRITECMD == Ccmd.CMD ){
|
| | | if(Ccmd.CMD == model.cmd_type){
|
| | | //写入数据成功
|
| | | isSuccess = true;
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | return isSuccess;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 尝试连接BTS设备
|
| | | */
|
| | | public void initConnectDevice(){
|
| | | boolean flag = false;
|
| | | try {
|
| | | // SerialPortFinder finder = new SerialPortFinder();
|
| | | // String[] devicesPath = finder.getAllDevicesPath();
|
| | | // for (int i =0;i<devicesPath.length;i++){
|
| | | // Log.d(TAG, "initConnectDevice: "+devicesPath[i]);
|
| | | // }
|
| | | this.socket = new SerialPort(new File(portName),portrate,0);
|
| | | flag = true;
|
| | | } catch (Exception e) {
|
| | | flag = false;
|
| | | e.printStackTrace();
|
| | | }finally {
|
| | | Log.d(TAG, "initConnectDevice: ERROR\t"+this.socket );
|
| | | if(!flag){
|
| | | if(this.socket != null){
|
| | | this.socket.close();
|
| | | this.socket = null;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | public byte[] makeComData(ServiceModel model){
|
| | | byte[] b = null;
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_READCMD == model.cmd_type ||
|
| | | Concentrator_ComBase.CMD_MODEBUS_WRITECMD == model.cmd_type ||
|
| | | Concentrator_ComBase.CMD_MODEBUS_SINGLEWRITECMD == model.cmd_type
|
| | | ){
|
| | | Concentrator_Cmd Ccmd = new Concentrator_Cmd();
|
| | | Ccmd.makeCmd(0xFF,model.cmd_type,model.cmd);
|
| | | ByteBuffer buffer = null;
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_SYSTEMPARAM_INDEX == model.cmd){
|
| | | //读写汇集器参数
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_READCMD == model.cmd_type){
|
| | | //读取汇集器参数
|
| | | buffer = concentratorParam.getReadByteBuffer();
|
| | | }else{
|
| | | //写汇集器参数
|
| | | Concentrator_Param param = gson.fromJson(model.msg,Concentrator_Param.class);
|
| | | buffer = param.getWriteByteBuffer();
|
| | | }
|
| | | }
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_DEVICESTATE_INDEX == model.cmd){
|
| | | //读取系统状态
|
| | | buffer = Ccmd.getReadByteBuffer(Concentrator_State.SYS_REG_COUNT);
|
| | | }
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_BATTMONVOL_INDEX == model.cmd ||
|
| | | Concentrator_ComBase.CMD_MODEBUS_BATTMONTMP_INDEX == model.cmd ||
|
| | | Concentrator_ComBase.CMD_MODEBUS_BATTMONRES_INDEX == model.cmd ){
|
| | | //读取单体数据(电压/温度/内阻)
|
| | | buffer = Ccmd.getReadByteBuffer(Concentrator_State.MON_REG_COUNT);
|
| | | }
|
| | |
|
| | | if(Concentrator_ComBase.CMD_STARTRESTEST_INDEX == model.cmd ||
|
| | | Concentrator_ComBase.CMD_STOPRESTTEST_INDEX == model.cmd ||
|
| | | Concentrator_ComBase.CMD_RESTART_INDEX == model.cmd
|
| | | ){
|
| | | //控制时传值(内阻测试[启动停止]/重启系统)
|
| | | buffer = Ccmd.getReadByteBuffer(Concentrator_ComBase.CMD_CONTROL_VALUE);
|
| | | }
|
| | | b = makeCommBuf(Ccmd,buffer);
|
| | | }else{
|
| | | b = new byte[0];
|
| | | }
|
| | | return b;
|
| | | }
|
| | |
|
| | | /*
|
| | | * 构造发送数据
|
| | | */
|
| | | public byte[] makeCommBuf(Concentrator_Cmd m_CMD, ByteBuffer bf)
|
| | | {
|
| | | ByteBuffer bbf = Concentrator_ComBuf.makeTmpSensorCommBuf(m_CMD, bf);
|
| | | byte byte_rest[] = new byte[bbf.limit()];
|
| | | bbf.get(byte_rest);
|
| | | return byte_rest;
|
| | | }
|
| | | }
|
New file |
| | |
| | | package com.util;
|
| | |
|
| | | import java.io.IOException;
|
| | | import java.text.DateFormat;
|
| | | import java.text.ParseException;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.Date;
|
| | |
|
| | | public class Com {
|
| | | final public static int UploadData_ClientType_BS_CLI = 0;
|
| | | final public static int UploadData_ClientType_CS_CLI = 1;
|
| | | final public static int UploadData_ClientType_CS_SVR = 2;
|
| | |
|
| | | final public static String DTF_YMDhms = "yyyy-MM-dd HH:mm:ss";
|
| | | final public static String DTF_YMDhm = "yyyy-MM-dd HH:mm";
|
| | | final public static String DTF_YMDh = "yyyy-MM-dd HH";
|
| | | final public static String DTF_YMD = "yyyy-MM-dd";
|
| | | final public static String DTFYMD = "yyyyMMdd";
|
| | | final public static String DTF_hms = "HH:mm:ss";
|
| | | final public static String DTF_YMD_h_m_s = "yyyy-MM-dd+HH_mm_ss";
|
| | | //final public static DateFormat DateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | |
|
| | | /**
|
| | | * 将传入的时间转换成指定的的时间格式并返回时间格式化之后的字符串
|
| | | * @param dt 需要转换的时间
|
| | | * @param format 格式化字符串
|
| | | * @return
|
| | | */
|
| | | public static String getDateTimeFormat(Date dt, String format){
|
| | | DateFormat dtf = new SimpleDateFormat(format);
|
| | | return dtf.format(dt);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 将传入的字符串按指定的格式解析成时间类型并返回
|
| | | * @param dt 需要解析的字符串
|
| | | * @param format 指定的时间格式
|
| | | * @return
|
| | | */
|
| | | public static Date getDateTimeFromStr(String dt, String format){
|
| | | DateFormat dtf = new SimpleDateFormat(format);
|
| | | Date date = new Date();
|
| | | try {
|
| | | date = dtf.parse(dt);
|
| | | } catch (ParseException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return date;
|
| | | }
|
| | |
|
| | | public static String getNowTimeWithAt() {
|
| | | return " @ " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 判断number参数是否是整型数表示方式
|
| | | * @param number
|
| | | * @return
|
| | | */
|
| | | public static boolean isIntegerNumber(String number){
|
| | | number = number.trim();
|
| | | String intNumRegex = "\\-{0,1}\\d+"; //整数的正则表达式
|
| | | if(number.matches(intNumRegex))
|
| | | return true;
|
| | | else
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 判断number参数是否是浮点数表示方式
|
| | | * @param number
|
| | | * @return
|
| | | */
|
| | | public static boolean isFloatPointNumber(String number){
|
| | | number = number.trim();
|
| | | String intNumRegex = "\\-{0,1}\\d+"; //整数的正则表达式
|
| | | String pointPrefix = "(\\-|\\+){0,1}\\d*\\.\\d+"; //浮点数的正则表达式-小数点在中间与前面
|
| | | String pointSuffix = "(\\-|\\+){0,1}\\d+\\."; //浮点数的正则表达式-小数点在后面
|
| | | if(number.matches(intNumRegex) || number.matches(pointPrefix) || number.matches(pointSuffix))
|
| | | return true;
|
| | | else
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 判断number参数是否是日期表示方式
|
| | | * @param str
|
| | | * @return
|
| | | */
|
| | | public static boolean isValidDate(String str, String format) {
|
| | | boolean convertSuccess = true;
|
| | | try {
|
| | | //DateTimeFormat.setLenient(false);
|
| | | DateFormat dtf = new SimpleDateFormat(format);
|
| | | dtf.parse(str);
|
| | | } catch (ParseException e) {
|
| | | convertSuccess = false;
|
| | | }
|
| | | return convertSuccess;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 设置系统的日期时间
|
| | | * @param ts
|
| | | * @return
|
| | | */
|
| | | public static void setDateTime(Date ts){
|
| | | String osName = System.getProperty("os.name");
|
| | | String cmd = "";
|
| | |
|
| | | try {
|
| | | if (osName.matches("^(?i)Windows.*$")) {// Window 系统
|
| | | // 格式 HH:mm:ss
|
| | | cmd = "cmd /c time " + getDateTimeFormat(ts, Com.DTF_hms);
|
| | | Runtime.getRuntime().exec(cmd);
|
| | | // 格式:yyyy-MM-dd
|
| | | cmd = "cmd /c date " + getDateTimeFormat(ts, Com.DTF_YMD);
|
| | | Runtime.getRuntime().exec(cmd);
|
| | | } else {// Linux 系统
|
| | | // 格式:yyyyMMdd
|
| | | cmd = "date -s " + getDateTimeFormat(ts, Com.DTFYMD);
|
| | | Runtime.getRuntime().exec(cmd);
|
| | | // 格式 HH:mm:ss
|
| | | cmd = "date -s " + getDateTimeFormat(ts, Com.DTF_hms);
|
| | | Runtime.getRuntime().exec(cmd);
|
| | | }
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | *
|
| | | * @param
|
| | | * @return
|
| | | */
|
| | | public static void getIPFromStr(String ipstr, byte ip[])
|
| | | {
|
| | | try
|
| | | {
|
| | | for(int n=0; n < 3; n++)
|
| | | {
|
| | | int index = ipstr.indexOf('.');
|
| | | if(index > 0)
|
| | | ip[n] = (byte)Integer.parseInt(ipstr.substring(0, index));
|
| | |
|
| | | ipstr = ipstr.substring(index+1);
|
| | | }
|
| | | ip[3] = (byte)Integer.parseInt(ipstr);
|
| | |
|
| | | //System.out.println((ip[0]&0xFF) + ": "+(ip[1]&0xFF) + ": "+(ip[2]&0xFF)+ ": "+(ip[3]&0xFF));
|
| | | }
|
| | | catch(Exception e)
|
| | | {
|
| | | System.out.println(e.getMessage());
|
| | | }
|
| | | }
|
| | | }
|
New file |
| | |
| | | package com.util;
|
| | |
|
| | | public class ComBase |
| | | {
|
| | | public static final byte CapType_Rest = 0;
|
| | | public static final byte CapType_Real = 1;
|
| | | //------------------------------------------------------------------------------
|
| | | //------------------------------------------------------------------------------
|
| | | public static byte changeIntToByte(int data)
|
| | | {
|
| | | return (byte)(data & 0xFF);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | public static short changeIntToShort(int data)
|
| | | {
|
| | | return (short)(data & 0xFFFF);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | public static byte changeShortToByte(short data)
|
| | | {
|
| | | return (byte)(data & 0xFF);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | public static int changeByteToInt(byte data)
|
| | | {
|
| | | int tmp = data;
|
| | | return (tmp & 0xFF);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | public static int changeShortToInt(short data)
|
| | | {
|
| | | int tmp = data;
|
| | | return (tmp & 0xFFFF);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | public static double changeShortToDouble(short data)
|
| | | {
|
| | | int tmp = data & 0xFFFF;
|
| | | return (double)(tmp);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | public static short changeDoubleToShort(double data)
|
| | | {
|
| | | int tmp = (int)data;
|
| | | return (short)(tmp & 0xFFFF);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | //------------------------------------------------------------------------------
|
| | | public static double GetFDCurrent(double stdcap, int hourrate)
|
| | | {
|
| | | double res = 0.055;
|
| | | switch(hourrate)
|
| | | {
|
| | | case 1: res = 0.514; break;
|
| | | case 2: res = 0.306; break;
|
| | | case 3: res = 0.250; break;
|
| | | case 4: res = 0.200; break;
|
| | | case 5: res = 0.166; break;
|
| | | case 6: res = 0.146; break;
|
| | | case 7: res = 0.131; break;
|
| | | case 8: res = 0.118; break;
|
| | | case 9: res = 0.108; break;
|
| | | case 10: res = 0.100; break;
|
| | | case 20: res = 0.055; break;
|
| | | default: res = 0.055; break;
|
| | | }
|
| | | |
| | | return (stdcap * res);
|
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | //------------------------------------------------------------------------------
|
| | | public static int GetHourRate(double stdah, double current)
|
| | | {
|
| | | int index = 0;
|
| | | double value[]={5.14, 3.06, 2.50, 2.00, 1.66, 1.46, 1.31, 1.18, 1.08, 1.00, 0.55};
|
| | | double res;
|
| | | |
| | | if(stdah < 1)
|
| | | stdah = 1;
|
| | | |
| | | res = current/(stdah/10);
|
| | | if(res >= 5.14) return 1;
|
| | | else if(res <= 0.55) return 20;
|
| | | else
|
| | | {
|
| | | for(index=0; index<10; index++)
|
| | | {
|
| | | if((res<=value[index]) && (res>value[index+1])) break;
|
| | | else continue;
|
| | | }
|
| | | if((value[index]-res) < (res-value[index+1]))
|
| | | {
|
| | | return (index+1);
|
| | | }
|
| | | else
|
| | | {
|
| | | if(index+2 > 10) return (20);
|
| | | else return (index+2);
|
| | | }
|
| | | } |
| | | }
|
| | | //------------------------------------------------------------------------------
|
| | | //------------------------------------------------------------------------------
|
| | | public static double N_TO_10H(int n_H)
|
| | | {
|
| | | switch(n_H)
|
| | | {
|
| | | case 1 : return(1/0.55);
|
| | | case 2 : return(1/0.61);
|
| | | case 3 : return(1/0.75);
|
| | | case 4 : return(1/0.79);
|
| | | case 5 : return(1/0.833);
|
| | | case 6 : return(1/0.876);
|
| | | case 7 : return(1/0.917);
|
| | | case 8 : return(1/0.944);
|
| | | case 9 : return(1/0.974);
|
| | | case 10: return(1/1);
|
| | | case 20: return(1/1.1);
|
| | | }
|
| | | return 1.0;
|
| | | }
|
| | | //-------------------------------------------------------------------------------
|
| | | //-------------------------------------------------------------------------------
|
| | | public static double GetMonomerCap(double STDAH, int HourRate, double SumAH, double MaxMonomerVol,
|
| | | double MonomerVol, double MonomerVolType, byte CapType)
|
| | | {
|
| | | if(MaxMonomerVol - MonomerVolType*0.9 <= 0)
|
| | | return 0;
|
| | |
|
| | | if(STDAH < 1)
|
| | | STDAH = 1;
|
| | | |
| | | if(SumAH < 0)
|
| | | SumAH *= (-1);
|
| | |
|
| | | double tmp_cap;
|
| | | tmp_cap = MonomerVol - MonomerVolType * 0.9;
|
| | | tmp_cap *= (STDAH - SumAH * N_TO_10H(HourRate));
|
| | | double dt_vol = MaxMonomerVol - MonomerVolType*0.9;
|
| | | if(dt_vol < 0.01)
|
| | | dt_vol = 0.01;
|
| | | tmp_cap = tmp_cap/dt_vol;
|
| | | if(tmp_cap < 0)
|
| | | tmp_cap = 0;
|
| | |
|
| | | if(CapType == CapType_Rest)
|
| | | return tmp_cap;
|
| | | else if(CapType == CapType_Real)
|
| | | return (tmp_cap + SumAH * N_TO_10H(HourRate));
|
| | | else
|
| | | return ((tmp_cap + SumAH * N_TO_10H(HourRate))*100 / STDAH);
|
| | | }
|
| | | //----------------------------------------------------------------------------------
|
| | | //----------------------------------------------------------------------------------
|
| | | public static int GetRestTimeSecond(double restcap, double curr)
|
| | | {
|
| | | double tmp_curr = Math.abs(curr);
|
| | | if(tmp_curr < 0.1)
|
| | | tmp_curr = 0.1;
|
| | | |
| | | int rest_time = (int)((restcap / tmp_curr) * 3600);
|
| | | if(rest_time > (99*3600))
|
| | | rest_time = (99*3600);
|
| | | |
| | | return rest_time;
|
| | | }
|
| | | //----------------------------------------------------------------------------------
|
| | | }
|
| | | /***************************************************************************************
|
| | | ******************************* end of file (FBS_ComBase)*******************************
|
| | | ***************************************************************************************/ |
New file |
| | |
| | | package com.util;
|
| | |
|
| | | import java.nio.ByteBuffer;
|
| | |
|
| | | public class Crc16
|
| | | {
|
| | | /*
|
| | | private final static int CRC16Table[] =
|
| | | {
|
| | | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
|
| | | 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
|
| | | 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
|
| | | 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
|
| | | 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
|
| | | 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
|
| | | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
|
| | | 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
|
| | | 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
|
| | | 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
|
| | | 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
|
| | | 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
|
| | | 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
|
| | | 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
|
| | | 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
|
| | | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
|
| | | 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
|
| | | 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
|
| | | 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
|
| | | 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
|
| | | 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
|
| | | 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
|
| | | 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
|
| | | 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
|
| | | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
|
| | | 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
|
| | | 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
|
| | | 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
|
| | | 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
|
| | | 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
|
| | | 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
|
| | | 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
|
| | | };
|
| | |
|
| | | public static short CalCRC16(final byte[] data_byte, int count)
|
| | | {
|
| | | byte[] tmpbuf = data_byte;
|
| | |
|
| | | int crc = 0xFFFF; //关键性的初值...
|
| | | int tmp;
|
| | | for (int i=0; i<count; i++)
|
| | | {
|
| | | tmp = crc;
|
| | | crc = CRC16Table[(int)((((tmp&0xFFFF) >> 8) ^ tmpbuf[i]) & 0xFF)];
|
| | | crc = crc ^ ((tmp&0xFFFF) << 8);
|
| | | }
|
| | |
|
| | | return (short) (crc & 0xFFFF);
|
| | | }
|
| | | */
|
| | | private final static char[] auchCRCHi =
|
| | | {
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
|
| | | 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
|
| | | 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
|
| | | 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
|
| | | 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
|
| | | 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
| | | 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
|
| | | 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
|
| | | 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
| | | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
|
| | | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
|
| | | };
|
| | | private final static short[] auchCRCLo =
|
| | | {
|
| | | 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
|
| | | 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
|
| | | 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
|
| | | 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
|
| | | 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
|
| | | 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
|
| | | 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
|
| | | 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
|
| | | 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
|
| | | 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
|
| | | 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
|
| | | 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
|
| | | 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
|
| | | 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
|
| | | 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
|
| | | 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
|
| | | 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
|
| | | 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
|
| | | 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
|
| | | 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
|
| | | 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
|
| | | 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
|
| | | 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
|
| | | 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
|
| | | 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
|
| | | 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
|
| | | };
|
| | | //------------局部函数声明区------------------------------------------------------------------------------
|
| | |
|
| | | //--------------------------------------------------------------------------------------------------------
|
| | |
|
| | |
|
| | | /************************************************************************
|
| | | ** 函数名:CalModbusCRC16
|
| | | ** 功 能:modbus CRC计算
|
| | | ** 输 入: 1,待校验数据地址
|
| | | ** 2,待校验数据长度(字节数)
|
| | | ** 输 出:无
|
| | | ** 返回值:计算的CRC
|
| | | ** 备 注:
|
| | | ** 时 间: [2014/08/26 16:29:34]
|
| | | ** 修订人:
|
| | | ************************************************************************/
|
| | | public static short CalCRC16 (byte[] puchMsg, int usDataLen)
|
| | | {
|
| | | short uchCRCHi = 0xFF;
|
| | | short uchCRCLo = 0xFF;
|
| | | int uIndex ;
|
| | |
|
| | | for(int n=0; n<usDataLen; n++)
|
| | | {
|
| | | uIndex = uchCRCHi ^ ((short)(puchMsg[n]&0xFF));
|
| | | uchCRCHi = (short) (uchCRCLo ^ ((short)(auchCRCHi[uIndex]&0xFF)));
|
| | | uchCRCLo = (short) (auchCRCLo[uIndex]&0xFF);
|
| | | }
|
| | |
|
| | | return (short)((uchCRCLo) | (uchCRCHi<<8));
|
| | | };
|
| | |
|
| | | public static short CalCRC16 (ByteBuffer bf, int usDataLen)
|
| | | {
|
| | | short uchCRCHi = 0xFF;
|
| | | short uchCRCLo = 0xFF;
|
| | | int uIndex ;
|
| | |
|
| | | bf.position(0);
|
| | | for(int n=0; n<usDataLen; n++)
|
| | | {
|
| | | uIndex = uchCRCHi ^ ((short)(bf.get()&0xFF));
|
| | | uchCRCHi = (short) (uchCRCLo ^ ((short)(auchCRCHi[uIndex]&0xFF)));
|
| | | uchCRCLo = (short) (auchCRCLo[uIndex]&0xFF);
|
| | | }
|
| | |
|
| | | return (short)((uchCRCLo) | (uchCRCHi<<8));
|
| | | };
|
| | | }
|
| | | /***************************************************************************************
|
| | | ****************************** end of file (FBS_Crc16) *********************************
|
| | | ***************************************************************************************/
|
| | |
| | | org.gradle.daemon=true |
| | | org.gradle.jvmargs=-Xmx2048m |
| | | android.useDeprecatedNdk=true |
| | | android.useDeprecatedNdk=true |
| | | android.enableAapt2=false |