Multiple register writes
-
I saw a few postings about this here but no answers.
I have a server listening for Modbus connections and I have about 20 sequential registers I would like to write to and also read back 10 registers.
As this will be over a GPRS connection, I would like to keep the number of bytes down and the present way of using a setValue for each register consumes too much data.
I see there is a reference in the forum to sendBatch but this function doesn't exist in the code I downloaded.
Modbus supports multiple reads and writes but this seems to be missing from the Modbus4j code or am I missing something?
How do I go about writing and reading to and from multiple registers.
-
I got it working.
For anyone else, have a look at WriteRegistersRequest.
-
Hello v8dave,
oour needs are exactly opposites : I want to write a single register (function 0x06) and my Mango instance only write multiple registers (0x16) and you want to writemultiple registers and your mango only writes one by one !!!
:-)
happy that ou found the solution but by the way how did you manage to write a single register ???
-
@nikorun said:
Hello v8dave,
happy that ou found the solution but by the way how did you manage to write a single register ???I just used the setValue() call. It takes a single value and writes this to the register. eg,
master.setValue(1, RegisterRange.INPUT_REGISTER, 0, DataType.TWO_BYTE_INT_SIGNED, value);
I think you'll find that it uses 16 because it handles other sizes of data. Eg, FOUR_BYTE_INT_SIGNED would write out to 2 registers but it would handle the Endian support for you. You would only have to worry about writing the value and not have to consider breaking the 32 bit value into 2 16 bit values. The library takes care of this for you.
The issue with this is if the Modbus device you have does not support function 16. You could modify the library to use function 6 if the value being written was of the 2 byte types. The source is there so this is very possible.
-
@nikorun
where this master.setValue
(...) line should be added ?