use of com.ghgande.j2mod.modbus.slave.ModbusSlave in project openems by OpenEMS.
the class ModbusTcpApiController method restartSlave.
protected void restartSlave(Optional<Integer> portOpt) {
// remove and close old slave if existing
if (this.slaveOpt.isPresent()) {
ModbusSlave oldSlave = this.slaveOpt.get();
oldSlave.close();
this.slaveOpt = Optional.empty();
}
// create new slave, initialize and start
try {
if (!portOpt.isPresent()) {
throw new OpenemsException("Port was not set");
}
int port = portOpt.get();
ModbusSlave newSlave = ModbusSlaveFactory.createTCPSlave(port, MAX_CONCURRENT_CONNECTIONS);
newSlave.addProcessImage(UNIT_ID, this.processImage);
newSlave.open();
// TODO slave should be closed on dispose of Controller
log.info("Modbus/TCP Api started on port [" + port + "] with UnitId [" + UNIT_ID + "].");
this.slaveOpt = Optional.of(newSlave);
} catch (OpenemsException | ModbusException e) {
log.error("Unable to start Modbus/TCP slave: " + e.getMessage());
}
}
Aggregations