Search in sources :

Example 6 with ModbusException

use of net.wimpi.modbus.ModbusException 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)

Aggregations

ModbusException (net.wimpi.modbus.ModbusException)6 ModbusIOException (net.wimpi.modbus.ModbusIOException)2 ModbusSlaveException (net.wimpi.modbus.ModbusSlaveException)2 ExceptionResponse (net.wimpi.modbus.msg.ExceptionResponse)2 ModbusResponse (net.wimpi.modbus.msg.ModbusResponse)2 ModbusSlaveConnection (net.wimpi.modbus.net.ModbusSlaveConnection)2 ModbusSlaveEndpoint (org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint)2 ModbusSerialMaster (net.wimpi.modbus.facade.ModbusSerialMaster)1 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)1 ReadCoilsRequest (net.wimpi.modbus.msg.ReadCoilsRequest)1 ReadCoilsResponse (net.wimpi.modbus.msg.ReadCoilsResponse)1 ReadInputDiscretesRequest (net.wimpi.modbus.msg.ReadInputDiscretesRequest)1 ReadInputDiscretesResponse (net.wimpi.modbus.msg.ReadInputDiscretesResponse)1 ReadInputRegistersRequest (net.wimpi.modbus.msg.ReadInputRegistersRequest)1 ReadInputRegistersResponse (net.wimpi.modbus.msg.ReadInputRegistersResponse)1 ReadMultipleRegistersRequest (net.wimpi.modbus.msg.ReadMultipleRegistersRequest)1 ReadMultipleRegistersResponse (net.wimpi.modbus.msg.ReadMultipleRegistersResponse)1 InputRegister (net.wimpi.modbus.procimg.InputRegister)1 Register (net.wimpi.modbus.procimg.Register)1 BitVector (net.wimpi.modbus.util.BitVector)1