Search in sources :

Example 6 with ModbusSlaveConnection

use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.

the class ModbusSlaveConnectionFactoryImpl method activateObject.

@Override
public void activateObject(ModbusSlaveEndpoint endpoint, PooledObject<ModbusSlaveConnection> obj) throws Exception {
    if (obj.getObject() == null) {
        return;
    }
    try {
        ModbusSlaveConnection connection = obj.getObject();
        EndpointPoolConfiguration config = endpointPoolConfigs.get(endpoint);
        if (connection.isConnected()) {
            if (config != null) {
                long waited = waitAtleast(lastPassivateMillis.get(endpoint), config.getPassivateBorrowMinMillis());
                logger.trace("Waited {}ms (passivateBorrowMinMillis {}ms) before giving returning connection {} for endpoint {}, to ensure delay between transactions.", waited, config.getPassivateBorrowMinMillis(), obj.getObject(), endpoint);
            }
        } else {
            // invariant: !connection.isConnected()
            tryConnect(endpoint, obj, connection, config);
        }
    } catch (Exception e) {
        logger.error("Error connecting connection {} for endpoint {}: {}", obj.getObject(), endpoint, e.getMessage());
    }
}
Also used : ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) UnknownHostException(java.net.UnknownHostException)

Example 7 with ModbusSlaveConnection

use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.

the class ModbusBinding method reconstructConnectionPool.

private static void reconstructConnectionPool() {
    connectionFactory = new ModbusSlaveConnectionFactoryImpl();
    GenericKeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection> genericKeyedObjectPool = new GenericKeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection>(connectionFactory, poolConfig);
    genericKeyedObjectPool.setSwallowedExceptionListener(new SwallowedExceptionListener() {

        @Override
        public void onSwallowException(Exception e) {
            logger.error("Connection pool swallowed unexpected exception: {}", e.getMessage());
        }
    });
    connectionPool = genericKeyedObjectPool;
}
Also used : GenericKeyedObjectPool(org.apache.commons.pool2.impl.GenericKeyedObjectPool) ModbusSlaveEndpoint(org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint) SwallowedExceptionListener(org.apache.commons.pool2.SwallowedExceptionListener) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) ModbusSlaveConnectionFactoryImpl(org.openhab.binding.modbus.internal.pooling.ModbusSlaveConnectionFactoryImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with ModbusSlaveConnection

use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.

the class ModbusSlave method getModbusData.

/**
     * Executes Modbus transaction that reads data from the device and returns response data
     *
     * @param request describes what data are requested from the device
     * @return response data
     * @throws ModbusConnectionException when connection cannot be established
     * @throws ModbusException ModbusIOException on IO errors, ModbusSlaveException with protocol level exceptions
     * @throws ModbusUnexpectedTransactionIdException when response transaction id does not match the request
     */
private ModbusResponse getModbusData(ModbusRequest request) throws ModbusConnectionException, ModbusException, ModbusUnexpectedTransactionIdException {
    ModbusSlaveEndpoint endpoint = getEndpoint();
    ModbusSlaveConnection connection = null;
    ModbusResponse response = null;
    try {
        connection = getConnection(endpoint);
        if (connection == null) {
            logger.warn("ModbusSlave ({}) not connected -- aborting read request {}. Endpoint {}", name, request, endpoint);
            throw new ModbusConnectionException(endpoint);
        }
        request.setUnitID(getId());
        transaction.setRequest(request);
        try {
            transaction.execute();
        } catch (ModbusException e) {
            logger.error("ModbusSlave ({}): Error getting modbus data for request {}. Error: {}. Endpoint {}. Connection: {}", name, request, e.getMessage(), endpoint, connection);
            invalidate(endpoint, connection);
            // Invalidated connections should not be returned
            connection = null;
            throw e;
        }
        response = transaction.getResponse();
        if ((response.getTransactionID() != transaction.getTransactionID()) && !response.isHeadless()) {
            logger.warn("ModbusSlave ({}): Transaction id of the response does not match request {}.  Endpoint {}. Connection: {}. Ignoring response.", name, request, endpoint, connection);
            throw new ModbusUnexpectedTransactionIdException();
        }
    } finally {
        returnConnection(endpoint, connection);
    }
    return response;
}
Also used : ModbusSlaveEndpoint(org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint) ModbusResponse(net.wimpi.modbus.msg.ModbusResponse) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) ModbusException(net.wimpi.modbus.ModbusException)

Example 9 with ModbusSlaveConnection

use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.

the class ModbusTcpSlave method getConnection.

/**
     * Performs physical write to device when slave type is "holding" using Modbus FC06 function
     * 
     * @param command command received from OpenHAB
     * @param readRegister reference to the register that stores current value
     * @param writeRegister register reference to write data to
     */
@Override
protected ModbusSlaveConnection getConnection(ModbusSlaveEndpoint endpoint) {
    ModbusSlaveConnection connection = super.getConnection(endpoint);
    if (connection == null) {
        return null;
    }
    if (!(connection instanceof TCPMasterConnection)) {
        // should not happen
        throw new IllegalStateException("Should not happen: wrong connection type for slave " + name);
    }
    ((ModbusTCPTransaction) transaction).setConnection((TCPMasterConnection) connection);
    return connection;
}
Also used : ModbusTCPTransaction(net.wimpi.modbus.io.ModbusTCPTransaction) TCPMasterConnection(net.wimpi.modbus.net.TCPMasterConnection) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection)

Example 10 with ModbusSlaveConnection

use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.

the class ModbusUdpSlave method getConnection.

@Override
protected ModbusSlaveConnection getConnection(ModbusSlaveEndpoint endpoint) {
    ModbusSlaveConnection connection = super.getConnection(endpoint);
    if (connection == null) {
        return null;
    }
    if (!(connection instanceof UDPMasterConnection)) {
        throw new IllegalStateException("Should not happen: wrong connection type for slave " + name);
    }
    ((ModbusUDPTransaction) transaction).setTerminal(((UDPMasterConnection) connection).getTerminal());
    return connection;
}
Also used : ModbusUDPTransaction(net.wimpi.modbus.io.ModbusUDPTransaction) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) UDPMasterConnection(net.wimpi.modbus.net.UDPMasterConnection)

Aggregations

ModbusSlaveConnection (net.wimpi.modbus.net.ModbusSlaveConnection)10 ModbusSlaveEndpoint (org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint)4 ModbusException (net.wimpi.modbus.ModbusException)3 ModbusResponse (net.wimpi.modbus.msg.ModbusResponse)2 UnknownHostException (java.net.UnknownHostException)1 NoSuchElementException (java.util.NoSuchElementException)1 ModbusSerialTransaction (net.wimpi.modbus.io.ModbusSerialTransaction)1 ModbusTCPTransaction (net.wimpi.modbus.io.ModbusTCPTransaction)1 ModbusUDPTransaction (net.wimpi.modbus.io.ModbusUDPTransaction)1 SerialConnection (net.wimpi.modbus.net.SerialConnection)1 TCPMasterConnection (net.wimpi.modbus.net.TCPMasterConnection)1 UDPMasterConnection (net.wimpi.modbus.net.UDPMasterConnection)1 SwallowedExceptionListener (org.apache.commons.pool2.SwallowedExceptionListener)1 GenericKeyedObjectPool (org.apache.commons.pool2.impl.GenericKeyedObjectPool)1 Test (org.junit.Test)1 ModbusSlaveConnectionFactoryImpl (org.openhab.binding.modbus.internal.pooling.ModbusSlaveConnectionFactoryImpl)1 ModbusTCPSlaveEndpoint (org.openhab.binding.modbus.internal.pooling.ModbusTCPSlaveEndpoint)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1