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