Skip to content

CLICK PLC Dialect

pyrung.click adds CLICK-PLC-specific blocks, type aliases, address mapping, nickname file I/O, validation, and a soft-PLC adapter on top of the hardware-agnostic core.

Installation

pip install pyrung

pyrung.click uses pyclickplc for address metadata, nickname CSV I/O, and soft-PLC server/client integration.

Imports

from pyrung import Bool, Int, PLC, Program, rung, copy, latch, reset, rise
from pyrung.click import ClickBlocks, TagMap

x, y, c, t, ct, sc, ds, dd, dh, df, xd, yd, xd0u, yd0u, td, ctd, sd, txt = ClickBlocks()

Workflow: write first, validate later

pyrung is intentionally permissive. Write logic with semantic tag names and native Python expressions, and simulate freely. No address mapping is required. Hardware constraints are opt-in.

The natural progression:

  1. Write — define semantic tags (StartButton, MotorRunning, Speed) and express logic in Python
  2. Simulate — run tests with a fixed dt that set inputs and assert outputs
  3. Map — create a TagMap linking semantic tags to CLICK hardware addresses
  4. Validate — mapping.validate(logic, mode="warn") surfaces CLICK-incompatible patterns
  5. Iterate — fix findings, tighten to mode="strict" when the program is clean

The validator tells you what CLICK can't do before you discover it at deploy time. It reports inline expressions, unsupported pointer modes and type mismatches.

Pre-built blocks

pyrung.click exports pre-built blocks for every CLICK memory bank:

Variable Bank Type Block kind
x X (inputs) BOOL InputBlock
y Y (outputs) BOOL OutputBlock
c C (bit memory) BOOL Block
ds DS (int memory) INT Block
dd DD (double int) DINT Block
dh DH (hex memory) WORD Block
df DF (float memory) REAL Block
t T (timer done) BOOL Block
td TD (timer acc) INT Block
ct CT (counter done) BOOL Block
ctd CTD (counter acc) DINT Block
sc SC (system control) BOOL Block
sd SD (system data) INT Block
txt TXT (text memory) CHAR Block
xd XD (word image) WORD InputBlock
yd YD (word image) WORD OutputBlock

Addresses use canonical CLICK display names:

x[1].name   # "X001"
y[1].name   # "Y001"
c[1].name   # "C1"
ds[1].name  # "DS1"

A system address that has a tag in system is that tag:

sd[21].name  # "rtc.month"
blockcopy(sd.select(21, 26), ds.select(500, 505))  # the date and time

Sparse banks

X and Y are sparse banks with non-contiguous valid addresses. .select() filters to valid addresses automatically:

x.select(1, 21)   # yields X001..X016 and X021 (17 tags, not 21)

A CLICK range must be consecutive addresses. CLICK validation and the export reject a range like this one in blockcopy(), fill(), a pack or an unpack. Split it at X016.

Per-slot configuration

Pre-built blocks support per-slot runtime policy for retention, default values, and naming. Configure before first access to a slot:

ds.slot(10, name="RecipeStep")
ds.slot(200, retentive=True, default=123)
td.slot(1, 5, retentive=False, default=0)

Each call to ClickBlocks() returns fresh blocks with no shared state, so multi-project workflows don't need manual cleanup.

Retentive registers ignore their CSV initial value, matching hardware. The CLICK PLC applies the initial value at project download only. A retentive register keeps its last value afterward. pyrung therefore models retentive slots with their type's zero value, and codegen omits default= for them.

Text memory (TXT)

copy("€", txt[1])                       # TXT1 holds the byte 80h
copy(txt[1], ds[1], convert=to_ascii)   # DS1 = 128

A TXT holds one byte. pyrung shows each byte as its Windows-1252 character, so a TXT holds €, ’, é and the rest of Windows-1252. pyrung refuses any other character, such as Ā.

A CLICK loads most TXT initial values as written. These are the exceptions:

  • € and 26 other characters, such as ’, load as other bytes. € loads as ¬. Validation reports each one as CLK_TXT_INITIAL_VALUE, and the ladder export warns about it.
  • CLICK's nickname import drops the whole row of a TXT whose initial value is a line feed or a carriage return. It keeps 1Ah, but then CLICK cannot open the project. Validation reports these three as the error CLK_TXT_INITIAL_REFUSED. The ladder export, to_nickname_file() and ladder_to_pyrung_project() refuse them.

pyrung starts the TXT with the character you wrote, so the simulation and a CLICK differ. To start a TXT with one of these characters, copy() it on the first scan.

Type aliases

CLICK-style constructor aliases as alternatives to IEC names:

CLICK alias IEC equivalent
Bit Bool
Int2 Dint
Float Real
Hex Word
Txt Char

DSL naming philosophy

This DSL follows CLICK PLC instruction naming as closely as possible, departing only when a Python conflict exists and the replacement name is genuinely better in a Python-hosted context.

  1. Keep the CLICK name when it's a clear action verb with no conflict: out, reset, fill, copy, blockcopy.
  2. Use a domain synonym when CLICK's name shadows a Python builtin. set becomes latch, which is well-understood PLC terminology. math keeps CLICK's name, although pyrung's math hides Python's math module in a file that imports it. Import Python's under another name (import math as pymath).
  3. Use clarified intent when Python's execution model changes the semantics. return becomes return_early. In CLICK, every subroutine needs an explicit RET. In this DSL, normal subroutine completion is implicit, so the only use is early exit. The name says so. A conditional END becomes end_early for the same reason. The export adds the END that closes the main program.
CLICK instruction pyrung DSL Reason
SET latch Shadows Python builtin set
RET return_early Normal return is implicit; only early exit needs a call
END end_early The export adds the closing END. Only an early end needs a call
BREAK break_loop break is a Python keyword
CONTINUE continue_loop continue is a Python keyword

Math formula functions use the Python name where one exists: LOG → log10, LN → log, ABS → abs, AVG → rng.mean(), MDN → rng.median(). See Range aggregates on CLICK.

The CSV ladder export uses CLICK-facing token names: return_early as return(), end_early as end(), forloop as for(...) with its body rungs and next(), and break_loop / continue_loop as break() / continue(). See the laddercodec CSV format guide for the full token grammar.

Writing a CLICK program

from pyrung import Bool, Real, PLC, Program, rung, copy, latch, reset, rise
from pyrung.click import ClickBlocks, TagMap

x, y, c, t, ct, sc, ds, dd, dh, df, xd, yd, xd0u, yd0u, td, ctd, sd, txt = ClickBlocks()

# Define semantic tags (hardware-agnostic)
StartButton  = Bool()
StopButton   = Bool()
MotorRunning = Bool()
RawSpeed     = Real()
Speed        = Real()

# Write logic using semantic names
with Program() as logic:
    with rung(rise(StartButton)):
        latch(MotorRunning)

    with rung(rise(StopButton)):
        reset(MotorRunning)

    with rung(MotorRunning):
        copy(RawSpeed, Speed)

# Simulate — no mapping needed
with PLC(logic, dt=0.1) as plc:
    StartButton.value = True
    plc.step()

TagMap — mapping to hardware

TagMap links semantic tags and blocks to CLICK hardware addresses. Mapping is separate from logic. Write and simulate first, then map.

Dict constructor

mapping = TagMap({
    StartButton:  x[1],           # BOOL → X001
    StopButton:   x[2],           # BOOL → X002
    MotorRunning: y[1],           # BOOL → Y001
    RawSpeed:     df[1],          # REAL → DF1 (analog input)
    Speed:        df[11],         # REAL → DF11
})

Method-call syntax

StartButton.map_to(x[1])
Speed.map_to(ds[1])

Mapping a block to a hardware range

Alarms = Block("Alarms", TagType.BOOL, 1, 100)

mapping = TagMap({
    Alarms: c.select(101, 200),    # Alarms[1..100] → C101..C200
})

Timer/Counter mapping

Timer and Counter are built-in UDTs. Map them to CLICK hardware banks explicitly, just like any other block:

OvenTimer = Timer.clone("OvenTimer")

mapping = TagMap([
    OvenTimer.Done.map_to(t[1]),
    OvenTimer.Acc.map_to(td[1]),
])

Codegen from nicknames

When importing ladder CSV with nicknames, a nickname on a T or CT address produces a named clone:

# Without nickname → clone named after the operand, fields named after their addresses
T1 = Timer.clone("T1", nicknames={'Done': 'T1', 'Acc': 'TD1'})
on_delay(T1, preset=100)

# With nickname {"T1": "OvenTimer"} → clone named after the nickname
OvenTimer = Timer.clone("OvenTimer", nicknames={'Done': 'OvenTimer', 'Acc': 'TD1'})
on_delay(OvenTimer, preset=100)

The clone is named after the T (done-bit) nickname, or after the TD/CTD nickname if the T has none.

If the nickname already ends with _Done or _Acc, the suffix is stripped automatically. "OvenTimer_Done" becomes Timer.clone("OvenTimer").

The clone takes the retentive setting of its addresses. The built-in Timer and Counter already match CLICK's bank defaults (T and TD are non-retentive, CT and CTD are retentive). Only an address whose retentive setting differs from its bank default gets an override, such as Timer.clone("T1", ..., retentive={'Acc': True}) for a TD set retentive.

Condition references resolve through the clone. A rung conditioned on T1 renders as OvenTimer.Done:

with rung(OvenTimer.Done):
    out(AlarmLight)

Input inference

When a tag is mapped to an input bank (x or xd), TagMap automatically marks it external=True. This tells pyrung that the tag's value comes from outside the ladder. The ladder lints and how() then treat it as an input, so you don't need to declare external=True yourself.

StartButton = Bool()                       # external=False initially
mapping = TagMap({StartButton: x[1]})
assert StartButton.external                # now True — stamped by TagMap

Tags mapped to output or memory banks (y, c, ds, etc.) are not stamped external. Tags that are readonly are skipped, because readonly and external are mutually exclusive.

Type validation at map time

TagMap validates that logical and hardware data types match:

TagMap({Speed: c[1]})   # raises: INT cannot map to C (BIT)

From nickname file

Load an existing CLICK nickname CSV:

mapping = TagMap.from_nickname_file("project.csv")

The importer reconstructs blocks, structures, and standalone tags from CSV comment markers and nickname patterns. Standalone nicknames become individual Tag objects. Non-marker address-comment text is preserved on standalone tags and block slots for CSV round-trip export.

For strict grouping validation, pass mode="strict". A structure grouping mismatch then fails immediately (it would otherwise fall back to plain blocks with a warning).

mapping = TagMap.from_nickname_file("project.csv", mode="strict")

Imported structure metadata is available via mapping.structures and mapping.structure_by_name("Base").

CSV marker format

The comment field on CSV rows carries block and structure boundaries. Three marker types:

Marker Example Meaning
Opening <Alarms:block> Start of a semantic plain block
Closing </Alarms:block> End of a semantic plain block
Self-closing <Config.timeout:udt /> Single-row semantic marker

Bare tags such as <Alarms>, </Alarms>, <Base.field> and <Base.field /> are grouping-only comments. They do not reconstruct pyrung semantics. They only group rows visually. Any inner nicknames import as ordinary standalone tags.

Bracket metadata can appear alongside markers or ordinary comments. It preserves tag flags, choices, range bounds, units, and physical annotations through nickname CSV import/export. For int-backed boolean values in register memory, use the built-in shorthand [choices=Bool] instead of spelling out [choices=False:0|True:1].

Plain blocks use explicit :block markers: <Alarms:block> / </Alarms:block>. If the logical start differs from the inferred default, export/import uses <Alarms:block(n)> or <Alarms:block(start=n)>.

Named arrays use :named_array markers. Count and stride are optional. The importer infers them from the row span between the open and close tags:

<Task:named_array>            count=1, stride from row count
<Task:named_array(2)>         count=2, stride = rows / count
<Task:named_array(2,3)>       count=2, stride=3 (fully explicit)

When both count and stride are given, the row span must equal count × stride. When stride is omitted, the row count must be divisible by count.

Nickname patterns. For count > 1, nicknames must follow {Base}{instance}_{field} with 1-based instance numbers. The instance is derived from position: position // stride + 1. The field name is what follows the {Base}{instance}_ prefix (Channel1_Id → field Id).

For count = 1, nicknames default to the compact form {Base}_{field} (no instance number). If the CSV already uses numbered names like Task1_Call, the importer detects this and sets always_number=True automatically. To force numbered names explicitly, add ,always_number to the marker:

<Task:named_array(1,2,always_number)>
</Task:named_array(1,2,always_number)>

The always_number flag only matters for singletons. A named array with count > 1 is always numbered.

Instance rules. Instance 1 defines the field template, so all its fields must be explicitly named. Instance 2+ fields must match instance 1's pattern (correct field name and instance number). Unnamed slots in instance 2+ are fine. The importer reads their settings and comments. A field name in instance 2+ that wasn't defined in instance 1 is an error.

Example: Channel with 2 instances, 3 fields, no gaps (stride=3):

Address Nickname Comment
DS101 Channel1_Id <Channel:named_array(2,3)>
DS102 Channel1_Val
DS103 Channel1_Name
DS104 Channel2_Id
DS105 Channel2_Val
DS106 Channel2_Name </Channel:named_array(2,3)>

Singleton with compact names (count=1):

Address Nickname Comment
DS501 Task_Call <Task:named_array(1,2)>
DS502 Task_Done </Task:named_array(1,2)>

If stride exceeds the field count, the extra slots at the end of each instance are gaps (empty nicknames). If a gap comes before a named field, the group becomes separate tags, and mapping.structure_warnings says so. Example with gaps at the end:

Address Nickname Comment
DS101 Sensor1_Raw <Sensor:named_array(2,3)>
DS102 Sensor1_Scaled
DS103 (gap)
DS104 Sensor2_Raw
DS105 Sensor2_Scaled
DS106 </Sensor:named_array(2,3)>

CLICK codegen can round-trip aligned whole-instance spans back into pyrung as Name.instance(...) or Name.instance_select(...) instead of raw bank ranges. This works for both dense and sparse layouts:

blockcopy(RecipeProfile.instance(2), WorkingRecipe.select(1, 3))
fill(0, RecipeProfile.instance_select(1, 2))

UDTs use explicit :udt markers per field and memory bank. Each attribute range is a separate marker:

<Motor.Speed:udt>
</Motor.Speed:udt>
<Config.Timeout:udt />

The importer collects all Base.Field:udt ranges that share the same base name and assembles them into a single @udt. Field attribute ranges must have matching hardware span lengths across all attributes.

Bare dotted tags such as <Motor.Speed> are grouping-only and do not reconstruct a UDT.

Nesting is not supported. A UDT field cannot itself be a named array (for example, Sts.Recipes:named_array(20,50) won't parse). Flatten the name instead, as StsRecipes:named_array(20,50).

Conflict rules. The same base name cannot be used across different marker kinds. These combinations are all errors:

  • Same name as both :named_array and .attr:udt
  • Same name as both :block and :named_array
  • Same name as both :block and .attr:udt
  • Duplicate :named_array or :block markers for the same name

To nickname file

Export to CLICK nickname CSV for import into CLICK Programming Software:

mapping.to_nickname_file("project.csv")

Mapped tags and blocks emit rows with canonical logical names, initial values, retentive flags, and preserved address comments. Unmapped tags are omitted. CLICK nicknames hold at most 24 characters. Validation reports a longer one as CLK_NICKNAME_TOO_LONG, and ladder export refuses it.

Validation

After mapping, validate your program against CLICK hardware restrictions:

report = mapping.validate(logic, mode="warn")
print(report.summary())

for finding in (*report.errors, *report.warnings, *report.hints):
    print(finding)

Each finding prints like this:

[math-operand-bank] hint
 --> Main:R1
  |
  |  math((Count + Mask), Total)
  |       ^^^^^^^^^^^^^^ DH1 is refused
  |
  = hint: A decimal Math takes only DS, DD, DF, TD, CTD and SD registers and decimal constants, and X, Y, C, T and SC bits only as the range of SUM ( a : b ). Split it into a math() of WORD tags and a math() of the other tags. Pass the result from one to the other through a tag. CLICK's error is OBJ-1002.

Common findings:

Issue pyrung allows CLICK requires
Pointer in copy source Any block, arithmetic DS only, no arithmetic
Inline expression in condition (A + B) > 100 Must use math() first
out / latch / reset target Any tag (reset(DS1), reset(TXT1)) Y, C or an SC bit that CLICK writes. out, latch and reset all take the same SC bits. A range that spans read-only SC bits passes as typed (bank-not-allowed, not-writable). Clear a register with copy() or fill()
Whole-number constant in math() Any. Hex mode cuts it to 16 bits, as a CLICK does −2147483648..2147483647, and 0..65535 in hex mode (math-constant-out-of-range). In hex mode the export writes −32768..−1 as its 16 bits. It rejects any other hex constant, even when allowed
Constant in copy() or fill() into a Word Any 0..65535 into DH, XD or YD (CLK_COPY_WIDE_CONSTANT). The export writes −32768..−1 as its 16 bits. A copy() may have a fraction, and a CLICK drops it (copy(1.5, DH1) stores 1). A fill() of several registers must be a whole number. The export rejects any other constant, even with allow=
A bit in a math() formula (c[1] + 5) Any Bool tag, read as 0 or 1 A bit only inside SUM over a bit range (math-operand-bank). Write c.select(1, 1).sum() + 5, or copy the bit into a DS tag in a rung above. A CT bit has no SUM, so copy it into a DS tag
Compare of a Word mapped to DH, XD or YD with a DS, DD, DF, TD, CTD or SD tag Compares the values Both sides hex, or a hex tag against a constant. Copy the Word into a DS tag first (compare-mixed-types, OBJ-007)
Compare of text with a number (txt[1] == 5, ds[1] == "A", txt[1] == ds[1]) Runs the compare Text on both sides, or a number on both sides (compare-mixed-types, OBJ-007). To compare a character's code, copy it into an Int with convert=to_ascii first
Compare of a bit with True or False (c[1] == True) Runs the compare A contact. Write c[1] or ~c[1] (bank-not-allowed)
Range in blockcopy(), fill(), a pack or an unpack Any tags, in any order Consecutive addresses in one bank, in ascending order (CLK_COPY_RANGE_NOT_CONSECUTIVE). y.select(1, 21) skips Y017..Y020, so split it at Y016. The export rejects such a range even with allow=
Range in .sum(), .max(), .min(), .median() or .mean() Any tags, in any order Consecutive addresses in one bank, in ascending order (CLK_MATH_RANGE_NOT_CONTIGUOUS, CLK_MATH_RANGE_MIXED_BANKS). Unlike a blockcopy() range, an aggregate may cross the gap between X or Y slots, such as x.select(1, 21).sum(). The export rejects a range that breaks the rule even with allow=
Text compare on a Char (Name == "AB") Compares the Chars that follow it in its block The Chars must map to consecutive TXT addresses, in order (CLK_TEXT_WINDOW_NOT_CONTIGUOUS)
Text copy() that runs past the end of its bank Drops what runs past Start it low enough to keep every character. If a constant runs past TXT1000 as written, such as 1.5 into TXT999, CLICK's Copy rejects it (text-past-bank). The export rejects it too, even with allow=. Both also reject 10 into TXT1000 with to_binary. That copy writes one character, but CLICK counts one TXT for each character of 10. If the constant runs past TXT1000 only after conversion, such as 1.5 into TXT995, validation warns (CLK_TEXT_PAST_BANK). A register source gets the same warning. CLICK's Copy accepts both
Text copy() that runs past the end of its block Drops what runs past A CLICK keeps writing into the bank past the block. Map the block onto more of the bank (CLK_TEXT_PAST_BLOCK)
Real fill() into a block mapped onto part of TXT (fill(1.5, Name.select(1, 4))) Writes the text past the range, and drops what runs past the block A CLICK writes the whole text past the range, on into the bank past the block. Validation warns and says what pyrung drops (CLK_FILL_REAL_PAST_RANGE)

Timer preset limits

CLICK timer accumulators are 16-bit signed INT (max 32,767). A literal preset exceeding this range silently clamps at runtime. Validation reports constant-out-of-range for a preset above 32,767 in on_delay(), off_delay(), debounce(), flasher() and timed(). Use a larger time unit instead.

Unit Max preset Max duration
Tms 32,767 32.7 seconds
Ts 32,767 9.1 hours
Tm 32,767 22.7 days
Th 32,767 3.7 years
Td 32,767 89 years
# Wrong — clamps silently to 32.7 seconds
on_delay(MyTimer, preset=60000)

# Right — use seconds
on_delay(MyTimer, preset=60, unit="sec")

Out coil variants

toggle, debounce, flasher and timed are CLICK v4's Out coil types. Map each phase timer to T<n> with the TD<n> of the same number. Map a register preset to DS. Validation reports a Done bit that has no address (CLK_BANK_UNRESOLVED) or sits in a wrong bank (bank-not-allowed). It reports a Done and an Acc with different numbers as CLK_TIMER_PAIR_MISMATCH for debounce, flasher and timed. It reports the same fault as timer-pair-mismatch for on_delay(), off_delay() and the counters.

CLICK's Out dialog offers fewer units than its timers do. debounce and flasher accept ms or sec. timed accepts ms, sec or min. Validation reports any other unit as coil-time-unit. Only toggle accepts immediate(...).

For/Next

A forloop exports as its For rung, one rung per body item, and next(). A bare instruction becomes a wire-only rung. A with rung(...) inside the loop keeps its contacts. A nested loop becomes a For rung of its own. break_loop() and continue_loop() export as break() and continue().

CLICK allows:

  • three levels of loops, counting the loops of a subroutine called inside a loop (CLK_FORLOOP_NESTING_DEPTH)
  • a one-shot only on the outermost For (CLK_FORLOOP_INNER_ONESHOT)
  • no return_early() between For and Next (CLK_FORLOOP_RETURN_INSIDE). Validation reports end_early() there with the same code. No one has tested whether a CLICK accepts an END between For and Next.
  • no nested subroutine calls, so validation flags a call() inside a subroutine (CLK_NESTED_CALL)

loop.idx has no CLICK address. Count iterations with a DS pointer instead. A false For rung skips its body.

A DS count is read as unsigned 16-bit, so -1 runs 65535 iterations. A DD count is signed, so -1 runs none. A count of 0 runs none.

Findings are hints by default (mode="warn"). Use mode="strict" to treat hints as errors.

Program steps

A CLICK project holds at most 8,000 program steps. estimate_steps(pyrung_to_ladder(logic, mapping)) estimates a program's count, and validation reports CLK_STEP_LIMIT as a program nears or passes the limit. Export still writes such a program, with the finding as a UserWarning.

Pointers into a block mapped at an offset

Blk = Block("Blk", TagType.INT, 1, 10)       # mapped onto ds.select(100, 109)
copy(Blk[idx], dest)                         # CLK_PTR_BLOCK_OFFSET

CLICK accepts only DS[pointer]. Validation reports CLK_PTR_BLOCK_OFFSET, and the ladder export rejects the program. The finding is a hint in the default mode and an error in strict mode. The export rejects the program in both modes. Add the offset in a DS tag and index the bank directly:

math(idx + 99, Ptr)
copy(ds[Ptr], dest)

Pointers past a bank

ptr = Int("Ptr", max=1010)
copy(dd[ptr], out)  # CLK_PTR_PAST_BANK: DD ends at DD1000

Validation warns with CLK_PTR_PAST_BANK when a pointer can address memory past the end of its bank. To decide which values a pointer can hold, validation uses:

  • the tag's choices
  • the tag's min and max (a max alone counts as an upper bound)
  • every value that a copy() or math() in the program can store in the pointer
  • 0 to count - 1, for a For loop index with a constant count

Validation does not report a pointer built from a value that the program cannot bound.

A CLICK copy() through a DD pointer past DD1000 writes into the memory there and sets no flag. pyrung drops that write, and a read from there returns 0. Keep the pointer inside the bank, or give the tag min/max or choices.

Ladder CSV export

pyrung_to_ladder(program, tag_map) emits deterministic CLICK ladder CSV row matrices via LadderBundle.

from pyrung.click import pyrung_to_ladder

bundle = pyrung_to_ladder(logic, mapping)
bundle.main_rows          # inspect rows in-memory
bundle.write("./output")  # write main.csv + subroutines/*.csv to disk

For the consumer-facing CSV decode contract (files, row semantics, token formats, branch wiring, and supported tokens), see the laddercodec CSV format guide.

To convert ladder CSV back into pyrung Python source, see CLICK Python Codegen.

A blank TXT holds NUL. A CLICK text constant writes one TXT per character, so "" writes nothing. CLICK ends a quoted constant at NUL. A blank text constant ("" or "\x00", in copy, fill, a compare or a search) therefore exports as $00, CLICK's ASCII-code constant for NUL. $00 imports back as the blank.

copy('say "hi"', txt[1])                                   # exports as copy("say "hi"",TXT1)
search(txt.select(1, 4) == 'a"b', result=ds[1], found=c[1])  # exports as search(TXT1..TXT4 == "a"b",...)
txt[1] == 'a"b'                                            # exports as TXT1=="a"b"
copy('"', txt[1])                                          # exports as copy($22,TXT1)
fill('a"b', txt.select(1, 4))                              # the export refuses it

A Copy, a Search and a comparison write their text as typed, with no escape for a quote. CLICK's editor accepts a quote in the text of all three. So copy('say "hi"', txt[1]) fills TXT1..TXT8. A text constant of one quote exports as $22, the ASCII code for a quote. The export refuses a fill() text that holds a quote among other characters. Write the text without the quote.

Empty and comment-only rungs

Empty rungs survive the round-trip. A with rung(): pass in pyrung exports as NOP in the CLICK CSV AF column and imports back as pass.

comment("--- Motor Control Section ---")
with rung():
    pass  # becomes NOP in CLICK ladder CSV

For CLICK programs that want to be explicit, pyrung.click also provides nop():

from pyrung.click import nop

comment("Section header")
with rung():
    nop()  # one per rung, must be the sole instruction

Both forms produce identical CSV output.

An empty rung with conditions (with rung(C1): pass) exports as the contacts in front of a NOP. CLICK keeps that rung. Its Syntax Check reports it as C0003, "There is no Coil instruction in the rung". Validation reports the rung as CLK_NOP_NO_COIL. The export writes the rung and warns.

Boxes with pins

A box with pins (a counter, a retentive timer's .reset(), a shift register, a drum) ends its rung. CLICK takes all of a box's inputs (its pins) from one branch. If the box's input wire also feeds another output, CLICK draws the rung but does not compile it (error C000A). pyrung rejects that rung when you build the program.

Put the outputs that come before the box in a rung above it. Put the outputs that follow it in a rung of their own:

with rung(Start):
    latch(Running)
with rung(Start):
    count_up(Parts, preset=10).reset(Clear)

A box with no pins, such as a TON, can share the wire.

A program saved before a syntax check can hold a CLICK rung with several wires. That rung imports with .continued() on each wire after the first. Outputs share a with rung(...) or with branch(...) only where the CLICK rows branch them off one contact. The generated code then reads the way the CLICK rung does. Two contacts with the same label stay two contacts.

Loading PLC state

Use CLICK Programming Software's PLC > Read Data from PLC to dump the live state of a CLICK PLC to CSV, then load that snapshot into a pyrung runner so it starts right where the PLC was.

from pyrung import PLC

state = mapping.load_snapshot("data.csv")
runner = PLC(logic, initial_state=state)

load_snapshot() reads the CSV and seeds each tag that the TagMap maps. It skips any address that isn't mapped.

By default (skip_default=True) it also skips zero, false and empty values. A PLC dump lists every address, and most sit at their default. Skipping them keeps the state small.

Some programs use CLICK bank slots directly, such as x.slot(1, name="Button") or a bare ds[5]. Generated projects do this for plain nicknames. These addresses have no TagMap entry. Pass the bank set as blocks to seed them too:

state = mapping.load_snapshot("data.csv", blocks=blocks)

CLICK writes a TXT comma without quotes, so a TXT row that holds a comma and an empty address can be read more than one way. load_snapshot() leaves those TXT addresses out, loads the rest of the file and warns with SkippedAddressesWarning. The warning's addresses attribute lists the skipped addresses.

To inject a PLC snapshot mid-run, read the file with pyclickplc and translate the hardware addresses with tags_from_plc_data():

from pyclickplc import PlcDataReadError, read_plc_data

try:
    data = read_plc_data("data.csv", skip_default=True)
except PlcDataReadError as exc:
    data = exc.values
runner.patch(mapping.tags_from_plc_data(data, blocks=blocks))
runner.step()  # applied at the start of this scan

read_plc_data() raises PlcDataReadError on a file with those TXT rows. The error's values attribute holds every other address, and its addresses attribute lists the TXT addresses it could not read.

ClickDataProvider — soft PLC

ClickDataProvider implements the pyclickplc DataProvider protocol, bridging pyrung's SystemState to a Modbus TCP server. This lets pyrung act as a soft PLC accessible from CLICK Programming Software or any Modbus client.

from pyrung.click import ClickDataProvider
from pyclickplc.server import ClickServer

provider = ClickDataProvider(runner, tag_map=mapping)
server = ClickServer(provider, port=502)

A read returns the last value that a client wrote since the last scan, or else the value that the last scan stored, as a CLICK does. A forced tag reads as its force. Writes queue a runner.patch(), so the ladder sees the value at the start of the next scan.

If another device on the LAN can't reach the soft PLC, check the Windows Firewall. It must allow both the TCP port (default 502) and the Python interpreter.

Word-image (XD / YD) addressing

  • XD* reads compute bit-image words from current X bit state.
  • YD* reads compute bit-image words from current Y bit state.
  • YD* writes fan out to the corresponding Y bits.
  • XD* writes are rejected (read-only).

Communication instructions

send and receive implement Modbus communication with remote devices. Two addressing modes are supported:

CLICK addresses (CLICK-to-CLICK)

Use a CLICK address string for remote_start when talking to another CLICK PLC:

from pyrung.click import ModbusTcpTarget, send, receive

plc = ModbusTcpTarget("plc1", "192.168.1.20")

send(
    target=plc,
    remote_start="DS1",
    source=LocalSetpoint,
    sending=CommSending,
    success=CommSuccess,
    error=CommError,
    exception_response=CommEx,
)

receive(
    target=plc,
    remote_start="DS1",
    dest=LocalWords.select(1, 4),
    receiving=CommReceiving,
    success=CommSuccess,
    error=CommError,
    exception_response=CommEx,
)

CLICK handles word swap and character order natively. Nothing needs configuring on the pyrung side.

Raw Modbus addresses (any device)

Use ModbusAddress for remote_start when talking to non-CLICK Modbus devices (VFDs, meters, sensors, etc.):

from pyrung import ModbusAddress, ModbusTcpTarget, ModbusRtuTarget, RegisterType, send, receive

vfd = ModbusTcpTarget("vfd", "192.168.1.50")

send(
    target=vfd,
    remote_start=ModbusAddress(400001),
    source=SpeedSetpoint,
    sending=VfdSending,
    success=VfdSuccess,
    error=VfdError,
    exception_response=VfdEx,
)

meter = ModbusRtuTarget("meter", "/dev/ttyUSB0", device_id=3, baudrate=19200)

receive(
    target=meter,
    remote_start=ModbusAddress(300001),
    dest=MeterReading,
    receiving=MeterReceiving,
    success=MeterSuccess,
    error=MeterError,
    exception_response=MeterEx,
    word_swap=True,
)

ModbusAddress accepts MODBUS 984 addresses (e.g. 400001 for holding, 300001 for input, 100001 for discrete input) or hex strings with an h suffix (e.g. "0h", "FFFEh"). For 984 addresses, the register type is inferred from the prefix. Hex addresses need an explicit RegisterType since the offset alone is ambiguous.

word_swap controls how 32-bit values (DINT, REAL) are packed across register pairs. False (the default) puts the high word first. True puts the low word first. This matters only for 32-bit CLICK types (DD, DF, etc.).

RegisterType selects the Modbus function code: HOLDING (FC 3/6/16, default), INPUT (FC 4, read-only), COIL (FC 1/5/15), DISCRETE_INPUT (FC 2, read-only). Sending to INPUT or DISCRETE_INPUT raises ValueError.

Target types

Type Transport Live I/O Codegen
ModbusTcpTarget Ethernet Yes (pymodbus for raw, pyclickplc for CLICK addresses) Yes
ModbusRtuTarget Serial Yes (pymodbus) Not yet
str (name only) — No (inert) Yes (resolved via ModbusClientConfig)

When target is a ModbusTcpTarget or ModbusRtuTarget, communication runs asynchronously in a background worker pool. The scan loop stays synchronous. When target is a plain string, the instruction is inert during simulation and exists only for CircuitPython code generation.