|
|
@ -0,0 +1,41 @@ |
|
|
|
|
|
package com.spring.modules.cdc.utils; |
|
|
|
|
|
|
|
|
|
|
|
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster; |
|
|
|
|
|
import com.ghgande.j2mod.modbus.procimg.Register; |
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
|
|
|
public class CollectUtil { |
|
|
|
|
|
public static Integer getValue(String IP, int port,int unitId,int ref){ |
|
|
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(IP, port); |
|
|
|
|
|
try { |
|
|
|
|
|
master.connect(); |
|
|
|
|
|
Register[] read = master.readMultipleRegisters(unitId,ref,1); |
|
|
|
|
|
return read[0].getValue(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("连接失败", e); |
|
|
|
|
|
}finally { |
|
|
|
|
|
master.disconnect(); |
|
|
|
|
|
} |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static List<Integer> getValues(String IP, int port,int unitId,int ref,int count){ |
|
|
|
|
|
ModbusTCPMaster master = new ModbusTCPMaster(IP, port); |
|
|
|
|
|
try { |
|
|
|
|
|
master.connect(); |
|
|
|
|
|
Register[] read = master.readMultipleRegisters(unitId,ref,count); |
|
|
|
|
|
return Arrays.stream(read).map(Register::getValue).collect(Collectors.toList()); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("连接失败", e); |
|
|
|
|
|
}finally { |
|
|
|
|
|
master.disconnect(); |
|
|
|
|
|
} |
|
|
|
|
|
return new ArrayList<>(); |
|
|
|
|
|
} |
|
|
|
|
|
} |