How-to
Replacing OnValueChange in a CODESYS Modbus TCP write channel
Short answer
The CODESYS Modbus configurator has no OnValueChange equivalent. Set the write channel's trigger to Rising Edge, drive its BOOL from the operator's commit event, clear the BOOL when the channel reports completion, and give each HMI a cyclic read channel into a separate read-back variable, so an idle master transmits nothing and cannot rewrite a stale value over the other HMI's change.

On this page
e!Cockpit's Modbus configurator offered a WriteOnly channel with an OnValueChange trigger, and that combination let two HMI masters share one register on the controller without overwriting each other. The CODESYS Modbus configurator has no OnValueChange option to select — the feature is not there. The working replacement is the Rising Edge trigger: bind the write channel to a BOOL that the operator's commit action raises, clear that BOOL once the channel reports completion, and give each HMI a cyclic read channel into its own read-back variable. An idle master then transmits no write frames at all on TCP port 502, so it cannot rewrite a stale local copy over the other HMI's value, and you keep Modbus TCP for the legacy equipment without touching the controller program.
Why two Modbus TCP masters overwrite one register #
The classic case is an e!Cockpit conversion: two TP600 panels acting as Modbus TCP masters, one PFC200 as the slave, one setpoint register both panels must read and write. The Modbus application protocol defines no register ownership, no session lock and no write-permission concept; nothing in the specification lets a master claim a register.
The path a write takes is short: HMI application, the panel's Modbus TCP client channel, the switch, TCP port 502 on the slave, the register map, and the slave applies whichever write arrives last. A cyclic write channel transmits the master's local copy on every poll, whether or not an operator touched it. If the operator on panel A commits a new setpoint while panel B's channel is still cycling B's old local copy, B writes the old value back within one poll period. Frames are delivered and acknowledged; nothing on the network failed, the data is simply stale.
Write-on-change removes the mechanism itself, because an idle master generates no write traffic and so has nothing to rewrite with.
Confirm first that nothing else on that network claims TCP port 502. A second service on that port produces the failure described in why a custom Modbus TCP slave fails on port 502, which looks similar and is not this problem.
What the Rising Edge trigger gives you #
The CODESYS Modbus configurator exposes each channel you define as a function block with the usual interface: xExecute on the input side, xError and errorCode on the output side. The channel trigger setting offers Rising Edge; there is no OnValueChange entry to pick. Bound to a BOOL, the channel transmits its data block once per rising edge of that BOOL instead of on every cycle. On the wire that is the same behavior OnValueChange produced: one write per operator action, zero writes while idle. The extra work lands in your application under two rules: raise and clear the trigger BOOL yourself, and hold the request until the channel reports completion instead of pulsing it for one scan.
Build the replacement on each HMI #
The variable names below are examples, not required names: xWriteTrig (BOOL) for the trigger, wSetpointCmd (WORD) for the value the operator enters, wSetpointRb (WORD) for the read-back, xEditing (BOOL) for the input-field focus flag.
- Keep the slave entry in each panel's Modbus TCP master as it is: same IP address and the same port as before. Both panels point at the one slave.
- Add a cyclic read channel covering the shared register block and map it to wSetpointRb, declared separately on each panel. Both displays then track the slave no matter which master wrote last.
- Add the write channel over the same register and set its channel trigger to Rising Edge, bound to xWriteTrig.
- Map the write channel's data to wSetpointCmd. Never bind one variable to both channels: a variable serving read and write turns every read-back into a rewrite request waiting to happen.
- Raise xWriteTrig from the visualization's commit event, the input field's confirm action or the button press. Do not derive it from comparing wSetpointCmd with wSetpointRb: that comparison goes true whenever the other panel changes the register, and your panel would then write back the value it just read.
- Hold xWriteTrig TRUE until the channel reports completion, then drop it so the next commit makes a fresh edge. A one-scan pulse can fall between task cycles and the channel never sees it. xError FALSE means the transaction completed; when xError is TRUE, read errorCode for the exception the slave returned.
- While xEditing is FALSE, copy wSetpointRb into wSetpointCmd so the local command copy always matches the slave, and gate that copy with xEditing so the read-back does not fight the operator's typing.
Step 7 is the half that prevents the stale write. With the command copy tracking the slave whenever nobody is editing, even an unexpected edge has no old value to carry onto the wire.
One safety note: if that register commands machine motion, edge-triggered writes stop accidental overwrites but not simultaneous commits. Two operators inside the same poll window still resolve to last write wins, so give each panel its own command register and arbitrate the choice in the controller program; the HMI pattern is not a safeguard for that case.
Worked example: what the change removes from the wire #
Example settings, not measured values: controller task period 10 ms, cyclic read period 250 ms, the shared setpoint written as a single register so a commit is one frame.
With the old cyclic write behavior at that same cadence, each idle panel sends 60000 ms / 250 ms = 240 write frames per minute, so two panels put 240 + 240 = 480 write frames per minute on the link, and every one of them can clobber the other panel's value. With the Rising Edge trigger the idle rate is 0 write frames per minute; an operator who commits ten setpoints in a shift produces ten frames in total. Display agreement still costs one read period: a commit landing just after panel B's poll leaves B showing the old value until its next read, one read period plus one task cycle, 250 ms + 10 ms = 260 ms at worst, before B's display follows. Halve the read period if that lag matters to operators; the read traffic doubles with it.
Faults this pattern still throws #
| Symptom | Mechanism | Check |
|---|---|---|
| Write fires once, then never again | The trigger BOOL is never cleared, so no second edge exists | Watch xWriteTrig online; it must return to FALSE between commits |
| Write skips intermittently | The pulse is shorter than the task cycle that services the channel | Hold the request until the channel reports completion |
| Value flips back moments after a commit | Another master still runs a cyclic write channel, or one variable is bound to both directions | Confirm every write channel on every master is edge-triggered and that the read and write variables are separate |
| The field jumps under the operator's typing | The read-back copy runs while the input field has focus | Gate the copy with xEditing |
| Two commits land in the same poll window | Both frames reach the slave and the last one wins, silently | Move to per-panel command registers arbitrated in the controller program |
A legacy Modbus master already writing that register cyclically is outside your protection: edge-triggered panels cannot stop a third cyclic writer, so check every master's poll configuration on the shared block.
Prove it with a port capture #
Mirror the switch port or tap the link to panel B and filter the capture to TCP port 502, then walk it in order:
- With nobody touching panel B, confirm only read requests leave B, function code 3, and no function code 6 or function code 16 frames.
- Commit a value on panel A and confirm exactly one write frame from A's IP address, carrying the new value.
- Confirm B's read channel returns that value within one read period and B's display updates without B sending any write.
- Commit a different value on panel B, confirm one write frame from B, then confirm A's display follows through A's read channel.
- Leave both panels idle for several minutes and confirm the register holds and the capture shows no write frames from either master.
If a write frame leaves and the register still does not change, the write is landing in the wrong place, the same failure shape as why an HMI Modbus TCP write leaves the S7-1200 DB unchanged. Check the register offset and the slave's access rights before blaming the trigger logic.
The decision for each case #
- Converting an e!Cockpit project that is in service, keeping Modbus TCP and leaving the controller program untouched: the Rising Edge write trigger plus a cyclic read channel into a separate read-back variable on both panels. That is the replacement, and it reproduces OnValueChange on the wire.
- Two operators who can realistically commit inside one poll window, or a register that drives motion: separate command registers per panel with the arbitration in the controller program. The shared-register pattern cannot arbitrate between people.
- A new project with no legacy Modbus equipment to serve: the Datasource Manager, which browses the controller application as an OPC UA client once the controller runs the FW26 firmware generation, replaces the Modbus link entirely. It does not retain Modbus TCP for legacy masters, so it is not the answer for this conversion.
Questions asked
Does the same trigger pattern work on a Modbus RTU master channel?
Yes. The trigger types and the edge logic are identical; the difference is the serial port. Reads and writes share one RTU port, so a long cyclic read list delays the write behind it in the queue. Keep the read list short and the commit path light, or the edge-triggered write follows the operator late instead of promptly.
Does the controller slave need any change for this pattern?
No. The slave keeps its register map and keeps answering on its Modbus TCP port; the whole change lives in the masters' channel configuration. The frames look the same to the slave, and the only difference is timing: writes arrive when an operator commits instead of on every poll, so the slave's write counters go quiet between operator actions.
What if one operator action must write several registers?
Keep the tags that one commit must move in one contiguous register block, so the channel sends them as a single write request with function code 16. Tags scattered across the register map need separate channels and separate trigger BOOLs raised by the same commit event; otherwise the slave holds a part-updated set between frames.
Can I keep cyclic writes and just lengthen the poll period?
No. A longer period only widens the window in which the stale local copy sits; the idle master still transmits eventually and still restores the old value. The replacement works because an idle master transmits never, not rarely. Stretching the period trades how often the overwrite happens for how long the wrong value stands, which is the worse trade on a machine.


