changes
This commit is contained in:
1 parent
7b52ffe4fe
commit
f4131286e6
2 files changed
+32
-5
No files matched your search
@@ -1,7 +1,20 @@
|
||||
package home.automation;
|
||||
|
||||
public class RegistryMapping{
|
||||
public int registry;
|
||||
public String desc;
|
||||
public class RegistryMapping {
|
||||
private final int registry;
|
||||
private final String desc;
|
||||
|
||||
public RegistryMapping(int registry, String desc) {
|
||||
this.registry = registry;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public int getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,36 @@
|
||||
package home.automation;
|
||||
|
||||
import com.ghgande.j2mod.modbus.ModbusException;
|
||||
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
|
||||
import com.ghgande.j2mod.modbus.procimg.Register;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class SDMService {
|
||||
|
||||
private final InetAddress address;
|
||||
private final ModbusTCPMaster modbusConnection;
|
||||
private final int port;
|
||||
|
||||
public SDMService(InetAddress address, int port) {
|
||||
public SDMService(InetAddress address, int port) throws Exception {
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
this.modbusConnection = initConnection();
|
||||
}
|
||||
|
||||
private void initConnection() throws Exception {
|
||||
public Register[] read(RegistryMapping mapping) {
|
||||
try {
|
||||
return modbusConnection.readMultipleRegisters(mapping.getRegistry(), 1);
|
||||
} catch (ModbusException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new Register[0];
|
||||
}
|
||||
|
||||
private ModbusTCPMaster initConnection() throws Exception {
|
||||
ModbusTCPMaster modbusRTUTCPTransport = new ModbusTCPMaster(address.getHostAddress(), port);
|
||||
modbusRTUTCPTransport.connect();
|
||||
return modbusRTUTCPTransport;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user