Search in sources :

Example 1 with ModbusSlave

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());
    }
}
Also used : ModbusSlave(com.ghgande.j2mod.modbus.slave.ModbusSlave) OpenemsException(io.openems.common.exceptions.OpenemsException) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Aggregations

ModbusException (com.ghgande.j2mod.modbus.ModbusException)1 ModbusSlave (com.ghgande.j2mod.modbus.slave.ModbusSlave)1 OpenemsException (io.openems.common.exceptions.OpenemsException)1