IMDHMIControls LibraryShop main store

Troubleshooting

Why a virtual PLC Modbus TCP write fails before the server

Industrial networks Published 7 min read

Short answer

The write stops inside the TwinCAT target, not at LabVIEW: FB_MBWriteRegs needs the TF6250 Modbus TCP function installed and licensed on the executing target, and a library reference does not install it. Install that runtime first, then aim the block at 127.0.0.1 on the registered Modbus port with the register offset the server actually serves.

On this page
  1. Install the runtime the library does not bring
  2. Why the address trials all failed the same way
  3. The unit ID and register start that match
  4. One transaction, watched end to end
  5. The decision for each stopping point
  6. Questions asked

Your LabVIEW master, its slave and the Python client all work, so the server, its listener and the loopback path are proven. The break is on the TwinCAT side, and the first suspect is not an address: FB_MBWriteRegs needs the TF6250 Modbus TCP function installed and licensed on the target that executes the program — here the virtual PLC on your engineering PC. Referencing the Modbus library in the project makes the call compile; it does not install the runtime. With the runtime absent, the block reports an error on its first active cycle and never opens a TCP connection, which is exactly why none of the addresses, unit IDs or register starts you tried changed anything. Work the checks below in order and the stopping point names itself.

Install the runtime the library does not bring #

TF6250 is the TwinCAT 3 Modbus TCP function: it gives the runtime both a Modbus TCP client and a server, acting as the gateway between Modbus devices and the TwinCAT runtime. Your write lives in that function, not in the PLC project. Adding Tc2_ModbusSrv to the project supplies the function-block interfaces you call; the executing target still needs the function itself, installed on that target with:

tcpkg install TF6250.ModbusTCP.XAR

For a virtual PLC the target is the engineering PC, so the command runs there. Complete Beckhoff's documented licensing for the target afterwards — the rule is general and worth saying once: the licensed runtime component must sit on whatever executes the code, not on the machine that compiled it. It is the same reason a VeriStand deployment requires its licensed system installed before anything runs.

TF6250 also covers the server role, so if other masters must later read this PLC, that side is its own job; the checklist in what you must configure on the PLC side for Modbus TCP applies to it.

A missing runtime announces itself with a fingerprint you can read in one PLC cycle:

  • The block is called, bExecute rises, and an error appears on the block's error output within a cycle or two.
  • A capture on the loopback interface shows no connection attempt during that time, because there is nothing on the wire.
  • Every later trial, whatever address, unit ID or register start you enter, fails identically and equally fast.

When every external client connects and the TwinCAT block errors with no traffic, install TF6250 before touching another field.

Why the address trials all failed the same way #

The destinations tried were 'localhost', 192.168.88.1 and 192.168.88.126; the unit IDs 0, 1 and 16#FF; the register starts 16#3000, 0 and 1. Every one of those fields is consumed after the runtime provides the communication service, so with TF6250 absent all the combinations fail the same way. That uniform failure is itself the diagnostic: a genuine endpoint problem fails differently per address.

With the runtime in place, the loopback geometry is the next thing to get right. Both programs run on one computer, so the connection never touches the network card — the same-host pattern behind how to run two TwinCAT PLC programs on one laptop, and it works for any two runtimes on one PC as long as each endpoint binds an address the other can reach. Two consequences follow:

  • The LabVIEW slave set to 'localhost' listens on the loopback address of its own host, and a listener bound to loopback does not accept connections addressed to 192.168.88.1 or 192.168.88.126. Those two trials could only ever be refused.
  • sIPAddr carries an IP address string, so give it the address itself rather than a name another program resolved for you. For the same-host test that means '127.0.0.1', with nTCPPort := 502 — the port registered for Modbus.

The unit ID and register start that match #

The unit identifier is a Modbus application field, not part of the TCP addressing, so it cannot cause a connection failure at all — a wrong value can only surface as an exception response after a connection exists. For a direct Modbus TCP server, Beckhoff's documentation gives 16#FF, decimal 255; servers sitting directly on TCP generally ignore the field or accept any value, while a serial gateway uses it to select the downstream slave. Keep 16#FF and spend no more time on it.

The register start is where real mismatches live, so work it through:

  • nMBAddr is a protocol offset counted from zero. The trial value 16#3000 is hexadecimal: 3 × 4096 = 12288 decimal.
  • The 4xxxx reference a LabVIEW display shows is the offset plus 40001, so offset 0 is register 40001 and the trial's offset of 12288 aims at reference 52289.
  • If the LabVIEW master writes the first register its display shows, the matching request is nMBAddr := 0. A request outside the server's mapped range comes back as exception 02, illegal data address — proof the transport is healthy and the map is wrong.

Confirm which convention the LabVIEW front panel shows before converting anything; the two differ by exactly 40001, and every migration stumbles on that once.

Your buffer assignments were right all along, and the arithmetic shows it: nValue is ARRAY[0..3] OF WORD, so four 16-bit registers occupy 4 × 2 bytes = 8 bytes. SIZEOF(nValue) therefore returns 8, which is what cbLength asks for in bytes, and nQuantity := 4 describes the same 8 bytes in registers. One caution: pSrcAddr := ADR(nValue) sends nValue, not the separately declared M0 AT %MB0, so do not watch %MB0 expecting movement.

T#5S is a generous timeout — a healthy loopback write finishes in milliseconds — so leave it alone; a request that consumes the whole timeout means the path dropped the connection attempt, not that the server is slow.

One transaction, watched end to end #

The example program increments its four values and immediately starts the next write, and it services the block only inside conditional branches. That makes one attempt hard to correlate with anything LabVIEW shows. For the diagnostic run, use one call site, a frozen data set and a recognisable test pattern:

PROGRAM MAIN
VAR
    fbWriteRegs : FB_MBWriteRegs;
    bSend       : BOOL := FALSE;                          // one rising edge per test
    nValue      : ARRAY[0..3] OF WORD := [1000, 1001, 1002, 1003];
END_VAR

fbWriteRegs.sIPAddr   := '127.0.0.1';
fbWriteRegs.nTCPPort  := 502;
fbWriteRegs.nUnitID   := 16#FF;
fbWriteRegs.nMBAddr   := 0;
fbWriteRegs.nQuantity := 4;
fbWriteRegs.cbLength  := SIZEOF(nValue);
fbWriteRegs.pSrcAddr  := ADR(nValue);
fbWriteRegs.tTimeout  := T#5S;

fbWriteRegs(bExecute := bSend);   // unconditional call, every PLC cycle
  • Hold the inputs steady, give bSend one rising edge, and leave bExecute high until the transaction ends; reissue only after the previous request completed and its result is recorded.
  • Watch bBUSY and the error output as they change, and write down the error identifier before another transaction can replace it. That identifier is the evidence; an unchanged LabVIEW display is not.
  • When the server is later a running machine's PLC instead of the LabVIEW example, aim the first write tests at a range that drives nothing.

The proof of success is the read-back: the working Python client or a read block must return 1000, 1001, 1002, 1003 from the four registers. Now read the stopping point off the wire and the block:

What you observe What it means Next check
Error on the block, no connection attempt captured Runtime missing or unlicensed, or the block never serviced TF6250 on the executing target, target in run, one call site
Connection refused Nothing accepting at that address and port LabVIEW slave running, its binding, who owns the port
Connection opens, exception response returns Transport fine; the server rejected the request Unit ID and register range
Write acknowledged, displayed value unchanged Wrong register, wrong view, or a later write overwrote it Read the same range back

Capture on the interface that carries the traffic: same-host loopback frames do not appear on the Ethernet adapter's capture in most tools, so select the loopback interface the capture tool offers.

The decision for each stopping point #

  • The block errors with no connection attempt on the wire: TF6250 is missing or unlicensed on the executing target. Install it with tcpkg install TF6250.ModbusTCP.XAR, complete the documented licensing, and change nothing else yet.
  • The connection to the loopback address is refused: nothing is listening there. Start the LabVIEW slave, confirm it bound the loopback address rather than a physical one, and confirm no other process owns the Modbus port on that host.
  • The connection opens and an exception comes back: the transport is proven, so fix the fields — keep 16#FF for the direct server and set nMBAddr to the offset of the register the LabVIEW master actually writes, 0 for the first displayed one, with all four registers writable.
  • The write is acknowledged but the display does not move: you wrote a different register than the one displayed, or the next transaction overwrote it before you looked. Freeze nValue, write once, and read the same range back before firing another write.

On this evidence — every external client connects while the block errors with no traffic — the fix is the first item: TF6250 installed and licensed on the virtual PLC's target, then '127.0.0.1' as the destination.

Questions asked

Can another program on the same PC already own port 502?

Yes, and it is a common loopback trap: only one listener can bind a given address and port, so a leftover simulator, an embedded server inside other software, or a second LabVIEW instance can hold the loopback listener while your intended server binds elsewhere or fails quietly. List the listening sockets with the operating system's tool and confirm which process owns the Modbus port before changing any client field; if it is the wrong process, stop it or move your server to another port and set nTCPPort to match.

What changes when the test moves from the virtual PLC to a hardware controller?

Two things. The TF6250 license belongs to the executing target, so the controller needs its own installation and license before the unchanged program runs there. And 127.0.0.1 stops meaning the server the moment client and server sit on different hosts: put the server's real address into sIPAddr and allow the controller through the host firewall on the Modbus port.

Does TF6250 also let the TwinCAT PLC act as a Modbus server?

Yes. The function provides both roles and acts as the gateway between Modbus TCP devices and the TwinCAT runtime, so the same installation can serve other masters later. The server side is configured separately, with a tool that maps PLC memory areas to Modbus addresses, and that mapping is its own task; nothing about the client write prepares it.

Why does the Python client succeed when the TwinCAT block cannot?

Python opens its own TCP socket straight to the listener and depends on nothing from TwinCAT, so it proves the server, its port and the loopback path, but nothing about the TwinCAT execution environment where the missing runtime sits. Once TF6250 runs, the Python client stays useful: copy the unit ID and register numbers it sends, because those are the application fields the server has already accepted.

Keep reading

  • 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

  • How-to

    Safe Modbus writes to PLC output registers from SCADA

    A Modbus write can command physical outputs whenever PLC logic copies the written word to output commands, so never map one word as both status and command. Keep a PLC-written status word that clients only read, route remote control through a separate request register validated against permissives each scan, and verify the map before go-live.

    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