Context
pyrung.core.program.context
Program
Container for PLC logic (rungs and subroutines).
Used as a context manager to capture rungs
with Program() as logic: with Rung(Button): out(Light)
Also works with PLCRunner
runner = PLCRunner(logic)
call_subroutine
Execute a subroutine by name (legacy state-based API).
call_subroutine_ctx
Execute a subroutine by name within a ScanContext.
register_dialect
classmethod
Register a portability validator callback for a dialect name.
registered_dialects
classmethod
Return registered dialect names in deterministic order.
validate
Run dialect-specific portability validation for this Program.
Rung
Subroutine
Context manager for defining a subroutine.
Subroutines are named blocks of rungs that are only executed when called.
Example
with subroutine("my_sub"): with Rung(): out(Light)
SubroutineFunc
A decorated function that represents a subroutine.
Created by using @subroutine("name") as a decorator. When passed to call(), auto-registers with the current Program on first use.
Example
@subroutine("init") def init_sequence(): with Rung(): out(Light)
with Program() as logic: with Rung(Button): call(init_sequence)
ForLoop
Context manager for a repeated instruction block within a rung.
Branch
Context manager for a parallel branch within a rung.
A branch executes when both the parent rung conditions AND the branch's own conditions are true.
Example
with Rung(Step == 0): out(Light1) with branch(AutoMode): # Only executes if Step==0 AND AutoMode out(Light2) copy(1, Step, oneshot=True)
subroutine
Define a named subroutine.
Subroutines are only executed when called via call(). They are NOT executed during normal program scan.
Example
with Program() as logic: with Rung(Button): call("my_sub")
with subroutine("my_sub"):
with Rung():
out(Light)
forloop
Create a repeated instruction block context.
Example
with Rung(Enable): with forloop(10) as loop: copy(Source[loop.idx + 1], Dest[loop.idx + 1])
branch
Create a parallel branch within a rung.
A branch executes when both the parent rung conditions AND the branch's own conditions are true.
Example
with Rung(Step == 0): out(Light1) with branch(AutoMode): # Only executes if Step==0 AND AutoMode out(Light2) copy(1, Step, oneshot=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conditions
|
ConditionTerm
|
Conditions that must be true (in addition to parent rung) for this branch's instructions to execute. |
()
|
Returns:
| Type | Description |
|---|---|
Branch
|
Branch context manager. |