From 245b3e22d6cfa359cf27a642341519047bf4e1a1 Mon Sep 17 00:00:00 2001 From: zq Date: Fri, 19 Dec 2025 11:50:11 +0800 Subject: [PATCH] =?UTF-8?q?PCS=E5=BC=80=E5=85=B3=E6=9C=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E9=80=9A=E8=BF=87modbus=E8=BF=9E=E6=8E=A5=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=8F=91=E9=80=81=E6=8E=A7=E5=88=B6=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/modbus/ModbusProcessor.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/ems-common/src/main/java/com/xzzn/common/core/modbus/ModbusProcessor.java b/ems-common/src/main/java/com/xzzn/common/core/modbus/ModbusProcessor.java index 3153a44..c504a23 100644 --- a/ems-common/src/main/java/com/xzzn/common/core/modbus/ModbusProcessor.java +++ b/ems-common/src/main/java/com/xzzn/common/core/modbus/ModbusProcessor.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -70,15 +71,7 @@ public class ModbusProcessor { tags.forEach(tag -> { Map type = ModBusType.REGISTER_TYPE; int firstDigit = Integer.parseInt(tag.getAddress().substring(0, 1)); - int address = 0; - int addressLength = tag.getAddress().length(); - int exp = (int) Math.pow(10, addressLength-1); - if (firstDigit != 0){ - int digit = Integer.parseInt(tag.getAddress()); - address = digit % (exp); - }else { - address = Integer.parseInt(tag.getAddress()); - } + int address = convertAddress(tag.getAddress()); RegisterType registerType = type.get(firstDigit); logger.info("Register type: {}, address: {}, firstDigit: {}", registerType, tag.getAddress(), firstDigit); switch (registerType) { @@ -381,4 +374,19 @@ public class ModbusProcessor { ); } + /** 转换寄存器地址 */ + public static int convertAddress(String address) { + if (StringUtils.isBlank(address)) { + return 0; + } + int firstDigit = Integer.parseInt(address.substring(0, 1)); + int addressLength = address.length(); + int exp = (int) Math.pow(10, addressLength-1); + if (firstDigit != 0){ + int digit = Integer.parseInt(address); + return digit % (exp); + } else { + return Integer.parseInt(address); + } + } }