Search in sources :

Example 1 with TCPMasterConnection

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;
}
Also used : TCPMasterConnection(com.ghgande.j2mod.modbus.net.TCPMasterConnection) InetAddress(java.net.InetAddress)

Example 2 with TCPMasterConnection

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;
}
Also used : ModbusTCPTransaction(com.ghgande.j2mod.modbus.io.ModbusTCPTransaction) TCPMasterConnection(com.ghgande.j2mod.modbus.net.TCPMasterConnection)

Example 3 with TCPMasterConnection

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();
}
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

TCPMasterConnection (com.ghgande.j2mod.modbus.net.TCPMasterConnection)3 ModbusTCPTransaction (com.ghgande.j2mod.modbus.io.ModbusTCPTransaction)1 ConfigException (io.openems.api.exception.ConfigException)1 InvalidValueException (io.openems.api.exception.InvalidValueException)1 OpenemsModbusException (io.openems.api.exception.OpenemsModbusException)1 InetAddress (java.net.InetAddress)1