PCS开关机功能通过modbus连接设备发送控制命令;

This commit is contained in:
zq
2025-12-19 11:50:11 +08:00
parent ff487f1b05
commit 245b3e22d6

View File

@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -70,15 +71,7 @@ public class ModbusProcessor {
tags.forEach(tag -> { tags.forEach(tag -> {
Map<Integer, RegisterType> type = ModBusType.REGISTER_TYPE; Map<Integer, RegisterType> type = ModBusType.REGISTER_TYPE;
int firstDigit = Integer.parseInt(tag.getAddress().substring(0, 1)); int firstDigit = Integer.parseInt(tag.getAddress().substring(0, 1));
int address = 0; int address = convertAddress(tag.getAddress());
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());
}
RegisterType registerType = type.get(firstDigit); RegisterType registerType = type.get(firstDigit);
logger.info("Register type: {}, address: {}, firstDigit: {}", registerType, tag.getAddress(), firstDigit); logger.info("Register type: {}, address: {}, firstDigit: {}", registerType, tag.getAddress(), firstDigit);
switch (registerType) { 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);
}
}
} }