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;
}
Aggregations