|
|
@ -10,6 +10,10 @@ import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
public class UfModbusRTUOverTCP extends UfConnectionBase { |
|
|
|
// host |
|
|
|
public String host; |
|
|
|
// port |
|
|
|
public Integer port; |
|
|
|
// connection |
|
|
|
private Socket socket; |
|
|
|
// writer |
|
|
@ -19,41 +23,10 @@ public class UfModbusRTUOverTCP extends UfConnectionBase { |
|
|
|
// busy |
|
|
|
private Boolean isBusy = false; |
|
|
|
|
|
|
|
// connections |
|
|
|
private static final Map<String, UfModbusRTUOverTCP> connections = new HashMap<>(); |
|
|
|
|
|
|
|
// get connection |
|
|
|
public static UfModbusRTUOverTCP getConnection(String host, Integer port) { |
|
|
|
String key = host + ":" + port; |
|
|
|
if ( connections.containsKey(key) ) { |
|
|
|
return connections.get(key); |
|
|
|
} |
|
|
|
|
|
|
|
UfModbusRTUOverTCP con = new UfModbusRTUOverTCP(); |
|
|
|
con.connect(host, port); |
|
|
|
connections.put(key, con); |
|
|
|
return con; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
synchronized public String execute(UfMdbActuatorCmd command) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void connect() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// constructor |
|
|
|
public UfModbusRTUOverTCP() { |
|
|
|
this.isBusy = false; |
|
|
|
} |
|
|
|
|
|
|
|
// connect |
|
|
|
public void connect( String host, Integer port ) { |
|
|
|
try { |
|
|
|
this.socket = new Socket(host, port); |
|
|
|
this.socket = new Socket(this.host, this.port); |
|
|
|
this.writer = new DataOutputStream(this.socket.getOutputStream()); |
|
|
|
this.reader = this.socket.getInputStream(); |
|
|
|
} catch (Exception e) { |
|
|
@ -61,6 +34,25 @@ public class UfModbusRTUOverTCP extends UfConnectionBase { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
synchronized public String execute(UfMdbActuatorCmd command) { |
|
|
|
if ( "03".equals(command.cmdId) ) { |
|
|
|
Integer slaveId = Integer.parseInt(command.cmdFlags); |
|
|
|
Integer address = Integer.parseInt(command.parameters); |
|
|
|
Integer value = this.readHoldingRegister(slaveId, address); |
|
|
|
return value.toString(); |
|
|
|
} else if ( "06".equals(command.cmdId) ) { |
|
|
|
Integer slaveId = Integer.parseInt(command.cmdFlags); |
|
|
|
String[] params = command.parameters.split(","); |
|
|
|
Integer address = Integer.parseInt(params[0]); |
|
|
|
Integer value = Integer.parseInt(params[1]); |
|
|
|
this.writeHoldingRegister(slaveId, address, value); |
|
|
|
return ""; |
|
|
|
} else { |
|
|
|
throw new RuntimeException("Modbus RTU over TCP: unknown command"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// write holding register |
|
|
|
public void writeHoldingRegister( Integer slaveId, Integer address, Integer value ) { |
|
|
|
byte[] cmd = new byte[8]; |
|
|
|