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