Browse Source

1、修复计算主机参数位数;

dev
mh 6 months ago
parent
commit
c82fb236af
  1. 9
      user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java
  2. 54
      user-service/src/main/java/com/mh/user/utils/ExchangeStringUtil.java
  3. 38
      user-service/src/test/java/com/mh/user/device/CRC16Test.java

9
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:

54
user-service/src/main/java/com/mh/user/utils/ExchangeStringUtil.java

@ -63,6 +63,7 @@ public class ExchangeStringUtil {
/**
* 获取到对应的buffer
*
* @param ctx
* @param sendStr
* @return
@ -80,6 +81,7 @@ public class ExchangeStringUtil {
/**
* double转换为String 当为整数时只显示整数当小数时直接显示小数
*
* @param num
* @return
*/
@ -92,6 +94,7 @@ public class ExchangeStringUtil {
/**
* 获取String中的数值
*
* @param result
* @return
*/
@ -104,6 +107,7 @@ public class ExchangeStringUtil {
/**
* 获取检验位值update by ljf on 2020-06-02
*
* @param result
* @return
*/
@ -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,6 +202,7 @@ public class ExchangeStringUtil {
/**
* ip地址转换成16进制long
*
* @param ipString
* @return
*/
@ -280,6 +308,7 @@ public class ExchangeStringUtil {
}
return result.toUpperCase();
}
/**
* 十六进制数据转换为十进制字符串数
*
@ -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,9 +449,9 @@ public class ExchangeStringUtil {
}
/**
* 字节转十六进制
*
* @param b 需要进行转换的byte字节
* @return 转换后的Hex字符串
*/
@ -433,6 +465,7 @@ public class ExchangeStringUtil {
/**
* hex字符串转byte数组
*
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
@ -496,7 +529,7 @@ public class ExchangeStringUtil {
/**
* 字符串是否为空
*
* <p>
* 如果这个字符串为null或者trim后为空字符串则返回true否则返回false
*
* @param str
@ -1095,12 +1128,10 @@ public class ExchangeStringUtil {
/**
*
* 获取实时天气2<br>
* getTodayWeather <br>
*
* @param Cityid
* 城市编码
* @param Cityid 城市编码
*/
public static Map<String, Object> getTodayWeather1(String Cityid)
throws IOException, NullPointerException {
@ -1166,6 +1197,7 @@ public class ExchangeStringUtil {
/**
* 时间获得星期
*
* @param strDate
* @return
* @throws ParseException
@ -1178,8 +1210,10 @@ public class ExchangeStringUtil {
String week = sdf.format(c.getTime());
return week;
}
/**
* 字符集转码
*
* @param url
* @return
* @throws UnsupportedEncodingException
@ -1206,12 +1240,9 @@ public class ExchangeStringUtil {
/**
* 判断时间是否在时间段内
*
* @param date
* 当前时间 yyyy-MM-dd HH:mm:ss
* @param strDateBegin
* 开始时间 00:00:00
* @param strDateEnd
* 结束时间 00:05:00
* @param date 当前时间 yyyy-MM-dd HH:mm:ss
* @param strDateBegin 开始时间 00:00:00
* @param strDateEnd 结束时间 00:05:00
* @return
*/
public static boolean isInDate(Date date, String strDateBegin,
@ -1268,6 +1299,7 @@ public class ExchangeStringUtil {
/**
* 当前时间向推几小时
*
* @param ihour 小时
* @return String
*/

38
user-service/src/test/java/com/mh/user/device/CRC16Test.java

@ -3,6 +3,8 @@ package com.mh.user.device;
import com.mh.user.utils.ExchangeStringUtil;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* @Classname CRC16Test
@ -67,11 +69,37 @@ public class CRC16Test {
//测试
public static void main(String[] args) throws IOException {
// c0a8020a1f90ff04fb0000000100010200000300000400000500000600000700140800000901cb0a00000b00000c00000d00000e00000f0000100000110025120000130910
byte[] buffer;
String string = "c0a8020a1f90ff02fd0000000100000200000300000400000500000600000700000800010900000a00780b00010c8c310d00000e00000f0000100000110022120000132d1e140000150000160000170001182c381900001a01a61b00001c00001d00011e00011f25cd20000121b1332200002300002400002500002600712700002834202900002a00002b00002c00002d00402e00002f0363a6ff";
buffer = ExchangeStringUtil.HexString2Bytes(string);
int crc16 = CRC16Test.calcCrc16(buffer,buffer.length);
System.out.println(String.format("0x%04x", crc16));//0x7c09
// byte[] buffer;
// String string = "c0a8020a1f90ff02fd0000000100000200000300000400000500000600000700000800010900000a00780b00010c8c310d00000e00000f0000100000110022120000132d1e140000150000160000170001182c381900001a01a61b00001c00001d00011e00011f25cd20000121b1332200002300002400002500002600712700002834202900002a00002b00002c00002d00402e00002f0363a6ff";
// buffer = ExchangeStringUtil.HexString2Bytes(string);
// int crc16 = CRC16Test.calcCrc16(buffer,buffer.length);
// System.out.println(String.format("0x%04x", crc16));//0x7c09
// String str;
// str = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex("3"), 2) // 设备地址
// + ExchangeStringUtil.addZeroForNum("3", 2) // 功能码
// + ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex("0010"), 4) // 寄存器地址
// + ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(String.valueOf("1")), 4); // 读取寄存器个数
// // 循环冗余校验
// String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验
// str = str + checkWord;
// System.out.println(str);
String receiveData = "04030202CDB4B1";
String dataLength = receiveData.substring(4, 6);
int dataLengthInt = Integer.parseInt(dataLength, 16);
// 截取数据域
String data = receiveData;
data = receiveData.substring(6, 6 + dataLengthInt * 2);
System.out.println(data);
String sValue = ExchangeStringUtil.hexToDec(data);
System.out.println(ExchangeStringUtil.hexToDec(data));
System.out.println(ExchangeStringUtil.rightAddZeroForNum("1", 2+1));
BigDecimal test1 = new BigDecimal(ExchangeStringUtil.rightAddZeroForNum("1", 2+1));
System.out.println(test1);
sValue = (new BigDecimal(sValue)).divide(test1, 2, RoundingMode.HALF_UP).toString();
System.out.println(sValue);
String test = String.valueOf(Math.abs(ExchangeStringUtil.hexToSingle(data)));
System.out.println(test);
}
}

Loading…
Cancel
Save