IMDHMIControls LibraryShop main store

Troubleshooting

What to do when a Modbus TCP server lacks FC22 support

Industrial networks Published 8 min read

Short answer

When a SCADA client sends FC22 (Mask Write Register) to a server that does not implement it, the reply is exception 01, Illegal Function. A compiled server library cannot be edited to add the function, so the fix is client-side: map the points as coils, write whole registers, or use FC23, and give every shared register a single writer.

On this page
  1. Why does the client's write fail with exception 01?
  2. Which function codes does the server actually answer?
  3. Can a compiled server library be modified to add FC22?
  4. What should the client send instead of FC22?
  5. How does a lost update happen without FC22?
  6. Should the shared bits move into the PLC program instead?
  7. Is writing a custom FC22 server ever justified?
  8. How do you confirm the fix on the wire?
  9. Questions asked

Fix it on the client side, not in the server. A compiled Modbus TCP server library whose documented list runs FC01 through FC06, FC15, FC16, FC23 and FC43/14 — with FC22 absent — answers a Mask Write Register request with exception 01, Illegal Function, and no edit to the compiled code changes that. The working fixes are to map the points as coils, write whole registers from a client-held image, or use FC23, and to give any register with shared bits a single writer.

Why does the client's write fail with exception 01? #

Because the server does not implement the function the client sent: exception 01, Illegal Function, is the reply a compliant server owes any request whose function is not on its list, and for FC22 the response byte is 0x96 — 0x16 with the high bit set — followed by the exception code.

Confirm that before changing anything. Capture at the client with a display filter of tcp.port == 502; the function code is the byte immediately after the 7-byte MBAP header. A request byte of 0x16 answered by exception 01 closes the case: the client is sending Mask Write Register and the server is replying exactly as its documentation implies.

The other outcomes point elsewhere:

  • A request byte of 0x03, 0x06, 0x10 or 0x17 means the function is supported and the fault is addressing or mapping — re-check the register offset convention, 0-based versus 1-based, in the client's device settings.
  • A read that lands outside the server's map returns exception 02 instead, the failure mode traced in Modbus exception 02 when a read crosses a register map gap.
  • No response, or a TCP reset, is a link, IP, port or server-configuration problem that sits below function codes.

Keep the wire apart from the runtime's own diagnostics: the error a CODESYS Modbus TCP server logs on its function block is a separate numbering, worked through in fixing CODESYS Modbus TCP server exception 0x0002.

Which function codes does the server actually answer? #

For the library in this case — referenced as IL_ModbusTCPServerType02 in the project — the documentation lists FC01, FC02, FC03, FC04, FC05, FC06, FC15, FC16, FC23 and FC43/14, and FC22 is the one write function missing from that list.

Function Name Documented
FC01 Read Coils Yes
FC02 Read Discrete Inputs Yes
FC03 Read Holding Registers Yes
FC04 Read Input Registers Yes
FC05 Write Single Coil Yes
FC06 Write Single Register Yes
FC15 Write Multiple Coils Yes
FC16 Write Multiple Registers Yes
FC22 Mask Write Register No
FC23 Read/Write Multiple Registers Yes
FC43/14 Read Device Identification Yes

Two qualifications keep the table honest. The list belongs to this library and version: read it from the Library Manager of your own project before quoting it anywhere, because another Modbus stack on the same platform can document a shorter list — one CODESYS Modbus/TCP implementation documents only FC03, FC04, FC06, FC16 and FC23. And the server does not "use" FC23 to the exclusion of anything: it answers every code on the list, and the client decides which one goes on the wire.

Can a compiled server library be modified to add FC22? #

No — the library ships compiled, so its code cannot be opened, viewed or edited in the engineering environment, and no parameter exists that would switch a function code on, which makes "modify the library" a dead end rather than a hard option.

That leaves three real routes: change what the client sends, change how the register map is organized, or replace the server with one you write yourself. The first two are an afternoon of configuration; the third is a code base you maintain for the life of the machine. The premise behind the edit request is also backwards — the server never chooses between FC22 and FC23. Function selection happens in the client, driven by tag configuration, which is why the fix lands there.

What should the client send instead of FC22? #

Coils and whole words: map every bit that stands alone as a coil (FC05 or FC15), write multi-bit words whole with FC06 or FC16, and use FC23 when one transaction must both read and write — every one of those codes is on the supported list.

What the point is Client configuration Function on the wire
Independent command or flag Address it as a coil and write it FC05 / FC15
Word written only by the client Keep a client-held image of the word; write it whole FC06 / FC16
Bits with several writers Split into one word or coil per writer FC05 / FC06 / FC15 / FC16
Read and write in one transaction Read/write multiple registers FC23

Which code the driver emits is set by the tag's address type and write mode in the client, not by anything on the server, so verify after remapping that the intended code actually appears in the capture. Note the limit of the last row: FC23 writes whole registers and applies no mask, so it removes the extra round trip, not the overwrite risk — which is why the shared-bits row matters more than the function choice.

How does a lost update happen without FC22? #

Because the mask moves to the client: FC22 applies its two masks inside the server in one indivisible step, while an FC03-then-FC06 sequence leaves a two-round-trip window in which anything the PLC wrote to that register is overwritten.

Section 6.16 of the MODBUS Application Specification V1.1b defines the operation as Result = (Current AND And_Mask) OR (Or_Mask AND NOT And_Mask): a 1 in And_Mask keeps the current bit, a 0 lets Or_Mask force that bit. Worked example — register 40011 (address 10, zero-based) is a shared command word holding 0x0024, which is 36 decimal with bits 2 and 5 set, and the client must set bit 3 alone:

  • With FC22, And_Mask 0xFFF7 and Or_Mask 0x0008: 0x0024 AND 0xFFF7 = 0x0024, because bit 3 was already 0; NOT 0xFFF7 = 0x0008; 0x0008 AND 0x0008 = 0x0008; 0x0024 OR 0x0008 = 0x002C, which is 44. Bits 2 and 5 are untouched, and no other writer can interleave.
  • Emulated in the client with FC03 then FC06: the client reads 36. The PLC clears bit 2 and sets bit 7 during the gap, taking the register to 0x00A0, which is 160. The client writes its computed 44 (0x002C). The register ends at 44 instead of 168 — the client's stale base of 36 plus its own bit of 8 — and the PLC's +128 and −4 never arrive.

The decision follows the arithmetic: a client-side read-modify-write is sound only when the PLC never writes that register, or when every bit in it has exactly one owner. Anywhere else, restructure the map until no register has two writers.

Should the shared bits move into the PLC program instead? #

Yes, whenever a word must carry bits from more than one writer: make the Modbus-visible word a pure command word written whole by the client, decode it with edge detection in the PLC, and give the PLC a separate status word that the client only reads.

Edge detection is the whole trick. Compare the command word against its previous-scan value and act on transitions, so a repeated or stale write from the client cannot re-trigger the action. Keep the decoded bits behind the machine's permissive and interlock logic rather than letting a raw network word command outputs directly — the same discipline behind safe Modbus writes to PLC output registers from SCADA. The cost is a few registers and a comparison rung; the return is that no register ever again needs an atomic masked write.

Is writing a custom FC22 server ever justified? #

Only when the client cannot be reconfigured and the map cannot be split: a custom server is a code base you own permanently, and it has to take over the port entirely, because one listener at a time can bind TCP port 502.

Built on the controller's TCP socket library, it must parse the MBAP header, answer every function the client uses — not FC22 alone — and handle unit ID, exceptions, connection limits and timeouts. Implement the mask as a single uninterrupted step against the same register array the PLC logic uses, and answer with the echo response the specification requires. If the client-side and mapping routes have genuinely failed, this is the remaining one; in every other case it is over-engineering.

How do you confirm the fix on the wire? #

With a capture and a live view: trigger the client's write with the capture running, confirm the request's function byte is one from the supported list and the response carries no exception, then verify in the controller that the intended variable actually changed.

  1. Record the runtime, PLC application and library versions in use, so the capture can be tied to one configuration.
  2. Capture with the display filter tcp.port == 502 and trigger the write to the affected point.
  3. Read the function byte after the MBAP header and confirm it is the code you planned — 05, 06, 15, 16 or 23.
  4. Confirm the response function byte carries no high bit and no exception code follows it.
  5. Watch the target word in the controller's online view: only the intended bits change and the neighbours hold.
  6. On a shared word, toggle a PLC-owned bit during a client write and confirm neither value is lost.

Questions asked

Is FC22 required by the Modbus specification?

No. Section 6.16 of the MODBUS Application Specification V1.1b defines the function's message format, but which functions a server implements is a per-device fact stated in its own documentation. Treat FC22 as an option you verify in the server's list, never as a baseline you assume, and configure the client's write path around the list you actually have.

Where does the client decide which function code to send?

In the tag and driver configuration, not on the server. The address type does most of it: a point addressed as a coil is written with FC05 or FC15, and a holding register with FC06 or FC16. After remapping, re-check the offset convention, 0-based versus 1-based, before treating a failed write as a function-support problem.

What is inside an FC22 request and response?

The request carries a 16-bit reference address, a 16-bit And_Mask and a 16-bit Or_Mask, six bytes in total. The response echoes those three fields unchanged, which is why a custom implementation must build that echo itself rather than reuse a generic write response.

Does the same fix apply over Modbus RTU?

Yes. The function and its masks are defined at the application layer, so the PDU is identical over serial and TCP; only the framing differs, and the 7-byte MBAP header exists on TCP alone. If the serial server's documented list also omits FC22, the same client-side restructure applies unchanged.

Which exception code means a bad address rather than a bad function?

Exception 02, Illegal Data Address. Exception 01 means the function itself is unsupported, 02 means the function is supported but the address falls outside the server's map, and 03 means a bad value in the request such as an invalid count. Read the code from the response before changing configuration.

Keep reading

  • How-to

    Connecting third-party Modbus TCP devices to a Modicon MC80

    Yes. A Modicon MC80 under Control Expert Classic exchanges data with any third-party Modbus TCP device through explicit READ_VAR and WRITE_VAR calls plus the device's register map — there is no ESD or EDS import, no IO Scanning and no DTM support. Run the MC80 as the TCP client, keep the exchange in the coil and holding-register tables, and latch each result in the reader so a 16 ms update survives a slower PLC poll.

    Industrial networks

  • How-to

    How to build a virtual PLC test bench for protocol practice

    One laptop runs the whole bench: a Modbus TCP server simulator, a master test client, a Node-RED OPC UA server and a SCADA trial connect over loopback, so you can practice addressing, sessions, scaling, alarms and fault diagnosis without a PLC. Work through three bounded projects and finish with seven pass criteria you can defend.

    Industrial networks

  • How-to

    Linking Modbus RTU BMS SCADA to a redundant S7-1500R

    Use one Modbus RTU-to-TCP gateway as the Modbus TCP client, and run the Modbus TCP server on the S7-1500R system IP address that switched communication moves to the surviving CPU. The BMS keeps polling its RS-485 link unchanged; on a switchover the TCP session drops and the gateway reconnects. Set the comm-fail timeout to the measured gap plus one poll interval: 3 s at a 1 s rate in the example.

    Industrial networks