use of io.openems.api.exception.OpenemsModbusException 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