From 5f332553a57e39dd0c18326758642d6e4d8a9362 Mon Sep 17 00:00:00 2001
From: qiezi <15576055375@163.com>
Date: Wed, 17 Jul 2024 13:13:19 +0800
Subject: [PATCH] 2024-06-21
---
pom.xml | 7 +++
.../modules/cdc/task/GatherDataTask.java | 4 +-
.../modules/cdc/utils/CollectUtils.java | 51 +++++++++++++++++++
src/main/resources/application-dev.yml | 1 +
4 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index 52f1cfc6..c0b2d856 100644
--- a/pom.xml
+++ b/pom.xml
@@ -322,12 +322,19 @@
org.springframework.boot
spring-boot-starter-mail
+
com.github.s7connector
s7connector
2.1
+
+ com.ghgande
+ j2mod
+ 3.2.1
+
+
com.github.dathlin
HslCommunication
diff --git a/src/main/java/com/spring/modules/cdc/task/GatherDataTask.java b/src/main/java/com/spring/modules/cdc/task/GatherDataTask.java
index 4a678661..5cf4d907 100644
--- a/src/main/java/com/spring/modules/cdc/task/GatherDataTask.java
+++ b/src/main/java/com/spring/modules/cdc/task/GatherDataTask.java
@@ -53,6 +53,8 @@ public class GatherDataTask {
@Value("${collect.data.line2Port}")
private int line2Port;
+ @Value("${collect.data.line3Port}")
+ private int line3Port;
@Autowired
private IDeviceService deviceService;
@Autowired
@@ -90,7 +92,7 @@ public class GatherDataTask {
deviceVos.addAll(readSLValueList);
log.info("小线:{}",readSLValueList.stream().map(DeviceVo::getValue).collect(Collectors.toList()));
}else if (ip.equals(line3IP)){
- List readValueList = CollectUtils.getReadValueList(deviceList, ip, new BigDecimal(line3Value));
+ List readValueList = CollectUtils.getReadModbusValueList(deviceList, ip, line3Port,new BigDecimal(line3Value));
deviceVos.addAll(readValueList);
log.info("配胶间:{}",readValueList.stream().map(DeviceVo::getValue).collect(Collectors.toList()));
}else {
diff --git a/src/main/java/com/spring/modules/cdc/utils/CollectUtils.java b/src/main/java/com/spring/modules/cdc/utils/CollectUtils.java
index 86089439..1b7b744f 100644
--- a/src/main/java/com/spring/modules/cdc/utils/CollectUtils.java
+++ b/src/main/java/com/spring/modules/cdc/utils/CollectUtils.java
@@ -7,6 +7,13 @@ import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Melsec.MelsecMcNet;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
+import com.ghgande.j2mod.modbus.ModbusException;
+import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
+import com.ghgande.j2mod.modbus.io.ModbusTCPTransaction;
+import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest;
+import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse;
+import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
+import com.ghgande.j2mod.modbus.procimg.Register;
import com.github.s7connector.api.DaveArea;
import com.github.s7connector.api.S7Connector;
import com.github.s7connector.api.factory.S7ConnectorFactory;
@@ -15,6 +22,8 @@ import com.spring.modules.cdc.vo.DeviceVo;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
@@ -155,6 +164,48 @@ public class CollectUtils {
return deviceVoList;
}
+ public static ModbusTCPMaster initModbus(String IP,int port){
+ ModbusTCPMaster master = new ModbusTCPMaster(IP, port);
+ try {
+ master.connect();
+ } catch (Exception e) {
+ log.error("连接失败", e);
+ throw new RuntimeException(e);
+ }
+ return master;
+ }
+
+ public static List getReadModbusValueList(List deviceList,String IP,int port,BigDecimal defaultValue){
+ ModbusTCPMaster master = initModbus(IP, port);
+ List deviceVoList = new ArrayList<>();
+ try {
+ master.connect();
+ } catch (Exception e) {
+ log.error("IP:"+IP+",端口:"+port+"连接失败", e);
+ deviceList.forEach(device -> {
+ deviceVoList.add(new DeviceVo(device, defaultValue,device.getCollectId()));
+ });
+ return deviceVoList;
+ }
+ for (DeviceVo deviceVo : deviceList) {
+ try {
+ Register[] read = master.readMultipleRegisters(Integer.parseInt(deviceVo.getDeviceIp()), 1);
+ int value = read[0].getValue();
+ BigDecimal val = new BigDecimal(value);
+ if (value > 0){
+ val = BigDecimal.valueOf(value).divide(new BigDecimal(10));
+ }
+ deviceVoList.add(new DeviceVo(deviceVo, val,deviceVo.getCollectId()));
+ log.info("设备:{} 读取值:{}", deviceVo.getDeviceDesc(), val);
+ } catch (ModbusException e) {
+ deviceVoList.add(new DeviceVo(deviceVo, defaultValue,deviceVo.getCollectId()));
+ log.info("设备:{} 使用默认值:{}", deviceVo.getDeviceDesc(), defaultValue);
+ }
+ }
+ master.disconnect();
+ return deviceVoList;
+ }
+
/**
*
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index 69d356ae..b725acd2 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -88,6 +88,7 @@ collect:
line1: 192.168.3.2
line2: 192.168.3.39
line2Port: 1020
+ line3Port: 7000
line3: 1
line1Value: 0
line2Value: 0