Explainer
Why PLC4j Modbus-TCP item address 1 sends offset 0 on the wire
Short answer
Modbus request PDUs carry a zero-based address offset while the PLC4j item API numbers register items from one. PLC4j subtracts one before it transmits, so item address 1 sends offset 0 on the wire, which selects the register your PLC's Modbus server maps first. Enter 1 for the first location, never 0, and leave the nonzero check in place: with the subtract-one conversion, an item address of 0 would transmit -1, an invalid field value.

On this page
To read the location your PLC's Modbus server maps first, enter item address 1 in PLC4j and never 0: the request PDU that Modbus TCP puts on the wire carries a zero-based address field, PLC4j numbers register items from one and subtracts one before it transmits, so the item address you type reaches the PLC as the offset one lower. That arithmetic is also why the nonzero check in addItem() must stay where it is — an item address of 0 would transmit as -1, a value the unsigned 16-bit address field cannot carry, so no PLC could answer the frame. The sections below separate the three numbers involved, then work a read of ten holding registers from the first mapped location through to the captured bytes.
Three numbers for one register #
One read touches three different numbers, and most addressing confusion comes from merging them.
- Item address — the value you hand PLC4j when you define a register item, through
addItem()in the code this question concerns. Item addresses are one-based, so the first item is 1. - Reference number — the one-based identifier used in Modbus documentation and tag lists, of the 40001 form for holding registers, where reference 40001 names the first holding register.
- Wire offset — the unsigned 16-bit address field inside the request PDU. It counts from zero, so offset 0 is the first register of the object, and 65535 is the largest value the field can hold.
The PLC's own memory addresses are a fourth number and none of the three above. The PLC's Modbus server maps chosen memory areas onto Modbus objects, so whether wire offset 0 lands on the memory location you have in mind is decided by that server mapping, not by PLC4j. Mixing a controller's internal numbering with a fieldbus offset is the same layer error behind why a Node-RED S7 read succeeds on the wrong DB address. Keep the layers apart and the reported symptom dissolves: the first mapped location was always reachable, as item 1.
What the specification requires #
The MODBUS Application Protocol Specification V1.1b3 fixes the convention: within the PDU, registers are addressed from zero, and a read request carries the offset of the first register plus the quantity wanted. References such as 40001 exist for people, documents and tag lists; the protocol on the wire never sees them.
A request for the first register is therefore not an edge case. Offset 0 is an ordinary field value — the field's rule is its range, 0 through 65535, not a ban on zero. The same numeral means something different in other frame fields: a unit identifier of 0 on a serial link is the broadcast address, a separate fault described in why Modbus RTU sends to slave address 0 instead of your device. Judge each field by its own rule.
Other engineering tools expose this same zero-based field under their own syntax; what CODESYS %IW0 means for a Modbus TCP holding register is one worked instance of a client-side address landing on this wire field.
How PLC4j converts the item address #
In PLC4j's Modbus-TCP model, register items are one-based and the library subtracts one when it builds the request, so the wire field is always the item address minus one. That conversion, together with a validation that refuses item address 0 in addItem(), is what the reported symptom is made of. Confirm it once on your build with a request capture rather than assuming, because conversion details belong to the library version you run.
The nonzero check is not an oversight. Apply the conversion to the value the check refuses: item address 0 minus one is -1, and an unsigned 16-bit field has no encoding for it. Deleting validation does not create a zero-based mode; it produces a frame no conformant PLC can answer. If a build offers a documented zero-based setting, that setting is the only legitimate way to change the numbering — on the PLC4j build this behavior was reported on, such an option was proposed as a possible change, not shipped as a setting — so plan on item address = offset + 1.
Worked example: ten holding registers from offset 0 #
Suppose a logging application needs the first ten holding registers: references 40001 through 40010, wire offsets 0 through 9.
- Item addresses 1 through 10, because item address = offset + 1 — check both ends of the range: 1 - 1 = 0 and 10 - 1 = 9.
- One request carries all ten: function code 3, an offset field of 0x0000, and a quantity of 0x000A, the hex form of 10.
- The response returns a byte count of 20 and then the data: ten registers at two bytes each, 10 * 2 = 20.
Identity check: the word returned under item address 1 must equal the value the PLC holds at the location its Modbus server maps to offset 0. If it does not, the server mapping is what needs fixing, not the addressing convention — find the mapping before adjusting any address. Reads are harmless at a wrong offset; before you write to a location you found by offset, confirm the mapping against the PLC program, because the frame lands wherever the server mapped that offset, and that can be a live setpoint.
Faults that look like this one #
- Double compensation. If you enter the wire offset where the item address belongs, every read lands one register early: entering 1 for the register at offset 1 returns offset 0 instead. Convert once, yourself, exactly as offset + 1, and let the library's subtraction be the only other one.
- Language habits imported into protocol fields. Most programming languages count array positions from zero. That fact describes your code's memory, not which field value a Modbus PLC expects; the two conventions coexist in one program without conflict.
- A clean read with a nonsense value blamed on addressing. An addressing fault usually returns the neighboring register or an exception code. A successful read with a garbled number points at interpretation — datatype, byte order within each register, scaling — and is fixed by decoding per the PLC's documented map, not by moving the address.
What to do on your project #
- For the first mapped location, at wire offset 0, enter item address 1. Never enter 0.
- For a register documented with a 40001-style reference, compute item address = reference - 40000; the wire offset is that result minus one again. A register at reference 40016 is item address 16 and travels at offset 15.
- For a block read from the first register, enter items 1 through the quantity you want, with contiguous offsets, and one request covers the whole run.
- Confirm the conversion once per build with a request capture: the frame that fetches the first register must show function code 3 and an offset field of 0x0000.
- Leave the nonzero check in place and never pass 0: the subtraction turns it into -1 on the wire, a field value with no meaning to any PLC.
Questions asked
Does the subtract-one conversion apply to coils and discrete inputs too?
Yes. All four Modbus data objects, coils, discrete inputs, input registers and holding registers, share the same zero-based PDU address field, and the function code selects which object an offset refers to. The conversion arithmetic is identical for every object type, and each object has its own first point: coil 1 and the first holding register are different objects even though both sit at the low end of their own ranges.
How many registers can one read request carry?
A read of holding registers can ask for up to 125 registers in a single transaction, with contiguous offsets from the first one requested. One exchange covering references 40001 through 40125, offsets 0 through 124, replaces 125 separate requests and is faster on the wire. Coils allow up to 2000 points per read, and the item-to-offset conversion is identical either way.
Why does the captured response show no address field?
A read response does not echo the requested offset. Its PDU carries the function code, a byte count and the register data, so the address exists only in the request. To verify the conversion, capture the request itself: the frame that fetches the first register must show its offset field as zero, captured on the wire as 0x0000. An exception response instead carries an exception code naming why the request was refused.
A register item reads successfully but the value is wrong - is addressing still at fault?
Usually not. A wrong offset returns the neighboring register or an exception code, so a clean success with a garbled number points at interpretation instead: datatype, byte order within each register, and scaling. Decode the returned words according to the PLC's documented map and compare one of them against the location the server maps at the offset you requested. Change addresses only after the interpretation checks out.
Does anything change on Modbus RTU over a serial link?
The conversion does not. Serial links carry the same PDU, with TCP adding only the MBAP header around it, so subtracting one from the one-based item address is identical on RTU. What serial adds is a unit identifier that selects the slave, where a value of zero means broadcast, plus timing rules between frames. The address arithmetic inside the PDU is untouched by the transport.

