IMDHMIControls LibraryShop main store

Explainer

What CODESYS %IW0 means for a Modbus TCP holding register

Industrial networks Published 6 min read

Short answer

CODESYS %IW0 names input word 0 of the application's I/O image, not a hex memory address, and there is no conversion between the two. In the documented Machine Expert Modbus TCP slave map (CODESYS 3.5.16), %IW0 lands on holding-register offset 0 and each mapped word is 2 bytes wide, so the client reads function code 03 at offset 0 and proves the map on its target with a test value.

On this page
  1. What %IW0 names in the application
  2. What the client actually puts on the wire
  3. The documented Machine Expert map, and its inversion
  4. Worked example: reading the third word
  5. How to confirm the map on your target
  6. The decision
  7. Questions asked

%IW0 does not hide a hex memory address, and no conversion to one exists anywhere in the protocol: it is input word 0 of the PLC application's I/O image, a slot label that the compiler resolves on the target. What a Modbus TCP client needs is the register offset that the slave declares, and that offset comes from the slave device's own description, not from the % notation. In the documented Machine Expert Modbus TCP slave map (CODESYS 3.5.16), %IW0 is holding-register offset 0 and %IW1 is offset 1, so the client reads function code 03 at offset 0 and receives the value of that word. Prove it on your target with one test value before you trust it: the sections below give the notation, the wire format, a worked example and the decision for each case.

What %IW0 names in the application #

The notation parses from left to right. The percent sign marks a direct address, the letter names a memory area (I for inputs, Q for outputs, M for markers), W sets the width at one 16-bit word, and the trailing number is the position inside that area. %IW0 is therefore the first word of the input image and %IW1 the second. CODESYS shows these addresses in two places: in an AT declaration that binds a named variable to the slot, and in the I/O mapping tab of a device placed in the project tree.

The grammar CODESYS documents for direct addresses, <memory position> : <number> ( .<number> )* // Depends on the target system, carries the deciding clause in its own comment: what a position means is defined per target. A direct address names a slot in an image, not a location in a memory chip, so any table that converts %IW0 into a hex byte address would have to be rewritten for every device. That no universal table exists is proven below, where two documented device descriptions bind the same letters to opposite Modbus areas.

What the client actually puts on the wire #

A Modbus TCP request has no field for a memory address, hex or otherwise. It carries a function code and a 16-bit register offset in the range 0 to 65535 (0x0000 to 0xFFFF), counted from zero: function code 03 reads holding registers, 04 reads input registers, and 06 or 16 writes holding registers. The slave answers from the map its own description declares, and a request whose offset-plus-quantity window runs past the declared area is refused with an exception such as illegal data address rather than answered with zeros. One CODESYS slave that produced its own exception code 0x0002 on such reads, and how that was fixed, is covered in why a CODESYS Modbus TCP server returns exception 0x0002.

The offset convention is the same when the reading side is a CODESYS application acting as client instead of your JavaScript program. That side's setup, including reaching a remote slave through a gateway, is covered in setting up a CODESYS Modbus TCP client to talk through a gateway.

The documented Machine Expert map, and its inversion #

Paraphrased from the Machine Expert documentation (CODESYS 3.5.16): from the master's perspective, the slave's %IW words occupy holding-register offsets 0 through n-1 and are readable and writable by the master, where n is the declared holding-register quantity; the %QW words occupy input-register offsets n through n+m-1 and are read-only, where m is the declared input-register quantity. Each mapped word is 2 bytes wide. Under that map the answer is direct: %IW0 sits at the first holding offset and %IW1 at the second.

CODESYS words (Machine Expert map) Modbus area on the wire Register offsets Master access Width per word
%IW0 through %IW(n-1) Holding registers 0 through n-1 FC03, FC06, FC16 2 bytes
%QW(n) through %QW(n+m-1) Input registers n through n+m-1 FC04 only 2 bytes

Notice what that map does to the mnemonics. The input-area words serve the master-writable holding registers and the output-area words serve the read-only input registers, the opposite of what the letters suggest. The generic CODESYS Modbus slave device binds them the other way round: per the CODESYS online help for its Modbus slave device, holding registers get writable %QW mappings generated instead of read-only %IW mappings. Two documented device descriptions, opposite bindings: the letter never decides the Modbus function code, the device description does.

Worked example: reading the third word #

Take the Machine Expert slave configured with n = 100 holding registers and m = 50 input registers; these are example quantities for the arithmetic, and you read your own from the device. The map is then %IW0 through %IW99 on holding offsets 0 through 99, and %QW100 through %QW149 on input offsets 100 through 149.

  • The value bound at %IW2 is read with function code 03, offset 2, quantity 1. The response's byte count field carries 1 register × 2 bytes = 2 bytes, followed by that register's value, high byte first.
  • The same quantity as 32 bits, held across %IW2 and %IW3 by the program, is read with one request: function code 03, offset 2, quantity 2, byte count 2 × 2 bytes = 4 bytes. Which word carries the high half is the program's decision, not the protocol's.
  • The proof run: bind a spare WORD variable at %IW0, set it online to 4660 (hex 0x1234), request function code 03, offset 0, quantity 1, and expect a byte count of 2 with bytes 0x12 then 0x34. Change the variable to 48879 (hex 0xBEEF) and read again. A read-back that follows both writes proves the map; a read-back that does not tells you this target does not use the Machine Expert map.

One safety note: a holding-register write is live process data, so run the proof on a spare or diagnostic word, or during commissioning in a machine state where a changing word cannot move anything.

How to confirm the map on your target #

  • Open the project and find the Modbus TCP slave device under the Ethernet manager. Its I/O mapping tab lists one row per exposed word, and that tab is the binding that answers on the wire: note which % area the holding-register rows use and where they begin.
  • Note the declared holding-register quantity n, because it bounds every offset your client may legally send.
  • Run the write-and-read-back proof from the worked example, using a value such as 0x1234 so the byte order shows in the response.
  • If the read-back matches, freeze the map into your client as named constants with a comment naming the device and the tab it came from. If it does not, build the client map row by row from the tab and drop the documentation assumption.

The decision #

  • On a CR1203 running a Machine Expert Modbus TCP slave with the documented map: %IWi is holding-register offset i for every i below n. Read %IW0 with function code 03 at offset 0, read the %QW words with function code 04 at offsets n through n+m-1, and confirm once with the 0x1234 read-back before the client goes into service.
  • On a project that places the generic CODESYS Modbus slave device: the holding registers live on the %QW rows, beginning at %QW0, and %IW0 belongs on the slave's read-only side until its tab says otherwise.
  • A raw or hex memory address is not something the protocol lets you derive or send: the request carries only the offset, the notation names only the slot, and the device description is the only thing that joins them.
  • If the read-back failed, rebuild the map from the project's I/O mapping tab and re-run the proof. Do not put a client built on an assumed map into service.

Questions asked

Do I need to add 1 for the 40001-style register numbering?

No. The 4xxxx numbering many client tools display is a documentation convention, not a wire format: the tool adds its display base to the offset, so the first register shows as 40001 while the request carries zero. Send the zero-based offset in the FC03 request; entering the displayed number on the wire is a classic off-by-one.

How many holding registers can one read request cover?

The Modbus protocol limits a single FC03 read to 125 registers, and the response then carries 250 data bytes plus its header. The client must also respect the slave's declared holding-register quantity: an offset-plus-quantity window reaching past it is refused with an exception. Split larger needs into sequential reads.

What byte order does a register value use on the wire?

Each 16-bit register travels big-endian, high byte first, so a test value set as 0x1234 arrives as bytes 0x12 then 0x34. For a 32-bit value spanning two consecutive registers the protocol fixes neither the word pairing nor the word order, so confirm it with the same read-back proof, writing a distinctive word such as 0xBEEF into each candidate register.

Can I bind a named variable to %IW0 instead of using the raw address?

Yes. Declare a variable of type WORD with an AT declaration at %IW0; the name then occupies that direct-address slot, and the Modbus device exposes it through its I/O mapping tab exactly as it exposes the raw address. Keeping the AT declarations in one program block leaves the register map documented in code instead of spread across the project.

Keep reading

  • Troubleshooting

    Why an HMI Modbus TCP write leaves the S7-1200 DB unchanged

    The write most likely never reached the register you think it did: the HMI's 400001 is a one-based display number, while the Modbus request carries a zero-based offset that the server block maps onto DB102.DBW0. Confirm the server block status and data area, confirm station 3 points at the S7-1200 rather than the motion controller, match the driver's register convention, and prove the path with one known value written to a single word.

    Industrial networks

  • Explainer

    Can a Modbus write change PLC outputs?

    Yes, but not by outrunning the scan. On a Modicon CPU the communication interface reaches data memory only between scans, so a write is applied whole and the next scan either consumes it or overwrites it. The real exposure is that Modbus TCP authenticates nothing: any host that can reach the CPU can write an unprotected holding register your logic unpacks into outputs, so pack status into read-only words, keep commands in a separate writable range, and enforce write protection with an IP access control list.

    Industrial networks

  • Troubleshooting

    Fixing CODESYS Modbus TCP server exception 0x0002

    Exception 0x0002 means the CODESYS Modbus TCP server holds no point at the address the client requested. The Data Model start-address fields are offsets within each data type's standard range, so entering 10001 for Discrete Inputs pushes the first bit to reference 20002. Set each start address to 0, confirm the client's notation, then prove one point with a scanner.

    Industrial networks