Search in sources :

Example 11 with OpenemsModbusException

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();
}
Also used : InvalidValueException(io.openems.api.exception.InvalidValueException) TCPMasterConnection(com.ghgande.j2mod.modbus.net.TCPMasterConnection) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) OpenemsModbusException(io.openems.api.exception.OpenemsModbusException) ConfigException(io.openems.api.exception.ConfigException) InvalidValueException(io.openems.api.exception.InvalidValueException)

Aggregations

OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)11 ModbusException (com.ghgande.j2mod.modbus.ModbusException)9 ModbusTransaction (com.ghgande.j2mod.modbus.io.ModbusTransaction)7 ModbusResponse (com.ghgande.j2mod.modbus.msg.ModbusResponse)5 ExceptionResponse (com.ghgande.j2mod.modbus.msg.ExceptionResponse)2 ConfigException (io.openems.api.exception.ConfigException)2 ReadCoilsRequest (com.ghgande.j2mod.modbus.msg.ReadCoilsRequest)1 ReadCoilsResponse (com.ghgande.j2mod.modbus.msg.ReadCoilsResponse)1 ReadInputRegistersRequest (com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest)1 ReadInputRegistersResponse (com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse)1 ReadMultipleRegistersRequest (com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest)1 ReadMultipleRegistersResponse (com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse)1 WriteCoilRequest (com.ghgande.j2mod.modbus.msg.WriteCoilRequest)1 WriteMultipleCoilsRequest (com.ghgande.j2mod.modbus.msg.WriteMultipleCoilsRequest)1 WriteMultipleRegistersRequest (com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersRequest)1 WriteSingleRegisterRequest (com.ghgande.j2mod.modbus.msg.WriteSingleRegisterRequest)1 SerialConnection (com.ghgande.j2mod.modbus.net.SerialConnection)1 TCPMasterConnection (com.ghgande.j2mod.modbus.net.TCPMasterConnection)1 BitVector (com.ghgande.j2mod.modbus.util.BitVector)1 SerialParameters (com.ghgande.j2mod.modbus.util.SerialParameters)1