Search in sources :

Example 46 with ModbusReadRequestBlueprint

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());
    }
}
Also used : ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ModbusConnectionException(org.openhab.core.io.transport.modbus.exception.ModbusConnectionException) IOException(java.io.IOException) ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModbusSlaveIOException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException) Test(org.junit.jupiter.api.Test)

Example 47 with ModbusReadRequestBlueprint

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());
    }
}
Also used : ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModbusSlaveIOException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException) CountDownLatch(java.util.concurrent.CountDownLatch) ModbusConnectionException(org.openhab.core.io.transport.modbus.exception.ModbusConnectionException) IOException(java.io.IOException) ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModbusSlaveIOException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveIOException) Test(org.junit.jupiter.api.Test)

Aggregations

ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)47 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)22 Test (org.junit.jupiter.api.Test)21 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)21 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)19 PollTask (org.openhab.core.io.transport.modbus.PollTask)17 ModbusDataThingHandler (org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler)14 Configuration (org.openhab.core.config.core.Configuration)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)12 ModbusTCPSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)10 Bridge (org.openhab.core.thing.Bridge)9 Thing (org.openhab.core.thing.Thing)9 ModbusPollerThingHandler (org.openhab.binding.modbus.handler.ModbusPollerThingHandler)8 ModbusReadCallback (org.openhab.core.io.transport.modbus.ModbusReadCallback)8 ModbusWriteRegisterRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)8 ModbusWriteRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint)7