Skip to content

Core Concepts

This page covers the vocabulary you need to write and understand a pyrung program. For engine internals and architecture, see Architecture.

Scans

A PLC doesn't run code line by line. It runs in scans. Each scan works through every rung in order, first to last. On each rung, conditions are checked, then instructions execute. Once every rung has been evaluated, that's one scan.

runner.step()           # Execute one scan
runner.run(cycles=100)  # Execute 100 scans

Tags

A tag is a named, typed value. Think of it as a PLC address with a human-readable name.

from pyrung import Bool, Int, Real, Char

Button  = Bool()             # 1 bit, resets on STOP→RUN
Step    = Int()              # 16-bit signed, retentive
Temp    = Real()             # float, retentive
State   = Char()             # one Windows-1252 character, retentive

Tags don't hold values themselves. They're references. Values live in the system state and update each scan.

Constructor Size Retentive by default
Bool 1 bit No
Int 16-bit signed Yes
Dint 32-bit signed Yes
Real float, ±3.4028235E+38 (64-bit precision in pyrung) Yes
Word 16-bit unsigned Yes
Char 1 byte, one Windows-1252 character Yes

Retentive means the value survives a STOP→RUN transition. Non-retentive tags reset to their defaults.

Rungs

A rung is a with block. The condition goes on the rung, and the instructions go in the body. If the condition is true, the instructions execute. If false, instructions that depend on the live power rail are turned off.

with rung(Button):
    latch(MotorRunning)

This reads like a ladder diagram. Button is the contact on the left rail, and latch(MotorRunning) is the coil on the right. If Button is true, MotorRunning gets latched.

Conditions can be combined and compared:

with rung(Button, ~EStop):        # AND + NOT
    latch(MotorRunning)

with rung(Temp > 150.0):          # Comparison
    out(OverTempAlarm)

with rung(State == "g"):          # Equality
    on_delay(GreenTimer, preset=3000)

Branches

A branch creates a parallel condition within a rung, like a parallel path on a ladder diagram. It ANDs its own condition with the rung's:

with rung(Running):
    out(Motor)
    with branch(HighSpeed):
        out(FastLamp)  # On when Running AND HighSpeed

Evaluation order

A rung is commonly said to be evaluated left to right, top to bottom. It's a little more nuanced than that. Instructions run in the order written. The rung's own condition is read once and holds for everything below it:

with rung(A):
    reset(A)
    out(Y)      # Still on this scan: A was already read

A branch reads its own contacts when execution reaches it, so it sees what the instructions above it in the same rung just wrote. See Branching.

Instructions

Instructions are what go inside a rung. Here are the ones you'll use most often:

out(Light)                    # Energize while rung is true, de-energize when false
latch(Motor)                  # Set and hold — stays true even if rung goes false
reset(Motor)                  # Clear a latched tag

copy("g", State)              # Copy a value into a tag
math(Step + 1, Step)          # Evaluate an expression, store the result

on_delay(MyTimer, preset=3000)               # Timer: accumulate while rung is true
count_up(MyCounter, preset=100)              # Counter: one count per rising edge

out vs latch: out follows the rung. The output is true when the rung is true and false when the rung is false. latch is sticky. Once set, it stays set until explicitly reset.

The full instruction set (branching, subroutines, shift registers, edge detection, and more) is in the Instruction Reference.

Timers and counters

Timer and Counter are built-in structured types. Each has a .Done bit and an .Acc accumulator. Use Timer.clone("Name") for named instances:

from pyrung import Timer, Counter

GreenTimer = Timer.clone("GreenTimer")

with rung(State == "g"):
    on_delay(GreenTimer, preset=3000)  # 3000 ms (default unit)

The accumulator tracks progress in the unit you specify (milliseconds by default). If the rung goes false before the preset, the accumulator resets (that's on_delay). Use off_delay for the inverse behavior.

Counters count rising edges. Each time the rung goes True, the count goes up by one, however long the rung stays True:

PartCounter = Counter.clone("PartCounter")

with rung(Sensor):
    count_up(PartCounter, preset=9999).reset(CountReset)

Instruction pins

Some instructions have extra condition inputs beyond the rung, like the .reset() on the counter example above. These are pins on the instruction block.

                    ┌─────────────────┐
 Sensor ───────────▶│   count_up       │
                    │                  │──▶ .Done
 Reverse ──.down()─▶│  preset: 100     │──▶ .Acc
 Home, Auto .reset()▶│                  │
                    └─────────────────┘

The rung condition powers the instruction (top wire). Other pins are wired with dot-methods: .down(), .reset(), .clock(). Multiple conditions on one pin AND together. Home, Auto on .reset() means both must be True.

                       ┌─────────────────┐
 State == "g" ────────▶│   on_delay       │
                       │                  │──▶ .Done
                       │  preset: 3000    │──▶ .Acc
 StopBtn, Fault .reset()▶│  unit: "ms"      │
                       └─────────────────┘

Each pin gets its own line with \ continuation:

with rung(Sensor):
    count_up(PartCounter, preset=100) \
        .down(Reverse) \
        .reset(Home, Auto)

The code reads directly off the diagram. The pin name in the ASCII maps to the dot-method in the Python.

Programs

Program collects your rungs into a unit of logic that a runner can execute.

with Program() as logic:
    with rung(Start):
        latch(Running)
    with rung(Stop):
        reset(Running)

For larger programs, use the @program decorator to define logic as a function:

@program
def logic():
    with rung(Start):
        latch(Running)
    with rung(Stop):
        reset(Running)

Both forms produce the same thing, a Program you pass to PLC.

Structured tags (UDTs)

When you have a group of related tags, a @udt keeps them organized:

from pyrung import udt

@udt()
class Motor:
    Running: Bool
    Speed: Int
    Fault: Bool

Access fields with dot notation:

with rung(Motor.Running):
    out(StatusLight)

For multiple instances of the same structure, set count:

@udt(count=3)
class Pump:
    Running: Bool
    Flow: Real

# Access by instance
with rung(Pump[1].Running):
    out(Pump1Light)

Blocks

A block is a contiguous array of tags, used for grouped memory and physical I/O. Addresses typically start at 1 to match PLC conventions, but any start index is supported.

from pyrung import Block, InputBlock, OutputBlock, TagType

ds = Block("DS", TagType.INT, 1, 100)         # Internal memory DS1..DS100
x  = InputBlock("X", TagType.BOOL, 1, 16)     # Physical inputs X1..X16
y  = OutputBlock("Y", TagType.BOOL, 1, 16)    # Physical outputs Y1..Y16

Index into a block to get a tag:

ds[1]   # Tag "DS1", INT
x[1]    # Input tag "X1", BOOL
y[1]    # Output tag "Y1", BOOL

Use .select() for bulk operations:

blockcopy(ds.select(1, 4), ds.select(2, 5))  # Shift DS1..DS4 into DS2..DS5

Reading and writing values

Inside a with PLC(...) as plc: block (or with runner: when you have a runner from a fixture), you can read and write tag values directly:

with PLC(logic) as plc:
    State.value = "g"           # Write (one-shot, consumed after one scan)
    print(State.value)          # Read
    plc.step()                  # Step with current values

For persistent overrides that hold across multiple scans, use forces:

plc.force("Button", True)
plc.step()   # True
plc.step()   # Still True
plc.unforce("Button")

System points

The PLC exposes built-in status and control through the system namespace. Import it with from pyrung import system.

system.sys — scan-level status: always_on, first_scan, clock toggles (clock_10ms through clock_1h), mode_run, scan_counter, and scan_clock_toggle. Use first_scan for one-time initialization:

with rung(system.sys.first_scan):
    copy("g", State)

system.fault — math and runtime fault flags: division_error, out_of_range, math_operation_error, address_error, plc_error, and code (the most recent fault code as an integer). Each math() clears division_error and math_operation_error as it starts, and sets each only if it faults. Each math() or copy instruction does the same for out_of_range and address_error. Read the flag on the next rung. A math() on a false rung clears its flags too. A copy instruction on a false rung leaves them. A flag stays on into the next scan until an instruction clears it.

with rung(system.fault.division_error):
    latch(MathFaultSeen)

system.rtc — real-time clock: year4, month, day, hour, minute, second (read-only). system.rtc also has writable counterparts (new_hour, etc.) and apply_date/apply_time triggers. Use for time-of-day logic like shift changes.

The CLICK cheatsheet has the full point-to-address mapping.

What's next as your programs grow

Once you have UDTs with command/feedback pairs (solenoids, sensors, actuators), you can annotate the physical behavior of feedback signals with physical= and link=. The autoharness reads those annotations and drives feedback in tests automatically, so you stop writing boilerplate that toggles inputs by hand. See Physical Annotations and Autoharness.

Next steps