Consystence
Request a demo
Back to blog
Updated 21 October 2026

C#, not Structured Text: retargeting the PLCnext generator

Why the PLCnext generator moved from Structured Text to the C# runtime used by the simulator, and what the change meant for keeping control behaviour aligned across targets.

Garry Thomas
ArchitectureAutomationEngineering

Update — 21 October 2026. We have corrected this post in place. Our 20 July note said the dam-pump-station program on our workshop’s virtual PLCnext had become a generated one; it was still the hand-written program, and we have removed that claim. A generated eCLR C# program first ran there in September 2026, for our coal-preparation demonstration. A later generated build was loaded there on 18 October 2026 and verified running, including after a restart; as loaded, it used one hand-written sequence helper. No generated program has run on PLCnext hardware yet. Lines below also overstated how the targets are held together. The PLCnext program compiles a copy of the master’s source, which matched the master at a pinned revision when checked on 15 October 2026 and is re-synced by hand; per-block vector replay on PLCnext is planned. No test suite holds the C# blocks to the hand-authored Structured Text they were first ported from: some of their tests trace scenarios from it, but none runs it. That library is also no longer frozen: in October some fixes made in the master were ported to it by hand.

Update — 20 July 2026. One thing has moved since this post. Structured Text is back in the generator’s output — not as the runtime target this post retired, but as a delivery format: for controllers programmed through the vendor’s engineering tool, the pipeline emits the generated program as IEC 61131-3 ST in PLCopen XML, with the hand-authored CSY_* library riding inside it verbatim. The platform’s own runtime path is unchanged — the program the controller runs under eCLR is C# — and the reasoning below stands for it.

Two weeks ago we wrote about generating PLC code instead of writing it, and we owed you a caveat at the end of that post: the generated Structured Text had not yet run on a virtual or physical PLC. Our dam-pump-station proving run had executed a hand-authored, pure-managed C# program on the controller’s eCLR runtime instead. Two artifacts, one honest gap between them.

That gap is now closed — but not the way we expected. We did not get the generated ST running on the controller. We retired the ST target.

What the runtime told us

Phoenix Contact’s PLCnext runs eCLR — a .NET runtime embedded in the controller, alongside the classic IEC 61131-3 environment. That is why our proving run could execute a C# program on a PLC at all, and it is also where our plan met reality.

The plan was tidy on paper: keep one behavioural master for the CSY_* block family in C# — the same blocks the simulator executes — compile it for eCLR, and have the generator emit Structured Text that instantiates those blocks, the way an engineer instantiates a Rockwell AOI. One library, one generated program, two languages meeting in the middle.

They do not meet. A Structured Text program can only instantiate what the runtime registers as a function-block POU, and a C# class compiled for eCLR is not one — it is a plain class, invisible to ST. Making our blocks ST-instantiable would have meant maintaining a second, decorated port of every block: two copies of the behaviour, drifting apart. Two copies of the truth is precisely the disease this whole pipeline exists to cure. We were about to reintroduce it at the deepest layer of the stack.

The decision

So the generator no longer emits Structured Text. It emits the controller program as eCLR C# — a program class that declares the GDS ports the faceplates and historian bind to, instantiates the same CSY blocks the simulator runs, and wires them into the scan. The template expansion is as deterministic as it ever was: the output is a pure function of the request, the type mapping, and the templates. You can diff it, sign it off, and get the same code next time. Nothing probabilistic touched the control code before, and nothing does now.

What changed is what the artifact is. The generated program is now the same species as the one our proving run executed on the virtual PLCnext — a managed program on the controller’s own runtime — rather than a sibling in a different language that had never been to the field. The behavioural master stays in C#: the simulator runs it, and the controller runs a copy compiled from the same source. There is no translation between languages, but that copy is re-synced by hand, and it has drifted from the master before.

The hand-authored IEC 61131-3 CSY_* library — the one with the 323-line pump state machine — is not deleted. It is frozen as the reference implementation: the code the C# master was first ported from, and the artifact a controls engineer can read without touching a line of C#. The C# master, not this library, is the source of truth for the behaviour.

What this buys

One behavioural source now feeds every target. The simulator wires the blocks natively; the PLCnext program calls a copy of the same blocks on eCLR, re-synced by hand; the ControlLogix side draws on the same master through add-on instructions generated or ported from it. When a block’s behaviour changes, it changes in the master first, and the master’s own tests, including the full dam-pump-station scenario, pin it down. ControlLogix blocks are checked against test vectors from the master; the PLCnext copy is checked with live tests on a virtual PLCnext.

To be precise about the line, as always:

  • Shipped: the retargeted generator, emitting the eCLR C# program and its GDS port declarations from the same device-type mapping as before; the ST reference library; the C# master’s behavioural tests, some traced from scenarios in that library when the blocks were ported.
  • Roadmap: a generated program’s own end-to-end field proving. The proving run executed a hand-authored program of the same kind; putting a generated one through the same run is the next box to tick, and we will say so when it is ticked.

We spent weeks building a code path and then argued ourselves out of it, which never feels good on the day. But the pipeline exists to guarantee there is exactly one description of how a pump behaves. The moment the plan required two, the plan was wrong — not the principle.