diff --git a/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java b/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java index 9a74247..c273faf 100644 --- a/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java +++ b/user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java @@ -62,7 +62,7 @@ public class ModbusProtocolStrategy implements ProtocolStrategy { // 地址(1) + 功能码(1) + 寄存器地址(2) + 数据域(2) + crc校验 str = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getMtCode()), 2) // 设备地址 + ExchangeStringUtil.addZeroForNum(meterManageEntity.getFuncCode(), 2) // 功能码 - + ExchangeStringUtil.addZeroForNum(meterManageEntity.getRegisterAddr(), 4) // 寄存器地址 + + ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getRegisterAddr()), 4) // 寄存器地址 + ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(String.valueOf(meterManageEntity.getRegisterSize())), 4); // 读取寄存器个数 // 循环冗余校验 String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验 @@ -111,9 +111,12 @@ public class ModbusProtocolStrategy implements ProtocolStrategy { sValue = ExchangeStringUtil.hexToDec(data); if (deviceCodeParamEntity.getDigit() == 0) { sValue = String.valueOf(Long.parseLong(sValue)); - } else { + } else if (deviceCodeParamEntity.getDigit() < 0) { + sValue = String.valueOf(Long.parseLong(sValue)) + ExchangeStringUtil.addZeroForNum("0", -deviceCodeParamEntity.getDigit()); + } + else { // 保留位数 - sValue = (new BigDecimal(sValue)).divide(new BigDecimal(String.valueOf(deviceCodeParamEntity.getDigit() * 10)), 2, RoundingMode.HALF_UP).toString(); + sValue = (new BigDecimal(sValue)).divide(new BigDecimal(ExchangeStringUtil.rightAddZeroForNum("1", deviceCodeParamEntity.getDigit()+1)), 2, RoundingMode.HALF_UP).toString(); } break; case 2: diff --git a/user-service/src/main/java/com/mh/user/utils/ExchangeStringUtil.java b/user-service/src/main/java/com/mh/user/utils/ExchangeStringUtil.java index ce7dfed..9d0d21e 100644 --- a/user-service/src/main/java/com/mh/user/utils/ExchangeStringUtil.java +++ b/user-service/src/main/java/com/mh/user/utils/ExchangeStringUtil.java @@ -30,7 +30,7 @@ import java.util.regex.Pattern; */ public class ExchangeStringUtil { - public static void main(String args[]){ + public static void main(String args[]) { // int ieee754Int = Integer.parseInt(str, 16); // float realValue = Float.intBitsToFloat(ieee754Int); @@ -63,6 +63,7 @@ public class ExchangeStringUtil { /** * 获取到对应的buffer + * * @param ctx * @param sendStr * @return @@ -80,23 +81,25 @@ public class ExchangeStringUtil { /** * double转换为String :当为整数时,只显示整数,当小数时直接显示小数 + * * @param num * @return */ - public static String doubleTrans1(double num){ - if(num % 1.0 == 0){ - return String.valueOf((long)num); + public static String doubleTrans1(double num) { + if (num % 1.0 == 0) { + return String.valueOf((long) num); } return String.valueOf(num); } /** * 获取String中的数值 + * * @param result * @return */ public static String getNumFromString(String result) { - String regEx="[^0-9]"; + String regEx = "[^0-9]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(result); return m.replaceAll("").trim(); @@ -104,6 +107,7 @@ public class ExchangeStringUtil { /** * 获取检验位值,update by ljf on 2020-06-02 + * * @param result * @return */ @@ -112,8 +116,8 @@ public class ExchangeStringUtil { byte[] strOrder = ExchangeStringUtil.hexStrToBinaryStr(result); int checkNum = CRC16.CRC16_MODBUS(strOrder); String checkWord = ExchangeStringUtil.decToHex(String.valueOf(checkNum)); - checkWord = ExchangeStringUtil.addZeroForNum(checkWord,4); - checkWord = checkWord.substring(2,4) + checkWord.substring(0,2); + checkWord = ExchangeStringUtil.addZeroForNum(checkWord, 4); + checkWord = checkWord.substring(2, 4) + checkWord.substring(0, 2); result = result + checkWord; return result; } @@ -123,8 +127,8 @@ public class ExchangeStringUtil { byte[] strOrder = ExchangeStringUtil.hexStrToBinaryStr(result); int checkNum = CRC16.CRC16_MODBUS(strOrder); String checkWord = ExchangeStringUtil.decToHex(String.valueOf(checkNum)); - checkWord = ExchangeStringUtil.addZeroForNum(checkWord,4); - checkWord = checkWord.substring(2,4) + checkWord.substring(0,2); + checkWord = ExchangeStringUtil.addZeroForNum(checkWord, 4); + checkWord = checkWord.substring(2, 4) + checkWord.substring(0, 2); return checkWord; } @@ -141,6 +145,7 @@ public class ExchangeStringUtil { /** * 字符串不足补“0” + * * @param str * @param strLength * @return @@ -159,8 +164,30 @@ public class ExchangeStringUtil { return str; } + /** + * 字符串不足右边补“0” + * + * @param str + * @param strLength + * @return + */ + public static String rightAddZeroForNum(String str, int strLength) { + int strLen = str.length(); + if (strLen < strLength) { + while (strLen < strLength) { + StringBuffer sb = new StringBuffer(); +// sb.append("0").append(str);// 左补0 + sb.append(str).append("0");//右补0 + str = sb.toString(); + strLen = str.length(); + } + } + return str; + } + /** * ip地址转换成16进制long + * * @param ipString * @return */ @@ -175,19 +202,20 @@ public class ExchangeStringUtil { /** * ip地址转换成16进制long + * * @param ipString * @return */ public static Long ipToLong(String ipString) { Long[] ip = new Long[4]; - int pos1= ipString.indexOf("."); - int pos2= ipString.indexOf(".",pos1+1); - int pos3= ipString.indexOf(".",pos2+1); - ip[0] = Long.parseLong(ipString.substring(0 , pos1)); - ip[1] = Long.parseLong(ipString.substring(pos1+1 , pos2)); - ip[2] = Long.parseLong(ipString.substring(pos2+1 , pos3)); - ip[3] = Long.parseLong(ipString.substring(pos3+1)); - return (ip[0]<<24)+(ip[1]<<16)+(ip[2]<<8)+ip[3]; + int pos1 = ipString.indexOf("."); + int pos2 = ipString.indexOf(".", pos1 + 1); + int pos3 = ipString.indexOf(".", pos2 + 1); + ip[0] = Long.parseLong(ipString.substring(0, pos1)); + ip[1] = Long.parseLong(ipString.substring(pos1 + 1, pos2)); + ip[2] = Long.parseLong(ipString.substring(pos2 + 1, pos3)); + ip[3] = Long.parseLong(ipString.substring(pos3 + 1)); + return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; } public static String splitData(String str, String strStart, String strEnd) { @@ -198,8 +226,8 @@ public class ExchangeStringUtil { public static String endData(String str, String strStart) { String tempStr; - String str1=str.substring(0, str.indexOf(strStart)); - tempStr=str.substring(str1.length()+1); + String str1 = str.substring(0, str.indexOf(strStart)); + tempStr = str.substring(str1.length() + 1); return tempStr; } @@ -213,11 +241,11 @@ public class ExchangeStringUtil { } // 加33 - public static String addThree(String data){ + public static String addThree(String data) { String result = ""; for (int i = 0; i < data.length() / 2; i++) { - BigInteger a = new BigInteger(data.substring(2*i,2*(i+1)),16); - BigInteger b = new BigInteger("33",16); + BigInteger a = new BigInteger(data.substring(2 * i, 2 * (i + 1)), 16); + BigInteger b = new BigInteger("33", 16); BigInteger c = a.add(b); result = result + ExchangeStringUtil.decToHex(c.toString(10)); } @@ -226,11 +254,11 @@ public class ExchangeStringUtil { } // 减33 - public static String cutThree(String data){ + public static String cutThree(String data) { String result = ""; for (int i = 0; i < data.length() / 2; i++) { - BigInteger a = new BigInteger(data.substring(2*i,2*(i+1)),16); - BigInteger b = new BigInteger("33",16); + BigInteger a = new BigInteger(data.substring(2 * i, 2 * (i + 1)), 16); + BigInteger b = new BigInteger("33", 16); BigInteger c = a.subtract(b); result = result + ExchangeStringUtil.decToHex(c.toString(10)); } @@ -270,16 +298,17 @@ public class ExchangeStringUtil { * @return */ public static String decToHex(String dec) { - BigInteger data = new BigInteger(dec,10); + BigInteger data = new BigInteger(dec, 10); String result = data.toString(16); if (result.length() < 2) { result = "0" + result; } - if ((result.length() % 2)!=0) { + if ((result.length() % 2) != 0) { result = "0" + result; } return result.toUpperCase(); } + /** * 十六进制数据转换为十进制字符串数 * @@ -287,32 +316,32 @@ public class ExchangeStringUtil { * @return */ public static String hexToDec(String hex) { - BigInteger data = new BigInteger(hex,16); + BigInteger data = new BigInteger(hex, 16); return data.toString(10); } - public static String IntToHex(int n){ + public static String IntToHex(int n) { char[] ch = new char[20]; int nIndex = 0; - while ( true ){ - int m = n/16; - int k = n%16; - if ( k == 15 ) + while (true) { + int m = n / 16; + int k = n % 16; + if (k == 15) ch[nIndex] = 'F'; - else if ( k == 14 ) + else if (k == 14) ch[nIndex] = 'E'; - else if ( k == 13 ) + else if (k == 13) ch[nIndex] = 'D'; - else if ( k == 12 ) + else if (k == 12) ch[nIndex] = 'C'; - else if ( k == 11 ) + else if (k == 11) ch[nIndex] = 'B'; - else if ( k == 10 ) + else if (k == 10) ch[nIndex] = 'A'; else - ch[nIndex] = (char)('0' + k); + ch[nIndex] = (char) ('0' + k); nIndex++; - if ( m == 0 ) + if (m == 0) break; n = m; } @@ -338,7 +367,7 @@ public class ExchangeStringUtil { return sbf.toString().trim(); } - public static String bytesToHexString(byte[] src){ + public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; @@ -356,6 +385,7 @@ public class ExchangeStringUtil { /** * 字符串转化成为16进制字符串 + * * @param s * @return */ @@ -371,6 +401,7 @@ public class ExchangeStringUtil { /** * 字符串转换成为16进制(无需Unicode编码) + * * @param str * @return */ @@ -391,6 +422,7 @@ public class ExchangeStringUtil { /** * 16进制转换成为string类型字符串 + * * @param s * @return */ @@ -417,15 +449,15 @@ public class ExchangeStringUtil { } - /** * 字节转十六进制 + * * @param b 需要进行转换的byte字节 - * @return 转换后的Hex字符串 + * @return 转换后的Hex字符串 */ - public static String byteToHex(byte b){ + public static String byteToHex(byte b) { String hex = Integer.toHexString(b & 0xFF); - if(hex.length() < 2){ + if (hex.length() < 2) { hex = "0" + hex; } return hex; @@ -433,24 +465,25 @@ public class ExchangeStringUtil { /** * hex字符串转byte数组 + * * @param inHex 待转换的Hex字符串 - * @return 转换后的byte数组结果 + * @return 转换后的byte数组结果 */ - public static byte[] hexToByteArray(String inHex){ + public static byte[] hexToByteArray(String inHex) { int hexlen = inHex.length(); byte[] result; - if (hexlen % 2 == 1){ + if (hexlen % 2 == 1) { //奇数 hexlen++; - result = new byte[(hexlen/2)]; - inHex="0"+inHex; - }else { + result = new byte[(hexlen / 2)]; + inHex = "0" + inHex; + } else { //偶数 - result = new byte[(hexlen/2)]; + result = new byte[(hexlen / 2)]; } - int j=0; - for (int i = 0; i < hexlen; i+=2){ - result[j]=(byte)Integer.parseInt(inHex.substring(i,i+2),16); + int j = 0; + for (int i = 0; i < hexlen; i += 2) { + result[j] = (byte) Integer.parseInt(inHex.substring(i, i + 2), 16); j++; } return result; @@ -476,7 +509,7 @@ public class ExchangeStringUtil { String sub = hexString.substring(index, index + 2); - bytes[index/2] = (byte) Integer.parseInt(sub,16); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); index += 2; } @@ -496,7 +529,7 @@ public class ExchangeStringUtil { /** * 字符串是否为空 - * + *
* 如果这个字符串为null或者trim后为空字符串则返回true,否则返回false。
*
* @param str
@@ -923,9 +956,9 @@ public class ExchangeStringUtil {
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
- byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
+ byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte) (_b0 << 4);
- byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
+ byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
@@ -973,11 +1006,11 @@ public class ExchangeStringUtil {
}
//字符转二进制
- public static String toBinary(String str){
- char[] strChar=str.toCharArray();
- String result="";
- for(int i=0;i
*
- * @param Cityid
- * 城市编码
+ * @param Cityid 城市编码
*/
public static Map