use of com.ghgande.j2mod.modbus.net.TCPMasterConnection in project SmartApplianceEnabler by camueller.
the class ModbusTcp method getConnection.
public TCPMasterConnection getConnection() throws UnknownHostException {
InetAddress address = InetAddress.getByName(getResolvedHost());
TCPMasterConnection connection = new TCPMasterConnection(address);
connection.setPort(getResolvedPort());
return connection;
}
use of com.ghgande.j2mod.modbus.net.TCPMasterConnection in project openems by OpenEMS.
the class ModbusTcp method getTransaction.
@Override
public ModbusTransaction getTransaction() throws OpenemsModbusException {
TCPMasterConnection connection = getModbusConnection();
ModbusTCPTransaction trans = new ModbusTCPTransaction(connection);
return trans;
}
use of com.ghgande.j2mod.modbus.net.TCPMasterConnection in project openems by OpenEMS.
the class ModbusTcp method getModbusConnection.
private TCPMasterConnection getModbusConnection() throws OpenemsModbusException {
if (!connection.isPresent()) {
try {
TCPMasterConnection tcpCon = new TCPMasterConnection(ip.value());
tcpCon.setPort(port.valueOptional().orElse(502));
connection = Optional.of(tcpCon);
this.configurationFault.setValue(false);
} catch (InvalidValueException e) {
this.configurationFault.setValue(true);
throw new OpenemsModbusException("Modbus-TCP is not configured completely");
}
}
if (!connection.get().isConnected()) {
try {
TCPMasterConnection tcpCon = connection.get();
tcpCon.connect();
tcpCon.getModbusTransport().setTimeout(1000);
this.connectionFault.setValue(false);
} catch (Exception e) {
this.connectionFault.setValue(true);
throw new OpenemsModbusException("Unable to open Modbus-TCP connection: " + ip.valueOptional().get());
}
}
return connection.get();
}
Aggregations