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); + } + } }