|
|
|
@ -13,6 +13,19 @@ import java.util.stream.Collectors; |
|
|
|
public class ModbusUtils { |
|
|
|
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) { |
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); |
|
|
|
try { |
|
|
|
master.setTimeout(MODBUS_TCP_TIMEOUT_MS); |
|
|
|
master.connect(); |
|
|
|
Register[] registers = master.readMultipleRegisters(unitId, ref, 1); |
|
|
|
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) { |
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); |
|
|
|
try { |
|
|
|
master.setTimeout(MODBUS_TCP_TIMEOUT_MS); |
|
|
|
master.connect(); |
|
|
|
Register[] registers = master.readMultipleRegisters(ref, count); |
|
|
|
if (registers != null) { |
|
|
|
@ -90,6 +105,7 @@ public class ModbusUtils { |
|
|
|
public static BitVector readCoils(String ip, int port, int ref, int count) { |
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); |
|
|
|
try { |
|
|
|
master.setTimeout(MODBUS_TCP_TIMEOUT_MS); |
|
|
|
master.connect(); |
|
|
|
return master.readCoils(ref, count); |
|
|
|
} catch (Exception e) { |
|
|
|
@ -161,8 +177,7 @@ public class ModbusUtils { |
|
|
|
Map<Integer, Integer> resultMap = new HashMap<>(); |
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); |
|
|
|
try { |
|
|
|
// 设置超时时间:连接超时和响应超时都设置为10秒 |
|
|
|
master.setTimeout(10000); |
|
|
|
master.setTimeout(MODBUS_TCP_TIMEOUT_MS); |
|
|
|
|
|
|
|
// 开启连接 |
|
|
|
master.connect(); |
|
|
|
@ -231,8 +246,7 @@ public class ModbusUtils { |
|
|
|
Map<Integer, Float> resultMap = new HashMap<>(); |
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); |
|
|
|
try { |
|
|
|
// 设置超时时间:连接超时和响应超时都设置为10秒 |
|
|
|
master.setTimeout(10000); |
|
|
|
master.setTimeout(MODBUS_TCP_TIMEOUT_MS); |
|
|
|
|
|
|
|
// 开启连接 |
|
|
|
master.connect(); |
|
|
|
@ -274,7 +288,7 @@ public class ModbusUtils { |
|
|
|
public static boolean writeSingleHoldingRegister(String ip, int port, int unitId, int ref, int value) { |
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); |
|
|
|
try { |
|
|
|
master.setTimeout(10000); |
|
|
|
master.setTimeout(MODBUS_TCP_TIMEOUT_MS); |
|
|
|
master.connect(); |
|
|
|
if (!master.isConnected()) { |
|
|
|
log.error("连接失败 - IP: {}, Port: {}", ip, port); |
|
|
|
@ -303,7 +317,7 @@ public class ModbusUtils { |
|
|
|
} |
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(ip, port); |
|
|
|
try { |
|
|
|
master.setTimeout(10000); |
|
|
|
master.setTimeout(MODBUS_TCP_TIMEOUT_MS); |
|
|
|
master.connect(); |
|
|
|
if (!master.isConnected()) { |
|
|
|
log.error("连接失败 - IP: {}, Port: {}", ip, port); |
|
|
|
|