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