Browse Source

20241223

java8
qiezi 1 year ago
parent
commit
b087c9204a
  1. 17
      src/main/java/com/xujie/sys/modules/reader/service/impl/ModbusCommunicateServiceImpl.java

17
src/main/java/com/xujie/sys/modules/reader/service/impl/ModbusCommunicateServiceImpl.java

@ -259,12 +259,6 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService {
//获取检验单号
@Transactional
public String getInspectionOrderNumber(String modbusIp, int modbusPort, int registerAddress, int registerNum, int unitId) {
log.info("modbus请求开始解析");
// String modbusIp = "172.26.58.222";
// int modbusPort = 502;
// int registerAddress = 299;
// int registerNum = 11;
// int unitId = 1;
log.info("获取modbus设备ip-端口-地址值-数量-unitId:{}-{}-{}-{}-{}", modbusIp, modbusPort, registerAddress, registerNum, unitId);
long startTime = System.nanoTime();
String finalString = "";
@ -286,7 +280,7 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService {
// 执行Modbus TCP事务
transaction.execute();
log.info("开始执行modbus tcp事物");
log.info("开始执行{}modbus tcp事物",modbusIp);
// 获取响应
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse();
@ -295,8 +289,7 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService {
double elapsedTimeInSeconds = (double) elapsedTimeInNanos / 1_000_000_000.0;
log.info("时间elapsedTimeInSeconds: {}", elapsedTimeInSeconds);
log.info("打印响应的数据: {}", response.getMessage());
if (response != null) {
log.info("打印响应的数据IP:{}: {}",modbusIp, response.getMessage());
InputRegister[] registers = response.getRegisters();
ArrayList<byte[]> byteArrayList = new ArrayList<byte[]>();
for (InputRegister register : registers) {
@ -325,14 +318,10 @@ public class ModbusCommunicateServiceImpl implements ModbusCommunicateService {
//如果不是零的话就对寄存器清零
deleteForModbus(registerAddress, registerNum, unitId, connection);
}
} else {
log.info("处理数据出现异常");
}
// 关闭连接
connection.close();
log.info("关闭连接");
} catch (Exception e) {
log.error("连接Modbus设备时出现异常: {}", e.getMessage());
log.error("连接IP{}Modbus设备时出现异常: {}", modbusIp,e.getMessage());
e.printStackTrace();
}
return finalString;

Loading…
Cancel
Save