From 2977912e0d11dd1122662304c527261f300068f6 Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 16 Oct 2024 17:37:17 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=A4=A7=E5=B1=8F=E6=8E=A5=E5=8F=A3b?= =?UTF-8?q?ug=E4=BF=AE=E6=94=B9=EF=BC=9B=202=E3=80=81=E7=94=B5=E8=A1=A8?= =?UTF-8?q?=EF=BC=8Cmodbus=E5=8D=8F=E8=AE=AE=E8=A7=A3=E6=9E=90bug=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mh/user/mapper/MeterManageMapper.java | 2 +- .../com/mh/user/netty/EchoServerHandler.java | 8 +- .../service/impl/MeterManageServiceImpl.java | 2 +- .../user/service/impl/ScreenServiceImpl.java | 7 +- .../mh/user/strategy/EleProtocolStrategy.java | 8 +- .../user/strategy/ModbusProtocolStrategy.java | 98 +++++++++++-------- 6 files changed, 75 insertions(+), 50 deletions(-) diff --git a/user-service/src/main/java/com/mh/user/mapper/MeterManageMapper.java b/user-service/src/main/java/com/mh/user/mapper/MeterManageMapper.java index 1ae5bd1..332f890 100644 --- a/user-service/src/main/java/com/mh/user/mapper/MeterManageMapper.java +++ b/user-service/src/main/java/com/mh/user/mapper/MeterManageMapper.java @@ -19,7 +19,7 @@ public interface MeterManageMapper extends BaseMapper { @Select("select * from meter_manage where mt_code=#{deviceAddr} " + " and mt_type=#{deviceType} " + " and project_id=#{projectId} " + - " and register_addr = #{registerAddr} ") + " and register_addr = #{registerAddr}") MeterManageEntity selectDevice(@Param("deviceAddr") String deviceAddr, @Param("deviceType") String deviceType, @Param("projectId") String projectId, diff --git a/user-service/src/main/java/com/mh/user/netty/EchoServerHandler.java b/user-service/src/main/java/com/mh/user/netty/EchoServerHandler.java index 66f1969..ab2ae47 100644 --- a/user-service/src/main/java/com/mh/user/netty/EchoServerHandler.java +++ b/user-service/src/main/java/com/mh/user/netty/EchoServerHandler.java @@ -111,12 +111,11 @@ public class EchoServerHandler extends ChannelInboundHandlerAdapter { receiveStr = receiveStr.toUpperCase();//返回值全部变成大写 log.info("channelReadComplete接收到的数据{}, 长度: ===> {}", receiveStr, receiveStr.length()); //心跳包处理 -// if ((receiveStr.length() == 8) && receiveStr.startsWith("24")) { - if ((receiveStr.length() == 8) && receiveStr.startsWith("C0A801FE")) { + if ((receiveStr.length() == 8) && receiveStr.startsWith("24")) { +// if ((receiveStr.length() == 8) && receiveStr.startsWith("C0A801FE")) { log.info("接收到心跳包 ===> {}", receiveStr); idleCount = 1; port = receiveStr.substring(4, 8);//心跳包包含网关端口(自己定义返回心跳包) - port = "6001"; // 清空receiveStr receiveStr = ""; // 更新对应的网关在线情况 @@ -390,7 +389,8 @@ public class EchoServerHandler extends ChannelInboundHandlerAdapter { // 异常捕捉 @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - log.info("异常捕捉,执行ctx.close()......{}", cause.getCause()); + cause.getCause().printStackTrace(); + log.info("异常捕捉,执行ctx.close" + cause.getCause()); ctx.close(); // 关闭该Channel } diff --git a/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java index 8e511e4..083118a 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java @@ -43,7 +43,7 @@ public class MeterManageServiceImpl implements MeterManageService { if (!StringUtils.isBlank(projectId)) { queryWrapper.eq("project_id", projectId); } - queryWrapper.eq("is_use", 0); + queryWrapper.eq("is_use", 1); return meterManageMapper.selectList(queryWrapper.orderByDesc("create_time")); } diff --git a/user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java index 38a7316..6cbc6af 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java @@ -65,8 +65,8 @@ public class ScreenServiceImpl implements ScreenService { String projectId = screenRequestParamDTO.getProjectId(); String startTime = screenRequestParamDTO.getStartTime(); String endTime = screenRequestParamDTO.getEndTime(); - String lastStartTime = DateUtil.yoyDate(startTime + " 00:00:00").substring(0, 10); - String lastEndTime = DateUtil.yoyDate(endTime+ " 23:59:59").substring(0, 10); + String lastStartTime = DateUtil.yoyDate(startTime + "-01 00:00:00").substring(0, 10); + String lastEndTime = DateUtil.yoyDate(endTime+ "-31 23:59:59").substring(0, 10); List> monthData = screenMapper.savingYoyEnergyData(projectId, startTime, endTime, lastStartTime, lastEndTime); resultJson.add(monthData); break; @@ -104,6 +104,9 @@ public class ScreenServiceImpl implements ScreenService { JSONObject data = new JSONObject(); float[] ll = new float[2]; data.put("name", map.get("project_name")); + if (map.get("longitude") == null || map.get("latitude") == null) { + continue; + } ll[0] = Float.parseFloat(map.get("longitude").toString()); ll[1] = Float.parseFloat(map.get("latitude").toString()); data.put("value", ll); diff --git a/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java b/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java index c1b0693..356428d 100644 --- a/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java +++ b/user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java @@ -99,7 +99,7 @@ public class EleProtocolStrategy implements ProtocolStrategy { @Override public String analysisReceiveData(DeviceCodeParamEntity deviceCodeParamEntity, String receiveData) { - log.info("电表97/07规约协议:工厂解析报文"); + log.info("电表97/07规约协议:工厂解析报文,接收到的报文<=={}",receiveData); String data = ""; if (receiveData.length() == 34 || receiveData.length() == 36 || receiveData.length() == 40 || receiveData.length() == 44 || receiveData.length() == 50) { String str1 = receiveData.substring(0, 8); @@ -118,9 +118,10 @@ public class EleProtocolStrategy implements ProtocolStrategy { log.info("电表报文检验失败: {}", receiveData); } } - if (!StringUtils.isBlank(data)) { - data = String.valueOf(Double.valueOf(data)); //00010.76,去除读数前面带0的情况 + if (StringUtils.isBlank(data) || Constant.FAIL.equals(data)) { + return Constant.FAIL; } + data = String.valueOf(Double.valueOf(data)); //00010.76,去除读数前面带0的情况 // 解析入库 analysisMeterOrder485(data, deviceCodeParamEntity); return data; @@ -130,6 +131,7 @@ public class EleProtocolStrategy implements ProtocolStrategy { StringBuilder stringBuilder = new StringBuilder(); // 累计总读数据:FE FE FE FE 68 69 04 00 70 01 08 68 81 06 43 C3 A5 33 33 33 81 16 // 瞬时有功功率:FE FE FE FE 68 69 04 00 70 01 08 68 81 05 63 E9 33 33 33 21 16 + // FE FE FE FE 68 98 01 50 04 00 00 68 C1 01 35 B4 16 // 判断属于哪个数据标识 String resultData = Constant.FAIL; if (receiveData.length() > 24) { 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 62b5445..59b6511 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 @@ -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; }