Browse Source

胶水修改

master
shenzhouyu 3 weeks ago
parent
commit
8b0b71707c
  1. 26
      src/main/java/com/xujie/sys/common/utils/ModbusUtils.java
  2. 22
      src/main/java/com/xujie/sys/modules/pms/service/Impl/QcServiceImpl.java

26
src/main/java/com/xujie/sys/common/utils/ModbusUtils.java

@ -13,6 +13,19 @@ import java.util.stream.Collectors;
public class ModbusUtils { public class ModbusUtils {
private static final Logger log = LoggerFactory.getLogger(ModbusUtils.class); private static final Logger log = LoggerFactory.getLogger(ModbusUtils.class);
/** Modbus TCP 套接字超时(毫秒),用于建连与读写;弱网或从站响应慢时可适当加大 */
private static final int MODBUS_TCP_TIMEOUT_MS = 20000;
/**
*
* @param highRegister 16 位所在寄存器的 {@code getValue()}
* @param lowRegister 16 位所在寄存器的 {@code getValue()}
*/
public static float holdingRegistersToFloatBigEndian(int highRegister, int lowRegister) {
int bits = (highRegister & 0xFFFF) << 16 | (lowRegister & 0xFFFF);
return Float.intBitsToFloat(bits);
}
/** /**
* 读取单个保持寄存器值 * 读取单个保持寄存器值
* *
@ -25,6 +38,7 @@ public class ModbusUtils {
public static Integer readSingleRegister(String ip, int port, int unitId, int ref) { public static Integer readSingleRegister(String ip, int port, int unitId, int ref) {
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); ModbusTCPMaster master = new ModbusTCPMaster(ip, port);
try { try {
master.setTimeout(MODBUS_TCP_TIMEOUT_MS);
master.connect(); master.connect();
Register[] registers = master.readMultipleRegisters(unitId, ref, 1); Register[] registers = master.readMultipleRegisters(unitId, ref, 1);
if (registers != null && registers.length > 0) { if (registers != null && registers.length > 0) {
@ -58,6 +72,7 @@ public class ModbusUtils {
public static List<Integer> readMultipleRegisters(String ip, int port, int ref, int count) { public static List<Integer> readMultipleRegisters(String ip, int port, int ref, int count) {
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); ModbusTCPMaster master = new ModbusTCPMaster(ip, port);
try { try {
master.setTimeout(MODBUS_TCP_TIMEOUT_MS);
master.connect(); master.connect();
Register[] registers = master.readMultipleRegisters(ref, count); Register[] registers = master.readMultipleRegisters(ref, count);
if (registers != null) { if (registers != null) {
@ -90,6 +105,7 @@ public class ModbusUtils {
public static BitVector readCoils(String ip, int port, int ref, int count) { public static BitVector readCoils(String ip, int port, int ref, int count) {
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); ModbusTCPMaster master = new ModbusTCPMaster(ip, port);
try { try {
master.setTimeout(MODBUS_TCP_TIMEOUT_MS);
master.connect(); master.connect();
return master.readCoils(ref, count); return master.readCoils(ref, count);
} catch (Exception e) { } catch (Exception e) {
@ -161,8 +177,7 @@ public class ModbusUtils {
Map<Integer, Integer> resultMap = new HashMap<>(); Map<Integer, Integer> resultMap = new HashMap<>();
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); ModbusTCPMaster master = new ModbusTCPMaster(ip, port);
try { try {
// 设置超时时间连接超时和响应超时都设置为10秒
master.setTimeout(10000);
master.setTimeout(MODBUS_TCP_TIMEOUT_MS);
// 开启连接 // 开启连接
master.connect(); master.connect();
@ -231,8 +246,7 @@ public class ModbusUtils {
Map<Integer, Float> resultMap = new HashMap<>(); Map<Integer, Float> resultMap = new HashMap<>();
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); ModbusTCPMaster master = new ModbusTCPMaster(ip, port);
try { try {
// 设置超时时间连接超时和响应超时都设置为10秒
master.setTimeout(10000);
master.setTimeout(MODBUS_TCP_TIMEOUT_MS);
// 开启连接 // 开启连接
master.connect(); master.connect();
@ -274,7 +288,7 @@ public class ModbusUtils {
public static boolean writeSingleHoldingRegister(String ip, int port, int unitId, int ref, int value) { public static boolean writeSingleHoldingRegister(String ip, int port, int unitId, int ref, int value) {
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); ModbusTCPMaster master = new ModbusTCPMaster(ip, port);
try { try {
master.setTimeout(10000);
master.setTimeout(MODBUS_TCP_TIMEOUT_MS);
master.connect(); master.connect();
if (!master.isConnected()) { if (!master.isConnected()) {
log.error("连接失败 - IP: {}, Port: {}", ip, port); log.error("连接失败 - IP: {}, Port: {}", ip, port);
@ -303,7 +317,7 @@ public class ModbusUtils {
} }
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); ModbusTCPMaster master = new ModbusTCPMaster(ip, port);
try { try {
master.setTimeout(10000);
master.setTimeout(MODBUS_TCP_TIMEOUT_MS);
master.connect(); master.connect();
if (!master.isConnected()) { if (!master.isConnected()) {
log.error("连接失败 - IP: {}, Port: {}", ip, port); log.error("连接失败 - IP: {}, Port: {}", ip, port);

22
src/main/java/com/xujie/sys/modules/pms/service/Impl/QcServiceImpl.java

@ -4834,7 +4834,7 @@ public class QcServiceImpl implements QcService {
} }
ArrayList<SubDetailValues> abnormalRows = getAbnormalCollectorData(ip, port, unitId, data); ArrayList<SubDetailValues> abnormalRows = getAbnormalCollectorData(ip, port, unitId, data);
//resetCollectorAbnormalRegisters(ip, port, unitId);
resetCollectorAbnormalRegisters(ip, port, unitId);
return abnormalRows; return abnormalRows;
}else{ }else{
logger.info(port+","+ip+","+unitId+","+baseAddr+","+groups+","); logger.info(port+","+ip+","+unitId+","+baseAddr+","+groups+",");
@ -4945,7 +4945,8 @@ public class QcServiceImpl implements QcService {
private ArrayList<SubDetailValues> getAbnormalCollectorData(String ip, int port, int unitId, QcFAIRecordData data) throws Exception { private ArrayList<SubDetailValues> getAbnormalCollectorData(String ip, int port, int unitId, QcFAIRecordData data) throws Exception {
int readStartAddr = 400; int readStartAddr = 400;
int readCount = 195; // 覆盖 400~594单次 FC03 字数不宜超过从站上限需分批读
// 10 组第 5 float 占用 594595共需读到 595 400 196 个字
int readCount = 196;
Map<Integer, Integer> raw = ModbusUtils.readHoldingRegistersBatched(ip, port, unitId, readStartAddr, readCount); Map<Integer, Integer> raw = ModbusUtils.readHoldingRegistersBatched(ip, port, unitId, readStartAddr, readCount);
ArrayList<SubDetailValues> subDetailValues = new ArrayList<>(); ArrayList<SubDetailValues> subDetailValues = new ArrayList<>();
if (raw == null || raw.isEmpty()) { if (raw == null || raw.isEmpty()) {
@ -4971,8 +4972,7 @@ public class QcServiceImpl implements QcService {
Integer high = raw.get(highRef); Integer high = raw.get(highRef);
Integer low = raw.get(lowRef); Integer low = raw.get(lowRef);
if (high != null && low != null) { if (high != null && low != null) {
int bits = (high << 16) | (low & 0xFFFF);
abnormalValues.add(Float.intBitsToFloat(bits));
abnormalValues.add(ModbusUtils.holdingRegistersToFloatBigEndian(high, low));
} else { } else {
abnormalValues.add(0F); abnormalValues.add(0F);
} }
@ -5020,7 +5020,7 @@ public class QcServiceImpl implements QcService {
526, 528, 530, 532, 534, 526, 528, 530, 532, 534,
546, 548, 550, 552, 554, 546, 548, 550, 552, 554,
566, 568, 570, 572, 574, 566, 568, 570, 572, 574,
586, 588, 590, 592, 594
586, 588, 590, 592, 594, 595
)); ));
logger.info("开始复位"); logger.info("开始复位");
ModbusUtils.resetRegisters(ip, port, unitId, resetAddrs); ModbusUtils.resetRegisters(ip, port, unitId, resetAddrs);
@ -5100,9 +5100,7 @@ public class QcServiceImpl implements QcService {
Integer high = raw.get(highRef); Integer high = raw.get(highRef);
Integer low = raw.get(lowRef); Integer low = raw.get(lowRef);
if (low != null && high != null) { if (low != null && high != null) {
int bits = (high << 16) | (low & 0xFFFF);
float f = Float.intBitsToFloat(bits);
resultMap.put(String.valueOf(addr), f);
resultMap.put(String.valueOf(addr), ModbusUtils.holdingRegistersToFloatBigEndian(high, low));
} }
} }
@ -5191,9 +5189,7 @@ public class QcServiceImpl implements QcService {
Integer low = raw.get(lowRef); Integer low = raw.get(lowRef);
Integer high = raw.get(highRef); Integer high = raw.get(highRef);
if (low != null && high != null) { if (low != null && high != null) {
int bits = (high << 16) | (low & 0xFFFF);
float f = Float.intBitsToFloat(bits);
resultMap.put(String.valueOf(addr), f);
resultMap.put(String.valueOf(addr), ModbusUtils.holdingRegistersToFloatBigEndian(high, low));
} }
} }
@ -5203,9 +5199,7 @@ public class QcServiceImpl implements QcService {
Integer lowAvg = raw.get(lowRefAvg); Integer lowAvg = raw.get(lowRefAvg);
Integer highAvg = raw.get(highRefAvg); Integer highAvg = raw.get(highRefAvg);
if (lowAvg != null && highAvg != null) { if (lowAvg != null && highAvg != null) {
int bits = (highAvg << 16) | (lowAvg & 0xFFFF);
float f = Float.intBitsToFloat(bits);
resultMap.put(String.valueOf(avgAddr), f);
resultMap.put(String.valueOf(avgAddr), ModbusUtils.holdingRegistersToFloatBigEndian(highAvg, lowAvg));
} }
return resultMap; return resultMap;

Loading…
Cancel
Save