use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-core by openhab.
the class SmokeTest method testSlaveReadErrorResponse.
/**
* Test handling of slave error responses. In this case, error code = 2, illegal data address, since no data.
*
* @throws Exception
*/
@Test
public void testSlaveReadErrorResponse() throws Exception {
ModbusSlaveEndpoint endpoint = getEndpoint();
AtomicInteger okCount = new AtomicInteger();
AtomicInteger errorCount = new AtomicInteger();
CountDownLatch callbackCalled = new CountDownLatch(1);
AtomicReference<Exception> lastError = new AtomicReference<>();
try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID, ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 0, 5, 1), result -> {
assert result.getRegisters().isPresent();
okCount.incrementAndGet();
callbackCalled.countDown();
}, failure -> {
errorCount.incrementAndGet();
lastError.set(failure.getCause());
callbackCalled.countDown();
});
assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));
assertThat(okCount.get(), is(equalTo(0)));
assertThat(errorCount.get(), is(equalTo(1)));
assertTrue(lastError.get() instanceof ModbusSlaveErrorResponseException, lastError.toString());
}
}
use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-core by openhab.
the class SmokeTest method testIOError.
/**
* Have super slow connection response, eventually resulting as timeout (due to default timeout of 3 s in
* net.wimpi.modbus.Modbus.DEFAULT_TIMEOUT)
*
* @throws Exception
*/
@Test
public void testIOError() throws Exception {
artificialServerWait = 60000;
ModbusSlaveEndpoint endpoint = getEndpoint();
AtomicInteger okCount = new AtomicInteger();
AtomicInteger errorCount = new AtomicInteger();
CountDownLatch callbackCalled = new CountDownLatch(1);
AtomicReference<Exception> lastError = new AtomicReference<>();
try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID, ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 0, 5, 1), result -> {
assert result.getRegisters().isPresent();
okCount.incrementAndGet();
callbackCalled.countDown();
}, failure -> {
errorCount.incrementAndGet();
lastError.set(failure.getCause());
callbackCalled.countDown();
});
assertTrue(callbackCalled.await(15, TimeUnit.SECONDS));
assertThat(okCount.get(), is(equalTo(0)));
assertThat(lastError.toString(), errorCount.get(), is(equalTo(1)));
assertTrue(lastError.get() instanceof ModbusSlaveIOException, lastError.toString());
}
}
Aggregations