Hi Sebastian,
thanks for the fast reply, so the 16 bit register can be fetched as byte array?
And if I need two registers (should be a 32 bit value altogether) I put two
items in the builder?
I just looked at our test code from marcel and its like
PlcReadRequest plcReadRequest1 = new PlcReadRequest();
// ReadRequestItem<byte[]> readRequestItem1 = new
ReadRequestItem<>(byte[].class,
ReadInputRegistersModbusAddress.of(ADDRESS_PREFIX +
modbusAddressToKafka.getAddress()), 2);
// plcReadRequest1.addItem(readRequestItem1);
//
// CompletableFuture<PlcReadResponse> completableFuture1 =
plcConnection.read(plcReadRequest1);
//
// PlcReadResponse plcReadResponse = completableFuture1.get(TIMEOUT,
TimeUnit.MILLISECONDS);
// Optional<ReadResponseItem<byte[]>> ret1 =
plcReadResponse.getValue(readRequestItem1);
// if (ret1.isPresent() && ret1.get().getValues().size() == 2) {
// ByteBuffer byteBuffer = ByteBuffer.allocate(4);
// byteBuffer.put(0, ret1.get().getValues().get(0)[0]);
// byteBuffer.put(1, ret1.get().getValues().get(0)[1]);
// byteBuffer.put(2, ret1.get().getValues().get(1)[0]);
// byteBuffer.put(3, ret1.get().getValues().get(1)[1]);
// return byteBuffer.getInt();
// } else {
// throw new ModbusReadException(TIMEOUT_ERROR_MSG + " " +
modbusAddressToKafka.getAddress());
// }
And I don’t like the byteBuffer part as we as client in this case should not
bother with stuff like that.
So after changing to the new API I build to requests for two subsequent
addresses (named with their address) and do:
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.put(0,
response.getByteArray(Integer.toString(modbusAddressToKafka.getAddress()))[0]);
byteBuffer.put(1,
response.getByteArray(Integer.toString(modbusAddressToKafka.getAddress()))[1]);
byteBuffer.put(2,
response.getByteArray(Integer.toString(modbusAddressToKafka.getAddress() +
1))[0]);
byteBuffer.put(3,
response.getByteArray(Integer.toString(modbusAddressToKafka.getAddress() +
1))[1]);
return byteBuffer.getInt();
Or is there any simpler approach to this?
Best
Julian
Am 18.09.18, 08:08 schrieb "Sebastian Rühl"
<[email protected]>:
Hi Julian,
Two bytes would be a register so you could read it like that („register:7"):
https://github.com/apache/incubator-plc4x/blob/master/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/ManualPlc4XModbusTest.java
<https://github.com/apache/incubator-plc4x/blob/master/plc4j/protocols/modbus/src/test/java/org/apache/plc4x/java/modbus/ManualPlc4XModbusTest.java>
All in all it seems that this driver is only able to read one register/coil
at a time a would need some work after the refactoring.
Types of addresses can be found here:
https://github.com/apache/incubator-plc4x/tree/master/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/model
<https://github.com/apache/incubator-plc4x/tree/master/plc4j/protocols/modbus/src/main/java/org/apache/plc4x/java/modbus/model>
Sebastian
> Am 18.09.2018 um 08:01 schrieb Julian Feinauer
<[email protected]>:
>
> Hi all,
>
> I am currently migrating some code to the new plc4j API and are a bit
unsure what to do.
> The current code is
>
> PlcReadRequest plcReadRequest1 = new PlcReadRequest();
> ReadRequestItem<byte[]> readRequestItem1 = new
ReadRequestItem<>(byte[].class,
plcConnection.parseAddress(someAddressStringHere), 2);
> plcReadRequest1.addItem(readRequestItem1);
>
> So we fetch 2 bytes from Modbus.
> How do I tell him to fetch an array of length 2 in the new API?
>
> Thanks!
> Julian