package tbDataModel_Dell;
|
import JNADell.DellJAN;
|
import Method.HexConvert;
|
import tbDataModel_Dell.ModbusUtil;
|
|
@SuppressWarnings("unused")
|
public class Tools {
|
|
|
|
/**
|
* ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×é
|
*
|
* @param hexString
|
* @return
|
*/
|
public static byte[] hexStrToBinaryStr(String hexString) {
|
|
|
if (hexString==null) {
|
return null;
|
}
|
|
hexString = hexString.replaceAll(" ", "");
|
|
int len = hexString.length();
|
int index = 0;
|
|
|
byte[] bytes = new byte[len / 2];
|
|
while (index < len) {
|
|
String sub = hexString.substring(index, index + 2);
|
bytes[index/2] = (byte)Integer.parseInt(sub,16);
|
index += 2;
|
}
|
|
|
return bytes;
|
}
|
|
/**
|
* ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×鲻ȥ³ý¿Õ¸ñ
|
*
|
* @param hexString
|
* @return
|
*/
|
public static byte[] hexStrToBinaryStr2(String hexString) {
|
|
if (hexString==null) {
|
return null;
|
}
|
|
int len = hexString.length();
|
|
int index = 0;
|
|
|
byte[] bytes = new byte[len / 2];
|
|
while (index < len) {
|
String sub = hexString.substring(index, index + 2);
|
bytes[index/2] = (byte)Integer.parseInt(sub,16);
|
index += 2;
|
}
|
|
|
return bytes;
|
}
|
|
|
|
public static final int registersToInt(byte[] bytes) {
|
return (
|
((bytes[0] & 0xff) << 24) |
|
((bytes[1] & 0xff) << 16) |
|
((bytes[2] & 0xff) << 8) |
|
(bytes[3] & 0xff)
|
);
|
}//registersToInt
|
|
|
/**½«×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®*/
|
public static String Bytes2HexString(byte[] b) {
|
String ret = "";
|
for (int i = 0; i < b.length; i++) {
|
String hex = Integer.toHexString(b[i] & 0xFF);
|
if (hex.length() == 1) {
|
hex = '0' + hex;
|
}
|
ret += hex.toUpperCase();
|
}
|
return ret;
|
}
|
|
//assiccת16½øÖÆ
|
public static byte[] assictohex(byte[] bytes){
|
byte by[]=new byte[4];
|
by[0]=0;
|
by[1]=0;
|
by[2]=(byte) Integer.parseInt(Tools.convertHexToString(Tools.Bytes2HexString(bytes)).substring(0, 2), 16);
|
by[3]=(byte) Integer.parseInt(Tools.convertHexToString(Tools.Bytes2HexString(bytes)).substring(2, 4), 16);
|
|
return by;
|
|
}
|
|
|
|
public static String convertHexToString(String hex){
|
|
StringBuilder sb = new StringBuilder();
|
StringBuilder temp = new StringBuilder();
|
|
for( int i=0; i<hex.length()-1; i+=2 ){
|
|
//grab the hex in pairs
|
String output = hex.substring(i, (i + 2));
|
//convert hex to decimal
|
int decimal = Integer.parseInt(output, 16);
|
//convert the decimal to character
|
sb.append((char)decimal);
|
|
temp.append(decimal);
|
}
|
|
return sb.toString();
|
}
|
|
public static final float registersToFloat(byte[] bytes) {
|
return Float.intBitsToFloat((
|
((bytes[0] & 0xff) << 24) |
|
((bytes[1] & 0xff) << 16) |
|
((bytes[2] & 0xff) << 8) |
|
(bytes[3] & 0xff)
|
));
|
}//
|
@SuppressWarnings("unused")
|
private static String intToHex(int n) {
|
StringBuffer s = new StringBuffer();
|
String a;
|
char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
|
while(n != 0){
|
s = s.append(b[n%16]);
|
n = n/16;
|
}
|
a = s.reverse().toString();
|
return a;
|
}
|
|
//½«·µ»ØµÄÊý¾Ýת»¯Îª10½øÖÆÊý¾Ý
|
public static int ormntoint(byte[] bytes){
|
int aa=0;
|
aa=bytes[0]-48 +(bytes[1]-48)*10+(bytes[2]-48)*100+(bytes[3]-48)*1000;
|
return aa;
|
}
|
|
|
/**¶Á´®¿ÚÇëÇó¶ÁÊý¾Ý°ü
|
* @param ref ÆôʼµØÖ·
|
* @param lenth Êý¾Ý³¤¶È*/
|
public static byte[] ReadData(byte ref ,byte lenth) {
|
byte[] buf =new byte[10];
|
byte[] buf1 =new byte[6];
|
//°üÍ·
|
buf[0]=(byte) 0x55;
|
//°üÍ·
|
buf[1]=(byte) 0xAA;
|
|
//Ö¸ÁîÀàÐÍ(²ÎÊý¶Áдģʽ)
|
buf1[0]=(byte) 0x03;
|
//Êý¾Ý³¤¶È
|
buf1[1]=(byte) 0x06;
|
|
//¶Áģʽ(¶Áģʽ)
|
buf1[2]=(byte) 0x01;
|
//ÆðʼµØÖ·
|
buf1[3]=(byte) ref;
|
//Êý¾Ý³¤¶È
|
buf1[4]=(byte) lenth;
|
|
//¹Ì¶¨Öµ(¶ÁģʽÏÂΪ0x00)
|
buf1[5]=(byte) 0x00;
|
|
for(int i=0;i<buf1.length;i++) {
|
buf[i+2]=buf1[i];
|
}
|
//УÑéÂë
|
buf[8]=Jiaoyan.check(buf1)[1];
|
buf[9]=Jiaoyan.check(buf1)[0];
|
return buf;
|
}
|
|
|
//д´®¿ÚÇëÇó¶ÁÊý¾Ý°ü
|
public static byte[] WriteData(byte ref ,byte lenth,int value) {
|
byte[] buf =new byte[9+lenth];
|
byte[] buf1 =new byte[5+lenth];
|
//°üÍ·
|
buf[0]=(byte) 0x55;
|
//°üÍ·
|
buf[1]=(byte) 0xAA;
|
//Ö¸ÁîÀàÐÍ
|
buf1[0]=(byte) 0x03;
|
//Êý¾Ý³¤¶È
|
buf1[1]=(byte) ((byte) 5+lenth);
|
//¶Áģʽ
|
buf1[2]=(byte) 0x02;
|
//ÆðʼµØÖ·
|
buf1[3]=(byte) ref;
|
//Êý¾Ý³¤¶È
|
buf1[4]=(byte) lenth;
|
|
//дÈëÖµ
|
if(lenth==2) {
|
buf1[5]=ModbusUtil.intToRegisters(value)[2];
|
buf1[6]=ModbusUtil.intToRegisters(value)[3];
|
}
|
|
|
for(int i=0;i<buf1.length;i++) {
|
buf[i+2]=buf1[i];
|
}
|
//УÑéÂë
|
buf[buf.length-2]=Jiaoyan.check(buf1)[1];
|
buf[buf.length-1]=Jiaoyan.check(buf1)[0];
|
return buf;
|
}
|
|
//»ñÈ¡Ëã·¨°æ±¾
|
public static int suanfaVersion(){
|
String suanfaversion = DellJAN.getSuanfaversion();
|
String[] suanfa = suanfaversion.split("\\.");
|
return Integer.parseInt(suanfa[2]) ;
|
}
|
|
|
|
|
|
}
|