DEV Community

tia-forge
tia-forge

Posted on

Ladder to SCL conversion: the six things every converter silently drops

Converting ladder to SCL looks like a translation problem. It isn't. The syntax maps almost one-to-one — a contact becomes AND, a coil becomes an assignment — and the resulting program compiles cleanly on the first try.

Then it behaves differently, and the difference only shows up in the field.

That gap is worth naming precisely, because "convert our ladder to SCL" is now a thing people ask AI to do, and the failure mode is not a compile error you can catch in review. Six things get dropped, and every one of them is a behaviour change.

1. Scan order, which was doing real work

Ladder networks execute top to bottom; inside a network, left to right. A coil that appears in two networks is evaluated twice per scan, and the last write wins.

// ladder: network 3 sets Motor, network 7 resets it
IF #ConditionA THEN #Motor := TRUE; END_IF;
IF #ConditionB THEN #Motor := FALSE; END_IF;
Enter fullscreen mode Exit fullscreen mode

This works — as long as the order survives. Reorder the two blocks for readability during cleanup and you've inverted the logic. In ladder, the order was the structure; in SCL it's an accident of text layout that nothing enforces.

What to do: one write point per output. Rewrite the two networks into a single IF / ELSIF chain that computes the value once, and the conversion stops depending on position.

2. Set / Reset coils and their memory

LAD (S) and (R) coils latch across the whole scan; a later network can reset what an earlier one set, and the interleaving is often the point (start/stop with a seal-in).

IF #Start THEN #Motor := TRUE; END_IF;
IF #Stop  THEN #Motor := FALSE; END_IF;   // order decides the winner
Enter fullscreen mode Exit fullscreen mode

A converter that emits these in a different order — or that "optimises" them into one IF — changes which condition wins when both are true in the same scan. That's the case your safety test never exercises and your operators hit every shift change.

What to do: decide explicitly. Either keep the exact order and document that it matters, or rewrite as a state machine where "stop dominates start" is visible in the code.

3. Edge detection had hidden state

A positive-edge contact (P) is not "the input is true". It carries a memory bit per instance — #prev. Convert it to IF #Start THEN … and you've turned a one-shot into a level, and now something fires every scan instead of once.

#Edge := #Start AND NOT #PrevStart;
#PrevStart := #Start;
IF #Edge THEN /* … once per rising edge … */ END_IF;
Enter fullscreen mode Exit fullscreen mode

Three lines, one of which people forget (#PrevStart), which is why converted code often "works" in simulation for a second and then doesn't. Count the edge contacts in the source block before converting — that count is the number of R_TRIG/memory variables the SCL version must declare.

4. Timers: the call must happen every scan, unconditionally

LAD puts a timer box in the network; it is called exactly once per scan, wherever it sits, whatever the rungs upstream are doing.

In SCL, TON is a function block with an instance. Call it inside an IF and it stops timing whenever the condition is false — the timer freezes, and the delay becomes "3 seconds of whatever else was happening".

// correct: call unconditionally, use the outputs afterwards
#TonDelay(IN := #Start, PT := T#3S);
IF #TonDelay.Q THEN /* … */ END_IF;
Enter fullscreen mode Exit fullscreen mode

Same for counters and for any instruction with an instance. This is the single most common defect in machine-translated ladder, and it does not show up in a compile.

5. Branches don't all have a textual shape

Most branches map to OR inside an AND. Two kinds don't:

  • A coil inside a branch. Ladder can energise an output in one leg of a parallel path; SCL has no expression-level assignment. It has to become a separate statement, and then you're back to scan-order semantics (point 1).
  • Nested branches with negation on the branch itself. NOT (A OR B) versus (NOT A) AND (NOT B) — equivalent, but a converter that negates the wrong level produces something that passes casual reading.

What to do: flatten branches bottom-up, and keep the result grouped by network so a reviewer can match one SCL IF to one ladder network. Anything that doesn't flatten cleanly should be flagged, not guessed.

6. The comments, titles and cross-references

Ladder carries maintenance knowledge that isn't logic: network titles that say why, element comments on a contact named M47.2 that explain what it really is, and cross-references that tell you what else touches the output.

No converter preserves these by default, and the resulting SCL is technically equivalent and operationally worse. The person at 3 a.m. now reads IF #bFault2 THEN with no idea which sensor that is.

What to do: carry network titles into block comments, carry element comments into inline comments, and keep the tag names symbolic. If the source uses absolute addresses, converting to SCL is the moment to fix that — not to freeze it in place.

And the parts that don't convert at all

Worth saying out loud, because tools that promise "any ladder to SCL" are lying by omission:

  • STL / GRAPH / FBD — different representations, not different syntax. GRAPH in particular has step/transition semantics with no textual equivalent.
  • Indirect addressing (pointer/register-indirect, M[#index] patterns) — mechanically translatable only if you know the original's bounds, which the ladder never had to state.
  • Jump/label pairs — JMP skips code, so outputs downstream hold their previous value. In SCL that "hold" has to be modelled explicitly.
  • Blocks that mix languages per network — half of them convert, and the half that doesn't is the interesting half.

The check that makes conversion trustworthy

Do not review converted SCL by reading it. Run both versions.

  1. Extract the source block, convert it, keep both.
  2. Write three scenarios: normal cycle, one sensor stuck, both commands true simultaneously.
  3. Step both implementations scan by scan against the same input sequence, and diff every output, every scan.
  4. The first scan where they diverge points at the network that was mistranslated — usually one of the six above.

That's a mechanical, repeatable check, and it's the reason we run a cloud simulator alongside the renderer: conversion without equivalence testing is a code review with no test suite. On a machine with a real cycle time, "it compiled" has never been the claim that matters.


Disclosure: I work on the tooling below; the conversion failure modes are ours to own and Openness/SimaticML are Siemens'. LadDoc Cloud — ladder IR → validated SimaticML. LAD Render Cloud — SimaticML → PNG, to compare the two versions visually. PLC Cloud Sim — step-run LAD and SCL against the same scenarios and diff the outputs. 100 free calls each; TiaLink for the on-premises project side.

Top comments (0)