Browse Source

2024-06-21

master
qiezi 2 years ago
parent
commit
5f332553a5
  1. 7
      pom.xml
  2. 4
      src/main/java/com/spring/modules/cdc/task/GatherDataTask.java
  3. 51
      src/main/java/com/spring/modules/cdc/utils/CollectUtils.java
  4. 1
      src/main/resources/application-dev.yml

7
pom.xml

@ -322,12 +322,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.github.s7connector</groupId>
<artifactId>s7connector</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.ghgande</groupId>
<artifactId>j2mod</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.github.dathlin</groupId>
<artifactId>HslCommunication</artifactId>

4
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<DeviceVo> readValueList = CollectUtils.getReadValueList(deviceList, ip, new BigDecimal(line3Value));
List<DeviceVo> readValueList = CollectUtils.getReadModbusValueList(deviceList, ip, line3Port,new BigDecimal(line3Value));
deviceVos.addAll(readValueList);
log.info("配胶间:{}",readValueList.stream().map(DeviceVo::getValue).collect(Collectors.toList()));
}else {

51
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<DeviceVo> getReadModbusValueList(List<DeviceVo> deviceList,String IP,int port,BigDecimal defaultValue){
ModbusTCPMaster master = initModbus(IP, port);
List<DeviceVo> 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;
}
/**
*

1
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
Loading…
Cancel
Save