|
|
|
@ -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; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
|