Read and translate an existing Structured Text program

The Core Idea

Reading and translating a program means preserving its behavior, including instruction order and memory across scans. Similar-looking expressions are not enough if the rewritten program responds differently.

What you will learn

  • Read a program you did not write and predict what it does before you run it.

  • Translate a Ladder routine into Structured Text, rung by rung, keeping its behaviour.

  • Prove the translation with the original routine's own test cases.

Why it matters

Existing machines often come with existing code. Understanding its behavior lets you explain or translate it without silently changing the control rule.

The problem

Most of a controls engineer's time goes into programs someone else wrote: reading them to find out what a machine does, and moving logic between languages when a system is upgraded. This lesson has two parts.

  1. Read it first. A short program with no comments runs two pumps. Work out what it does from the code alone, then run it.

  2. Translate it. The forward/reverse motor routine from Motor control circuits is in Ladder. Write it in Structured Text so it passes the same tests as the original.

The simulator scans every 100 ms.

Read it first

The program below runs two pumps, PUMP_A and PUMP_B, from one input, DEMAND. DEMAND_MEM and LEAD_IS_B start FALSE. Read every line before you answer.

IF DEMAND AND NOT DEMAND_MEM THEN
    LEAD_IS_B := NOT LEAD_IS_B;
END_IF;
DEMAND_MEM := DEMAND;

PUMP_A := DEMAND AND NOT LEAD_IS_B;
PUMP_B := DEMAND AND LEAD_IS_B;

To read code like this, take one statement at a time, and for each one ask what it reads, what it writes, and whether the value it writes lasts into the next scan. DEMAND_MEM copies DEMAND at the end, so the first IF compares this scan with the last one: it is an edge. LEAD_IS_B is only ever flipped, so it alternates. The last two lines just pick a pump.

Translate it

This is the Ladder routine. Each rung seals in one direction, and a normally closed contact of the other direction's output keeps both from running together.

Write the routine in Structured Text below, one assignment per rung. The checks are the Ladder lesson's own test cases. A hidden one presses both buttons in the same scan, where only the rung order decides which direction wins. Editing pauses the program, so press Run to try your version.

Passing the checks records Structured Text progress on reading and translating programs.

What happened and why

A rung and an assignment describe the same thing, so the translation is mechanical once each piece has a name:

LadderStructured Text
Contacts in seriesAND
A parallel branchOR, in parentheses
Normally open contact (XIC)The tag
Normally closed contact (XIO)NOT the tag
Output coil (OTE)The tag on the left of :=
Rung order, top to bottomStatement order, top to bottom

The parentheses matter because AND binds tighter than OR. Without them, FWD_PB OR MOTOR_FWD AND STOP_PB lets the Forward button start the motor while Stop is pressed.

Rung order matters because the forward rung writes MOTOR_FWD before the reverse rung reads it. With both buttons in one scan, forward is decided first and blocks reverse. Swap the statements and reverse wins: every ordinary test still passes, which is why the translation is checked against a test that needs the order.

A translation is finished when it passes the original's tests, not when it looks like the original. The checks here are the Ladder lesson's tests, run on your Structured Text.

Common mistakes

  • Changing the order of the rungs: the program looks the same, but a scan with both buttons now runs the motor in reverse. Keep the statements in rung order.

  • Dropping the seal-in branch: the contactor drops as soon as the button is released. The coil's own contact belongs in the OR.

  • Leaving out the parentheses: AND binds tighter than OR, so the branch no longer passes through Stop or the interlock.

  • Interlocking the other button instead of the other contactor: NOT REV_PB drops forward and starts reverse in the same scan, reversing a running motor without stopping it.

Platform notes

  • Many platforms can show the same logic in more than one language, or convert between them. A converter follows the same rules as this table, but it cannot know which behaviour a test depends on. Run the original's tests on the result.

  • On a real reversing contactor pair, the interlock in the program is backed by a mechanical interlock and by auxiliary contacts wired between the two contactor coils, so a fault in the controller cannot close both.

Finish the unit

This is the last lesson in Debugging and reuse. The unit capstone is the batching conveyor from the applied review, as a ST Studio problem: the same input/output (I/O) list, sequence, and alarm table. Studio has no conveyor model, so its checks switch the photo-eye, the jam sensor, and the overload on and off themselves, and all of them are visible. The card shows Done once every check passes.

Enable JavaScript to run the embedded simulator and complete the interactive exercises.