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