|
|
|
@ -61,8 +61,8 @@ public class ModbusProtocolStrategy implements ProtocolStrategy {
|
|
|
|
|
try { |
|
|
|
|
// 地址(1) + 功能码(1) + 寄存器地址(2) + 数据域(2) + crc校验
|
|
|
|
|
str = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getMtCode()), 2) // 设备地址
|
|
|
|
|
+ ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getFuncCode()), 2) // 功能码
|
|
|
|
|
+ ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getRegisterAddr()), 4) // 寄存器地址
|
|
|
|
|
+ ExchangeStringUtil.addZeroForNum(meterManageEntity.getFuncCode(), 2) // 功能码
|
|
|
|
|
+ ExchangeStringUtil.addZeroForNum(meterManageEntity.getRegisterAddr(), 4) // 寄存器地址
|
|
|
|
|
+ ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(String.valueOf(meterManageEntity.getRegisterSize())), 4); // 读取寄存器个数
|
|
|
|
|
// 循环冗余校验
|
|
|
|
|
String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验
|
|
|
|
@ -77,43 +77,63 @@ public class ModbusProtocolStrategy implements ProtocolStrategy {
|
|
|
|
|
@Override |
|
|
|
|
public String analysisReceiveData(DeviceCodeParamEntity deviceCodeParamEntity, String receiveData) { |
|
|
|
|
log.info("modbus标准协议:策略解析报文"); |
|
|
|
|
String checkStr = receiveData.substring(0, receiveData.length() - 4);//检验报文
|
|
|
|
|
String checkWord = ExchangeStringUtil.getStrCRC16(checkStr);//生成校验码
|
|
|
|
|
String sValue = "0"; |
|
|
|
|
String rtData = Constant.FAIL; |
|
|
|
|
if (!checkWord.equalsIgnoreCase(receiveData.substring(receiveData.length() - 4))) { |
|
|
|
|
log.info("Modbus报文检验失败: {}", receiveData); |
|
|
|
|
return rtData; |
|
|
|
|
} |
|
|
|
|
// 开始解析: 地址+功能码+数据长度+数据域
|
|
|
|
|
// 截取数据长度
|
|
|
|
|
String dataLength = receiveData.substring(4, 6); |
|
|
|
|
int dataLengthInt = Integer.parseInt(dataLength, 16); |
|
|
|
|
// 截取数据域
|
|
|
|
|
String data = receiveData.substring(6, 6 + dataLengthInt * 2); |
|
|
|
|
// 判断
|
|
|
|
|
switch (deviceCodeParamEntity.getDataType()) { |
|
|
|
|
case 0: |
|
|
|
|
case 1: |
|
|
|
|
// 16进制转十进制类型
|
|
|
|
|
sValue = ExchangeStringUtil.hexToDec(data); |
|
|
|
|
// 保留位数
|
|
|
|
|
sValue = (new BigDecimal(sValue)).divide(new BigDecimal(String.valueOf(deviceCodeParamEntity.getDigit() * 10)), 2, RoundingMode.HALF_UP).toString(); |
|
|
|
|
break; |
|
|
|
|
case 2: |
|
|
|
|
// 十六进制字符串转IEEE754浮点型
|
|
|
|
|
sValue = String.valueOf(ExchangeStringUtil.hexToSingle(data)); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
log.info("解析数据==>表号:{},寄存器地址:{},值:{}", deviceCodeParamEntity.getDeviceAddr(), deviceCodeParamEntity.getRegisterAddr(), sValue); |
|
|
|
|
// 入库数据
|
|
|
|
|
// 冷量表
|
|
|
|
|
if ("2".equals(deviceCodeParamEntity.getDeviceType())) { |
|
|
|
|
analysisCloudOrder485(sValue, deviceCodeParamEntity); |
|
|
|
|
} else if ("0".equals(deviceCodeParamEntity.getDeviceType())) { |
|
|
|
|
analysisChillerOrder485(sValue, deviceCodeParamEntity); |
|
|
|
|
String sValue = null; |
|
|
|
|
try { |
|
|
|
|
String checkStr = receiveData.substring(0, receiveData.length() - 4);//检验报文
|
|
|
|
|
String checkWord = ExchangeStringUtil.getStrCRC16(checkStr);//生成校验码
|
|
|
|
|
sValue = "0"; |
|
|
|
|
String rtData = Constant.FAIL; |
|
|
|
|
if (!checkWord.equalsIgnoreCase(receiveData.substring(receiveData.length() - 4))) { |
|
|
|
|
log.info("Modbus报文检验失败: {}", receiveData); |
|
|
|
|
return rtData; |
|
|
|
|
} |
|
|
|
|
// 开始解析: 地址+功能码+数据长度+数据域
|
|
|
|
|
// 03 01 01 01 91 F0
|
|
|
|
|
// 03 01 01 01 91 F0
|
|
|
|
|
// 截取数据长度
|
|
|
|
|
String dataLength = receiveData.substring(4, 6); |
|
|
|
|
int dataLengthInt = Integer.parseInt(dataLength, 16); |
|
|
|
|
// 截取数据域
|
|
|
|
|
String data = receiveData; |
|
|
|
|
if ("2".equals(deviceCodeParamEntity.getDeviceType())) { |
|
|
|
|
data = data.substring(data.length() - 8, data.length() - 6) |
|
|
|
|
+ data.substring(data.length() - 6, data.length() - 4) |
|
|
|
|
+ data.substring(data.length() - 12, data.length() - 10) |
|
|
|
|
+ data.substring(data.length() - 10, data.length() - 8); |
|
|
|
|
} else { |
|
|
|
|
data = receiveData.substring(6, 6 + dataLengthInt * 2); |
|
|
|
|
} |
|
|
|
|
// 判断
|
|
|
|
|
switch (deviceCodeParamEntity.getDataType()) { |
|
|
|
|
case 0: |
|
|
|
|
case 1: |
|
|
|
|
// 16进制转十进制类型
|
|
|
|
|
sValue = ExchangeStringUtil.hexToDec(data); |
|
|
|
|
if (deviceCodeParamEntity.getDigit() == 0) { |
|
|
|
|
sValue = String.valueOf(Long.parseLong(sValue)); |
|
|
|
|
} else { |
|
|
|
|
// 保留位数
|
|
|
|
|
sValue = (new BigDecimal(sValue)).divide(new BigDecimal(String.valueOf(deviceCodeParamEntity.getDigit() * 10)), 2, RoundingMode.HALF_UP).toString(); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case 2: |
|
|
|
|
// 十六进制字符串转IEEE754浮点型
|
|
|
|
|
sValue = String.valueOf(Math.abs(ExchangeStringUtil.hexToSingle(data))); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
log.info("解析数据==>表号:{},寄存器地址:{},值:{}", deviceCodeParamEntity.getDeviceAddr(), deviceCodeParamEntity.getRegisterAddr(), sValue); |
|
|
|
|
|
|
|
|
|
// 入库数据
|
|
|
|
|
// 冷量表
|
|
|
|
|
if ("2".equals(deviceCodeParamEntity.getDeviceType())) { |
|
|
|
|
analysisCloudOrder485(sValue, deviceCodeParamEntity); |
|
|
|
|
} else if ("0".equals(deviceCodeParamEntity.getDeviceType())) { |
|
|
|
|
analysisChillerOrder485(sValue, deviceCodeParamEntity); |
|
|
|
|
} |
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
log.error("modbus标准协议:策略解析报文异常", e); |
|
|
|
|
} |
|
|
|
|
return sValue; |
|
|
|
|
} |
|
|
|
|