Search in sources :

Example 1 with WriteMultipleCoilsRequest

use of net.wimpi.modbus.msg.WriteMultipleCoilsRequest 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 WriteMultipleCoilsRequest

use of net.wimpi.modbus.msg.WriteMultipleCoilsRequest 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 3 with WriteMultipleCoilsRequest

use of net.wimpi.modbus.msg.WriteMultipleCoilsRequest in project openhab-core by openhab.

the class ModbusLibraryWrapper method createRequest.

/**
 * Convert the general request to Modbus library request object
 *
 * @param message
 * @throws IllegalArgumentException
 *             1) in case function code implies coil data but we have registers
 *             2) in case function code implies register data but we have coils
 *             3) in case there is no data
 *             4) in case there is too much data in case of WRITE_COIL or WRITE_SINGLE_REGISTER
 * @throws IllegalStateException unexpected function code. Implementation is lacking and this can be considered a
 *             bug
 * @return MODBUS library request matching the write request
 */
public static ModbusRequest createRequest(ModbusWriteRequestBlueprint message) {
    // ModbusRequest[] request = new ModbusRequest[1];
    AtomicReference<ModbusRequest> request = new AtomicReference<>();
    AtomicBoolean writeSingle = new AtomicBoolean(false);
    switch(message.getFunctionCode()) {
        case WRITE_COIL:
            writeSingle.set(true);
        // fall-through on purpose
        case WRITE_MULTIPLE_COILS:
            message.accept(new ModbusWriteRequestBlueprintVisitor() {

                @Override
                public void visit(ModbusWriteRegisterRequestBlueprint blueprint) {
                    throw new IllegalArgumentException();
                }

                @Override
                public void visit(ModbusWriteCoilRequestBlueprint blueprint) {
                    BitArray coils = blueprint.getCoils();
                    if (coils.size() == 0) {
                        throw new IllegalArgumentException("Must provide at least one coil");
                    }
                    if (writeSingle.get()) {
                        if (coils.size() != 1) {
                            throw new IllegalArgumentException("Must provide single coil with WRITE_COIL");
                        }
                        request.set(new WriteCoilRequest(message.getReference(), coils.getBit(0)));
                    } else {
                        request.set(new WriteMultipleCoilsRequest(message.getReference(), ModbusLibraryWrapper.convertBits(coils)));
                    }
                }
            });
            break;
        case WRITE_SINGLE_REGISTER:
            writeSingle.set(true);
        // fall-through on purpose
        case WRITE_MULTIPLE_REGISTERS:
            message.accept(new ModbusWriteRequestBlueprintVisitor() {

                @Override
                public void visit(ModbusWriteRegisterRequestBlueprint blueprint) {
                    Register[] registers = ModbusLibraryWrapper.convertRegisters(blueprint.getRegisters());
                    if (registers.length == 0) {
                        throw new IllegalArgumentException("Must provide at least one register");
                    }
                    if (writeSingle.get()) {
                        if (blueprint.getRegisters().size() != 1) {
                            throw new IllegalArgumentException("Must provide single register with WRITE_SINGLE_REGISTER");
                        }
                        request.set(new WriteSingleRegisterRequest(message.getReference(), registers[0]));
                    } else {
                        request.set(new WriteMultipleRegistersRequest(message.getReference(), registers));
                    }
                }

                @Override
                public void visit(ModbusWriteCoilRequestBlueprint blueprint) {
                    throw new IllegalArgumentException();
                }
            });
            break;
        default:
            getLogger().error("Unexpected function code {}", message.getFunctionCode());
            throw new IllegalStateException(String.format("Unexpected function code %s", message.getFunctionCode()));
    }
    ModbusRequest modbusRequest = request.get();
    modbusRequest.setUnitID(message.getUnitID());
    modbusRequest.setProtocolID(message.getProtocolID());
    return modbusRequest;
}
Also used : WriteSingleRegisterRequest(net.wimpi.modbus.msg.WriteSingleRegisterRequest) WriteMultipleCoilsRequest(net.wimpi.modbus.msg.WriteMultipleCoilsRequest) ModbusRequest(net.wimpi.modbus.msg.ModbusRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModbusWriteRequestBlueprintVisitor(org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprintVisitor) ModbusWriteRegisterRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BitArray(org.openhab.core.io.transport.modbus.BitArray) WriteCoilRequest(net.wimpi.modbus.msg.WriteCoilRequest) WriteMultipleRegistersRequest(net.wimpi.modbus.msg.WriteMultipleRegistersRequest) ModbusWriteCoilRequestBlueprint(org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ModbusRequest (net.wimpi.modbus.msg.ModbusRequest)3 WriteMultipleCoilsRequest (net.wimpi.modbus.msg.WriteMultipleCoilsRequest)3 BitArray (org.openhab.core.io.transport.modbus.BitArray)3 ModbusWriteCoilRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteCoilRequestBlueprint)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BitVector (net.wimpi.modbus.util.BitVector)2 Test (org.junit.jupiter.api.Test)2 ModbusCommunicationInterface (org.openhab.core.io.transport.modbus.ModbusCommunicationInterface)2 ModbusSlaveEndpoint (org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 WriteCoilRequest (net.wimpi.modbus.msg.WriteCoilRequest)1 WriteMultipleRegistersRequest (net.wimpi.modbus.msg.WriteMultipleRegistersRequest)1 WriteSingleRegisterRequest (net.wimpi.modbus.msg.WriteSingleRegisterRequest)1 ModbusResponse (org.openhab.core.io.transport.modbus.ModbusResponse)1 ModbusWriteRegisterRequestBlueprint (org.openhab.core.io.transport.modbus.ModbusWriteRegisterRequestBlueprint)1 ModbusWriteRequestBlueprintVisitor (org.openhab.core.io.transport.modbus.ModbusWriteRequestBlueprintVisitor)1