Search in sources :

Example 1 with BitArray

use of org.openhab.core.io.transport.modbus.BitArray in project openhab-core by openhab.

the class SmokeTest method testOneOffWriteMultipleCoil.

/**
 * @throws Exception
 */
@Test
public void testOneOffWriteMultipleCoil() throws Exception {
    LoggerFactory.getLogger(this.getClass()).error("STARTING MULTIPLE");
    generateData();
    ModbusSlaveEndpoint endpoint = getEndpoint();
    AtomicInteger unexpectedCount = new AtomicInteger();
    AtomicReference<Object> lastData = new AtomicReference<>();
    BitArray bits = new BitArray(true, true, false, false, true, true);
    try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
        comms.submitOneTimeWrite(new ModbusWriteCoilRequestBlueprint(SLAVE_UNIT_ID, 3, bits, true, 1), result -> {
            lastData.set(result.getResponse());
        }, failure -> {
            unexpectedCount.incrementAndGet();
        });
        waitForAssert(() -> {
            assertThat(unexpectedCount.get(), is(equalTo(0)));
            assertThat(lastData.get(), is(notNullValue()));
            ModbusResponse response = (ModbusResponse) lastData.get();
            assertThat(response.getFunctionCode(), is(equalTo(15)));
            assertThat(modbustRequestCaptor.getAllReturnValues().size(), is(equalTo(1)));
            ModbusRequest request = modbustRequestCaptor.getAllReturnValues().get(0);
            assertThat(request.getFunctionCode(), is(equalTo(15)));
            assertThat(((WriteMultipleCoilsRequest) request).getReference(), is(equalTo(3)));
            assertThat(((WriteMultipleCoilsRequest) request).getBitCount(), is(equalTo(bits.size())));
            BitVector writeRequestCoils = ((WriteMultipleCoilsRequest) request).getCoils();
            BitArray writtenBits = new BitArray(BitSet.valueOf(writeRequestCoils.getBytes()), bits.size());
            assertThat(writtenBits, is(equalTo(bits)));
        }, 6000, 10);
    }
    LoggerFactory.getLogger(this.getClass()).error("ENDINGMULTIPLE");
}
Also used : BitVector(net.wimpi.modbus.util.BitVector) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteMultipleCoilsRequest(net.wimpi.modbus.msg.WriteMultipleCoilsRequest) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) ModbusResponse(org.openhab.core.io.transport.modbus.ModbusResponse) ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) BitArray(org.openhab.core.io.transport.modbus.BitArray) ModbusWriteCoilRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint) Test(org.junit.jupiter.api.Test)

Example 2 with BitArray

use of org.openhab.core.io.transport.modbus.BitArray in project openhab-core by openhab.

the class SmokeTest method testRegularReadEvery150msWithCoil.

/**
 * Testing regular polling of coils
 *
 * Amount of requests is timed, and average poll period is checked
 *
 * @throws Exception
 */
@Test
public void testRegularReadEvery150msWithCoil() throws Exception {
    generateData();
    ModbusSlaveEndpoint endpoint = getEndpoint();
    AtomicInteger unexpectedCount = new AtomicInteger();
    CountDownLatch callbackCalled = new CountDownLatch(5);
    AtomicInteger dataReceived = new AtomicInteger();
    long start = System.currentTimeMillis();
    try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
        comms.registerRegularPoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID, ModbusReadFunctionCode.READ_COILS, 1, 15, 1), 150, 0, result -> {
            Optional<BitArray> bitsOptional = result.getBits();
            if (bitsOptional.isPresent()) {
                BitArray bits = bitsOptional.get();
                dataReceived.incrementAndGet();
                try {
                    assertThat(bits.size(), is(equalTo(15)));
                    testCoilValues(bits, 1);
                } catch (AssertionError e) {
                    unexpectedCount.incrementAndGet();
                }
            } else {
                unexpectedCount.incrementAndGet();
            }
            callbackCalled.countDown();
        }, failure -> {
            unexpectedCount.incrementAndGet();
            callbackCalled.countDown();
        });
        assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));
        long end = System.currentTimeMillis();
        assertPollDetails(unexpectedCount, dataReceived, start, end, 145, 500);
    }
}
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) BitArray(org.openhab.core.io.transport.modbus.BitArray) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 3 with BitArray

use of org.openhab.core.io.transport.modbus.BitArray in project openhab-core by openhab.

the class SmokeTest method testOneOffWriteMultipleCoilError.

/**
 * Write is out-of-bounds, slave should return error
 *
 * @throws Exception
 */
@Test
public void testOneOffWriteMultipleCoilError() throws Exception {
    generateData();
    ModbusSlaveEndpoint endpoint = getEndpoint();
    AtomicInteger unexpectedCount = new AtomicInteger();
    CountDownLatch callbackCalled = new CountDownLatch(1);
    AtomicReference<Exception> lastError = new AtomicReference<>();
    BitArray bits = new BitArray(500);
    try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
        comms.submitOneTimeWrite(new ModbusWriteCoilRequestBlueprint(SLAVE_UNIT_ID, 3, bits, true, 1), result -> {
            unexpectedCount.incrementAndGet();
            callbackCalled.countDown();
        }, failure -> {
            lastError.set(failure.getCause());
            callbackCalled.countDown();
        });
        assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));
        assertThat(unexpectedCount.get(), is(equalTo(0)));
        assertTrue(lastError.get() instanceof ModbusSlaveErrorResponseException, lastError.toString());
        assertThat(modbustRequestCaptor.getAllReturnValues().size(), is(equalTo(1)));
        ModbusRequest request = modbustRequestCaptor.getAllReturnValues().get(0);
        assertThat(request.getFunctionCode(), is(equalTo(15)));
        assertThat(((WriteMultipleCoilsRequest) request).getReference(), is(equalTo(3)));
        assertThat(((WriteMultipleCoilsRequest) request).getBitCount(), is(equalTo(bits.size())));
        BitVector writeRequestCoils = ((WriteMultipleCoilsRequest) request).getCoils();
        BitArray writtenBits = new BitArray(BitSet.valueOf(writeRequestCoils.getBytes()), bits.size());
        assertThat(writtenBits, is(equalTo(bits)));
    }
}
Also used : BitVector(net.wimpi.modbus.util.BitVector) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) WriteMultipleCoilsRequest(net.wimpi.modbus.msg.WriteMultipleCoilsRequest) ModbusCommunicationInterface(org.openhab.core.io.transport.modbus.ModbusCommunicationInterface) ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) 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) ModbusSlaveErrorResponseException(org.openhab.core.io.transport.modbus.exception.ModbusSlaveErrorResponseException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BitArray(org.openhab.core.io.transport.modbus.BitArray) ModbusWriteCoilRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint) Test(org.junit.jupiter.api.Test)

Example 4 with BitArray

use of org.openhab.core.io.transport.modbus.BitArray in project openhab-core by openhab.

the class SmokeTest method testOneOffReadWithDiscreteOrCoils.

public void testOneOffReadWithDiscreteOrCoils(ModbusReadFunctionCode functionCode, int count) throws Exception {
    assertThat(functionCode, is(anyOf(equalTo(ModbusReadFunctionCode.READ_INPUT_DISCRETES), equalTo(ModbusReadFunctionCode.READ_COILS))));
    generateData();
    ModbusSlaveEndpoint endpoint = getEndpoint();
    AtomicInteger unexpectedCount = new AtomicInteger();
    CountDownLatch callbackCalled = new CountDownLatch(1);
    AtomicReference<Object> lastData = new AtomicReference<>();
    final int offset = 1;
    try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
        comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID, functionCode, offset, count, 1), result -> {
            Optional<BitArray> bitsOptional = result.getBits();
            if (bitsOptional.isPresent()) {
                lastData.set(bitsOptional.get());
            } else {
                unexpectedCount.incrementAndGet();
            }
            callbackCalled.countDown();
        }, failure -> {
            unexpectedCount.incrementAndGet();
            callbackCalled.countDown();
        });
        assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));
        assertThat(unexpectedCount.get(), is(equalTo(0)));
        BitArray bits = (BitArray) lastData.get();
        assertThat(bits, notNullValue());
        assertThat(bits.size(), is(equalTo(count)));
        if (functionCode == ModbusReadFunctionCode.READ_INPUT_DISCRETES) {
            testDiscreteValues(bits, offset);
        } else {
            testCoilValues(bits, offset);
        }
    }
}
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) BitArray(org.openhab.core.io.transport.modbus.BitArray) CountDownLatch(java.util.concurrent.CountDownLatch) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusTCPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint) ModbusWriteCoilRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)

Example 5 with BitArray

use of org.openhab.core.io.transport.modbus.BitArray in project openhab-core by openhab.

the class ModbusLibraryWrapper method invokeCallbackWithResponse.

/**
 * Invoke callback with the data received
 *
 * @param message original request
 * @param callback callback for read
 * @param response Modbus library response object
 */
public static void invokeCallbackWithResponse(ModbusReadRequestBlueprint request, ModbusReadCallback callback, ModbusResponse response) {
    try {
        getLogger().trace("Calling read response callback {} for request {}. Response was {}", callback, request, response);
        // The number of coils/discrete inputs received in response are always in the multiples of 8
        // bits.
        // So even if querying 5 bits, you will actually get 8 bits. Here we wrap the data in
        // BitArrayWrappingBitVector
        // with will validate that the consumer is not accessing the "invalid" bits of the response.
        int dataItemsInResponse = getNumberOfItemsInResponse(response, request);
        if (request.getFunctionCode() == ModbusReadFunctionCode.READ_COILS) {
            BitVector bits = ((ReadCoilsResponse) response).getCoils();
            BitArray payload = bitArrayFromBitVector(bits, Math.min(dataItemsInResponse, request.getDataLength()));
            callback.handle(new AsyncModbusReadResult(request, payload));
        } else if (request.getFunctionCode() == ModbusReadFunctionCode.READ_INPUT_DISCRETES) {
            BitVector bits = ((ReadInputDiscretesResponse) response).getDiscretes();
            BitArray payload = bitArrayFromBitVector(bits, Math.min(dataItemsInResponse, request.getDataLength()));
            callback.handle(new AsyncModbusReadResult(request, payload));
        } else if (request.getFunctionCode() == ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS) {
            ModbusRegisterArray payload = modbusRegisterArrayFromInputRegisters(((ReadMultipleRegistersResponse) response).getRegisters());
            callback.handle(new AsyncModbusReadResult(request, payload));
        } else if (request.getFunctionCode() == ModbusReadFunctionCode.READ_INPUT_REGISTERS) {
            ModbusRegisterArray payload = modbusRegisterArrayFromInputRegisters(((ReadInputRegistersResponse) response).getRegisters());
            callback.handle(new AsyncModbusReadResult(request, payload));
        } else {
            throw new IllegalArgumentException(String.format("Unexpected function code %s", request.getFunctionCode()));
        }
    } finally {
        getLogger().trace("Called read response callback {} for request {}. Response was {}", callback, request, response);
    }
}
Also used : BitVector(net.wimpi.modbus.util.BitVector) ModbusRegisterArray(org.openhab.core.io.transport.modbus.ModbusRegisterArray) ReadCoilsResponse(net.wimpi.modbus.msg.ReadCoilsResponse) BitArray(org.openhab.core.io.transport.modbus.BitArray) AsyncModbusReadResult(org.openhab.core.io.transport.modbus.AsyncModbusReadResult) ModbusWriteCoilRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint) ModbusUDPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusUDPSlaveEndpoint) ModbusReadRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint) ModbusWriteRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint) ModbusWriteRegisterRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint) ModbusTCPSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint) ModbusSerialSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSerialSlaveEndpoint) ModbusSlaveEndpoint(org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint) ReadInputRegistersResponse(net.wimpi.modbus.msg.ReadInputRegistersResponse)

Aggregations

BitArray (org.openhab.core.io.transport.modbus.BitArray)16 Test (org.junit.jupiter.api.Test)12 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)8 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)5 ModbusReadRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint)4 WriteMultipleCoilsRequest (net.wimpi.modbus.msg.WriteMultipleCoilsRequest)3 BitVector (net.wimpi.modbus.util.BitVector)3 ModbusWriteRegisterRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AsyncModbusReadResult (org.openhab.core.io.transport.modbus.AsyncModbusReadResult)2 ModbusRegisterArray (org.openhab.core.io.transport.modbus.ModbusRegisterArray)2 ModbusResponse (org.openhab.core.io.transport.modbus.ModbusResponse)2 ModbusWriteRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprint)2