Skip to content

Instructions API

Tier: Stable Core

Domain objects for ladder logic instructions. Email, Home, PositionMove and VelocityMove are provisional: their fields are named after CLICK's tag ids and will change. Each Literal alias, such as ContactKind, lists the values that a field accepts. typing.get_args(ContactKind) returns them.

laddercodec.Condition

Base class for condition-side instructions (Contact, CompareContact).

The constructor of each condition class in laddercodec accepts the keyword-only allow, as :class:Instruction describes.

laddercodec.Instruction

Base class for instructions in the AF (output) column, such as Coil, Timer and Copy.

The constructor of each instruction class in laddercodec accepts the keyword-only allow, a list of the codes of the refusals that you accept, such as Coil("X001", allow=["bank-not-allowed"]). It builds an instruction whose refusals all have a listed code instead of raising, with the same limits as read_csv(path, allow=...). encode() writes that instruction from its fields. Any other refusal raises. The instruction does not store allow. A bad allow raises :class:~laddercodec.LaddercodecError.

laddercodec.ConditionToken module-attribute

ConditionToken = (
    str
    | Contact
    | CompareContact
    | Condition
    | UnknownCondition
    | Tee
)

A condition-column cell of a Rung.

It is a wire token ("-", "|", "T" or ""), a Condition such as a Contact or a CompareContact, or an UnknownCondition for a cell that the decoder could not read. A cell that holds one of these instructions and a wire down is a :class:Tee around the instruction.

laddercodec.InstructionToken module-attribute

InstructionToken = (
    str
    | Coil
    | OutVariant
    | Timer
    | Counter
    | Copy
    | BlockCopy
    | Fill
    | Pack
    | Unpack
    | ForLoop
    | Next
    | Break
    | Continue
    | Shift
    | End
    | Return
    | Undecoded
    | RawInstruction
    | Instruction
    | UnknownInstruction
)

An AF-column cell of a Rung.

It is "", "NOP", an output instruction, or an UnknownInstruction for a cell that the decoder could not read. An output instruction is any Instruction, including a RawInstruction and the Undecoded marker.

laddercodec.Contact dataclass

Bases: Condition, AllowsRefusals

A contact: normally open (no), normally closed (nc), or an edge contact (rise, fall).

Contact("X001", kind="nc", immediate=True) is the CSV token ~X001.immediate. Only an NO or NC contact on X has an immediate form.

from_csv_token classmethod

from_csv_token(token: str) -> Contact

Parse NO/NC/immediate forms plus edge forms rise(...)/fall(...).

A token with the wire-down prefix T: or |:, such as T:X001, raises :class:~laddercodec.CsvReadError. A contact holds no wire. Wrap the contact in a :class:~laddercodec.Tee instead.

cell_params

cell_params() -> dict

Return the ClickCell keyword arguments that this instruction sets.

build_blob

build_blob() -> bytes

Build the instruction data blob for this contact cell.

laddercodec.ContactKind module-attribute

ContactKind = Literal['no', 'nc', 'rise', 'fall']

A contact's kind. The CSV tokens of the four kinds are X001, ~X001, rise(X001) and fall(X001).

laddercodec.CompareContact dataclass

Bases: Condition, AllowsRefusals

A comparison contact (EQ, NE, GT, LT, GE, LE).

It sits in a condition column, like any contact. Its binary class name is Compare and its type marker is 0x2714.

from_csv_token classmethod

from_csv_token(token: str) -> CompareContact

Parse the CSV token of a compare contact, such as DS1==1 or DS2>=DS3.

A token with the wire-down prefix T: or |:, such as T:DS1==1, raises :class:~laddercodec.CsvReadError. A contact holds no wire. Wrap the contact in a :class:~laddercodec.Tee instead.

cell_params

cell_params() -> dict

Return the ClickCell keyword arguments that depend on this instruction alone.

build_blob

build_blob() -> bytes

Build the instruction data blob for this compare contact cell.

laddercodec.CompareOp module-attribute

CompareOp = Literal['==', '!=', '>', '<', '>=', '<=']

The operator of a compare contact or a Search.

laddercodec.Tee dataclass

A condition cell that holds a contact and a wire down from the cell's left edge.

It is the contact form of the "T" wire token. In the CSV, the cell is the contact's token with a T: prefix. Tee(Contact("X001")) is T:X001.

contact is a Contact, a CompareContact or another Condition, or an UnknownCondition. Building a Tee around anything else raises :class:~laddercodec.FieldError, and so does a Tee around a Tee.

to_csv

to_csv() -> str

Return the CSV text of the cell: T: and the contact's token, such as T:X001.

write_csv writes this text for the cell. An UnknownCondition has no CSV token, so a Tee around one raises :class:~laddercodec.CsvWriteError.

laddercodec.Coil dataclass

Bases: Instruction, AllowsRefusals

An output coil: out, latch (Set) or reset, on one bit or a range.

Coil("Y001", kind="latch") is the CSV token latch(Y001), and Coil("C1", range_end="C8", oneshot=True) is out(C1..C8,oneshot=1).

from_csv_token classmethod

from_csv_token(token: str) -> Coil

Parse the CSV token of a coil, such as out(Y001) or out(C1..C8,oneshot=1).

An immediate(...) wrapper goes inside the call, as in out(immediate(Y001)).

cell_params

cell_params() -> dict

Return the ClickCell keyword arguments that depend on this instruction alone.

build_blob

build_blob() -> bytes

Build the instruction data blob for this coil cell.

laddercodec.CoilKind module-attribute

CoilKind = Literal['out', 'latch', 'reset']

The kind of a coil. Each kind is also the name of the coil's CSV call: out(...), latch(...) or reset(...).

laddercodec.OutVariant dataclass

Bases: Instruction, AllowsRefusals

A CLICK v4 Out coil variant: toggle, debounce, flasher or timed.

from_csv_token classmethod

from_csv_token(token: str) -> OutVariant

Parse toggle(...), debounce(...), flasher(...) or timed(...).

cell_params

cell_params() -> dict

Return the ClickCell keyword arguments that depend on this instruction alone.

build_blob

build_blob() -> bytes

Build the instruction data blob for this Out variant cell.

laddercodec.OutVariantKind module-attribute

OutVariantKind = Literal[
    "toggle", "debounce", "flasher", "timed"
]

The kind of an OutVariant coil. Each kind is also the name of its CSV call.

laddercodec.OutVariantUnit module-attribute

OutVariantUnit = Literal['Tms', 'Ts', 'Tm']

The time unit of a debounce, flasher or timed coil. A timed coil accepts all three. A debounce or flasher coil accepts "Tms" or "Ts". A toggle has no unit, and its unit is "".

laddercodec.Timer dataclass

Bases: PinnedInstruction, AllowsRefusals

An on-delay or off-delay timer instruction.

The timer sits in the AF column. CLICK stores it with the binary class name Tmr and the type marker 0x2718. The box spans the grid rows from its own row down to the row of the reset input, which pin_rows gives, so it is at least two rows tall.

enable_func_code property

enable_func_code: str

Return the func code of the enable input.

It is 8717 plus 6 times the unit index, plus 1 for an off-delay timer or 2 for a retentive on-delay timer.

reset_func_code property

reset_func_code: str

Return the func code of the reset input.

For a retentive timer it is 8717 plus 6 times the unit index, plus 3. For any other timer it is "0".

from_csv_token classmethod

from_csv_token(token: str) -> Timer

Parse a timer token such as on_delay(T1,TD1,preset=1000,unit=Tms).

The positional arguments are done and accumulator. The keyword arguments are preset and unit. The token never names retained. A .reset() pin row makes the timer retentive.

cell_params

cell_params() -> dict

Return the ClickCell keyword arguments that the timer sets: the box's height in grid rows and its number of sub-rows.

build_blob

build_blob() -> bytes

Build the instruction data blob for this timer cell.

laddercodec.TimerKind module-attribute

TimerKind = Literal['on_delay', 'off_delay']

The kind of a timer. Each kind is also the name of its CSV call.

laddercodec.TimeUnit module-attribute

TimeUnit = Literal['Tms', 'Ts', 'Tm', 'Th', 'Td']

The time unit of a timer or a drum: milliseconds, seconds, minutes, hours or days.

laddercodec.Counter dataclass

Bases: PinnedInstruction, AllowsRefusals

A counter instruction (count_up / count_down).

from_csv_token classmethod

from_csv_token(token: str) -> Counter

Parse a CSV v1 counter token, such as count_up(CT1,CTD1,preset=100).

build_blob

build_blob() -> bytes

Build the instruction data blob for this counter cell.

laddercodec.CounterKind module-attribute

CounterKind = Literal['count_up', 'count_down']

The kind of a counter. Each kind is also the name of its CSV call.

laddercodec.Copy dataclass

Bases: Instruction, AllowsRefusals

A single copy instruction.

Attributes

source: Source operand or literal (e.g. "DS7", "42", "3.14"). dest: Destination operand (e.g. "DS8"). convert: The converter, by its CSV name: None (a plain copy), "to_value", "to_text", "to_binary" or "to_ascii". A TXT register copied into a numeric register other than DF needs "to_value" or "to_ascii", and None is refused for it. "to_binary" is refused for a DF register or a real constant copied into TXT. oneshot: Execute once on OFF→ON transition. suppress_zero: The suppress zero option of a copy to text, for an integer, hex or register source. True suppresses zeros and False does not. None is read as False. A DF source or a real constant copied with "to_text" has no such option. For it the attribute holds None, and any other value is refused. A TXT source refuses True. exponential: The numbering option of a copy to text, for a DF source or a real constant: False for real numbering and True for exponential. Any other source has no such option and refuses True. termination_code: The termination code. Only a copy into TXT accepts one, from one of these sources:

- a number register, with ``"to_text"``
- a number register other than DF, with ``"to_binary"``
- a number constant, with ``"to_text"``
- an integer or hex constant, with ``"to_binary"``
- a TXT register or quoted text, with ``None``

The code adds no TXT to the end that a constant fills. The value is ``"0"`` or
``"none"`` for no code, stored as ``"0"``. Otherwise it is a hex ASCII code
``"$HH"`` with exactly two digits: ``"$0D"`` is 0x0D, and ``"$D"`` is refused. A
lowercase code is stored in uppercase (``"$0d"`` as ``"$0D"``).

from_csv_token classmethod

from_csv_token(token: str) -> Copy

Parse copy(source, dest) with optional convert=to_* modifier.

Examples::

copy(DS7,DS8)
copy(42,DS9)
copy(DS26,DS27,oneshot=1)
copy(TXT1,DS10,convert=to_value)
copy(DS12,TXT2,convert=to_text(suppress_zero=1,termination_code=none))
copy(TXT1,TXT21,termination_code=$0D)

cell_params

cell_params() -> dict

Return ClickCell kwargs intrinsic to this instruction.

build_blob

build_blob() -> bytes

Build the instruction data blob for this copy cell.

laddercodec.CopyConvert module-attribute

CopyConvert = Literal[
    "to_value", "to_text", "to_binary", "to_ascii"
]

The converter of a single Copy, by its CSV name. Copy.convert is one of these or None.

laddercodec.BlockCopy dataclass

Bases: Instruction, AllowsRefusals

A block copy instruction (copy_type_idx=1).

Copies a contiguous range of source registers to a destination range.

Attributes

source_start, source_end: Source range (e.g. "DS28" .. "DS31"). dest_start, dest_end: Destination range. convert: None, "to_value" (the characters' values) or "to_ascii" (their ASCII codes), for a TXT source into a numeric register. oneshot: Execute once on OFF→ON transition. termination_code: The termination code. Only a TXT range copied to TXT accepts one. The value is "0" or "none" for no code, stored as "0", or a hex ASCII code "$HH", the same as for a single Copy.

cell_params

cell_params() -> dict

Return ClickCell kwargs intrinsic to this instruction.

build_blob

build_blob() -> bytes

Build the instruction data blob for this block copy cell.

laddercodec.BlockCopyConvert module-attribute

BlockCopyConvert = Literal['to_value', 'to_ascii']

The converter of a Block Copy, by its CSV name. BlockCopy.convert is one of these or None.

laddercodec.Fill dataclass

Bases: Instruction, AllowsRefusals

A fill instruction (copy_type_idx=2).

Fills a contiguous range of destination registers with a single value.

Attributes

value: The value to fill with: a constant ("0") or an address ("DS56"). dest_start, dest_end: Destination range. oneshot: Execute once on OFF→ON transition.

cell_params

cell_params() -> dict

Return ClickCell kwargs intrinsic to this instruction.

build_blob

build_blob() -> bytes

Build the instruction data blob for this fill cell.

laddercodec.Pack dataclass

Bases: Instruction, AllowsRefusals

A pack instruction (copy_type_idx=3).

Packs a range of source registers into a single destination.

Attributes

source_start, source_end: Source range (e.g. "C108" .. "C123"). dest: Destination operand (e.g. "DS167"). kind: "pack_bits", "pack_words" or "pack_text". allow_whitespace: Text option — allow whitespace in text packing. oneshot: Execute once on OFF→ON transition.

laddercodec.PackKind module-attribute

PackKind = Literal['pack_bits', 'pack_words', 'pack_text']

The kind of a Pack. Each kind is also the name of its CSV call.

laddercodec.Unpack dataclass

Bases: Instruction, AllowsRefusals

An unpack instruction (copy_type_idx=4).

Unpacks a single source into a range of destination registers.

Attributes

source: Source operand (e.g. "DS172"). dest_start, dest_end: Destination range (e.g. "C128" .. "C143"). kind: "unpack_to_bits" or "unpack_to_words". oneshot: Execute once on OFF→ON transition.

laddercodec.UnpackKind module-attribute

UnpackKind = Literal['unpack_to_bits', 'unpack_to_words']

The kind of an Unpack. Each kind is also the name of its CSV call.

laddercodec.Shift dataclass

Bases: PinnedInstruction, AllowsRefusals

A shift register instruction.

from_csv_token classmethod

from_csv_token(token: str) -> Shift

Parse shift(C99..C106).

build_blob

build_blob() -> bytes

Build the instruction data blob for this shift register.

laddercodec.Search dataclass

Bases: Instruction, AllowsRefusals

A search instruction.

Attributes

table_start: Start of search table (e.g. "DS72"). table_end: End of search table (e.g. "DS81"). value: Value to search for — register ("DS71") or literal ("A"). result: Destination register for the result index. found: Found flag bit (e.g. "C81"). op: Comparison operator: "==", "!=", ">", "<", ">=", "<=". continuous: Search continuously (every scan). oneshot: Execute once on OFF→ON transition.

laddercodec.Drum dataclass

Bases: PinnedInstruction, AllowsRefusals

A drum sequencer instruction: an event_drum, a time_drum or a time_event_drum.

The drum sits in the AF column. Its binary class name is Drum, and its type marker is 0x271B. A drum takes at least 4 grid rows. Row 0 is the main row, and the reset, jump and jog pins sit at pin_rows. These are rows 1, 2 and 3 unless OR legs push them down.

kind is the CSV call name. An event drum has events, a time drum has presets, and a time & event drum has both, one of each per step. The drum refuses the list that its kind does not use. A time & event step may leave either one blank, as "" here and None in CSV. The lists are stored as tuples.

A list field that is "" is blank, as in the CSV token event_drum(outputs=,...). The drum refuses a blank list that its kind uses, with the reason missing. It stores a blank list that its kind does not use as an empty tuple.

has_register_preset property

has_register_preset: bool

True if this time or time & event drum reads a step's duration from a register.

v4_blob property

v4_blob: bool

True if the drum is a time & event drum or a time drum with a register preset.

Such a drum is written in v4's 82-field form.

build_blob

build_blob() -> bytes

Build the instruction data blob for this drum cell.

laddercodec.DrumKind module-attribute

DrumKind = Literal[
    "event_drum", "time_drum", "time_event_drum"
]

The kind of a drum. Each kind is also the name of its CSV call.

laddercodec.Math dataclass

Bases: Instruction, AllowsRefusals

A math instruction.

Attributes

expression: The formula in Click syntax (e.g. "DS65 + DS66"). Building a Math rewrites it in the form CLICK shows, with one space between tokens, uppercase function names and addresses in display form. A real that CLICK keeps only when it is typed with an E stays in that form (1.2345E-04), though the blob stores it as CLICK shows it (0.00012345). A formula that CLICK's Math dialog refuses raises :class:FieldError. So does a <nickname>, because Math takes only addresses. dest: Destination register (e.g. "DS67"). mode: "decimal" or "hex". oneshot: Execute once on OFF→ON transition.

laddercodec.MathMode module-attribute

MathMode = Literal['decimal', 'hex']

The mode of a Math instruction: decimal or hex.

laddercodec.Call dataclass

Bases: Instruction, AllowsRefusals

Subroutine call instruction.

laddercodec.Return dataclass

Bases: Instruction, AllowsRefusals

Return instruction.

laddercodec.End dataclass

Bases: Instruction, AllowsRefusals

Program terminator instruction.

laddercodec.ForLoop dataclass

Bases: Instruction, AllowsRefusals

FOR loop instruction.

laddercodec.Next dataclass

Bases: Instruction, AllowsRefusals

NEXT loop instruction.

laddercodec.Break dataclass

Bases: Instruction, AllowsRefusals

The CLICK v4 BREAK instruction, which leaves the innermost loop.

Its binary class is Next, with func code 9233.

laddercodec.Continue dataclass

Bases: Instruction, AllowsRefusals

The CLICK v4 CONTINUE instruction, which skips to the next iteration of the loop.

Its binary class is Next, with func code 9232.

laddercodec.Send dataclass

Bases: Instruction, AllowsRefusals

A Modbus Send (SD) instruction.

shown_function_code property

shown_function_code: int | None

The function code that CLICK shows on the box.

It is function_code when that is set. Otherwise it is the default code that encoding writes. It is None when no default can be worked out, such as for a remote address that cannot be read. Reading it never raises.

from_csv_token classmethod

from_csv_token(token: str) -> Send

Parse the CSV token of a Send, such as send(target=ModbusTcpTarget(name="plc",ip="192.168.1.10"),remote_start="DS1",source=DS1).

read_csv reads the AF cell of a Send with the same parser. The method raises CsvReadError or FieldError, as read_csv does for the same AF cell.

laddercodec.SendFunctionCode module-attribute

SendFunctionCode = Literal[5, 6, 15, 16]

A Modbus function code that a Send can use.

laddercodec.Receive dataclass

Bases: Instruction, AllowsRefusals

A Modbus Receive (RD) instruction.

shown_function_code property

shown_function_code: int | None

The function code that CLICK shows on the box.

It is function_code when that is set. Otherwise it is the default code that encoding writes. It is None when no default can be worked out, such as for a remote address that cannot be read. Reading it never raises.

from_csv_token classmethod

from_csv_token(token: str) -> Receive

Parse the CSV token of a Receive, such as receive(target=ModbusTcpTarget(name="plc",ip="192.168.1.10"),remote_start="DS1",dest=DS1).

read_csv reads the AF cell of a Receive with the same parser. The method raises CsvReadError or FieldError, as read_csv does for the same AF cell.

laddercodec.ReceiveFunctionCode module-attribute

ReceiveFunctionCode = Literal[1, 2, 3, 4]

A Modbus function code that a Receive can use.

laddercodec.ModbusAddress dataclass

Bases: AllowsRefusals

A remote address given as a MODBUS 984 number or in hex, not as a CLICK address.

laddercodec.ModbusRegisterType module-attribute

ModbusRegisterType = Literal[
    "coil", "discrete_input", "holding", "input"
]

The register type of a Modbus address given in hex. A Receive reads the four types with function codes 1, 2, 3 and 4, in that order.

laddercodec.ModbusRtuTarget dataclass

Bases: AllowsRefusals

A Modbus RTU device on a serial port, which a Send or Receive talks to.

laddercodec.ModbusRtuPort module-attribute

ModbusRtuPort = Literal[
    "cpu2",
    "cpu3",
    "slot0_1",
    "slot0_2",
    "slot1_1",
    "slot1_2",
]

The serial port of a Modbus RTU target.

laddercodec.ModbusTcpTarget dataclass

Bases: AllowsRefusals

A Modbus TCP device at an IP address and port, which a Send or Receive talks to.

laddercodec.ModbusTcpPort module-attribute

ModbusTcpPort = Literal['ethernet', 'wlan']

The network port of a Modbus TCP target. "ethernet" is CPU port 1.

laddercodec.RawInstruction dataclass

Bases: Instruction, AllowsRefusals

An AF instruction kept as its blob, so that it is written back byte for byte.

Attributes

class_name: The class name in the blob, such as "Copy" or "Cnt". The CSV token shows it so that a person can read it. It is also inside blob. blob: Full instruction blob bytes (from cell offset +0x25 to the end of tagged fields, excluding tail). part_count: The number of parts: 1 for a single-row box, more for a multi-row box. The clipboard decoder reads it from the blob. So does the CSV reader for the hex form of the token, which uses 1 if the blob does not parse. The program file decoder counts the part rows of the entry, and the CSV reader takes it from the decoded-fields form of the token. Each of those two builds the blob with that count. rows, sub: The row bytes of the box's cell header. rows is the row span (+0x09), and sub is the sub-rows (+0x0A). The decoders keep the bytes of the cell that CLICK wrote. They leave a box raw when its stored row span is not the height of the typed instruction. If rows is None, the box takes its height from its class name and its blob. If the blob alone gives the height of the typed family, the box takes that height: the Copy family, Search, Send, Receive, Out variants, and 3 rows for Email, Home, Position and Velocity. Otherwise the box ends one row below the row of its last part, as the extra part bytes of the blob give it. A 1-part box is 1 row. Boxes made in CLICK's editor may store other sub-rows: Math 01 03, some Search 02 03, Tmr 02 03, Drum 04 05, SR 03 02. For those classes CLICK keeps the height that laddercodec writes. If rows is set and sub is None, the sub-rows equal the row span. The CSV token holds the two bytes that the box encodes (rows=, sub=). For a box of 2 or more parts, it also holds the extra part bytes (pins=). Two boxes are equal when they encode the same bytes, even if one has None and the other states the height that None gives.

cell_params

cell_params() -> dict

Return ClickCell kwargs intrinsic to this instruction.

part_rows

part_rows() -> tuple[int, ...]

Return the row of each later part, counted from the main row.

These rows are the part bytes of the blob. Return an empty tuple for a 1-part box, when the blob does not parse, and when the part bytes are not one ascending row from 1 to 31 for each later part.

build_blob

build_blob() -> bytes

Return the raw blob bytes (no-op — already stored).

to_csv

to_csv() -> str

Return the decoded-fields token raw(ClassName,0xTTTT,N,rows=R,sub=S,...).

rows and sub are the cell bytes that the box encodes (+0x09, +0x0A). A box of 2 or more parts adds pins=, its extra part bytes.

The method returns the hex form, with rows and sub, in these cases:

  • The blob does not parse into fields.
  • The part bytes are not ascending rows from 1 to 31, or the row span is not one more than the last of them.
  • The decoded-fields token does not read back as this box, because it can't hold one of the field values.

from_csv_token classmethod

from_csv_token(token: str) -> RawInstruction

Parse a raw CSV token in the decoded-fields form or the hex form.

Decoded-fields format::

raw(ClassName,0xTTTT,N,rows=R,sub=S,pins=P,field_specs...)

Hex format::

raw(ClassName,hex_blob,rows=R,sub=S)

rows (row span) and sub (sub-rows) come right after the part count, or right after the hex. Each is optional, and an absent one is None. If both are absent, the box takes its height from its class name and its blob. If only sub is absent, the sub-rows equal rows. pins is the extra part bytes joined by /. It is allowed only in the decoded-fields form, for a box of 2 or more parts. Without it, the part bytes are 01 02 .... If rows is given, it must be one more than the last pin.

laddercodec.Undecoded dataclass

Bases: Instruction, AllowsRefusals

A marker for a rung that laddercodec could not decode, write or strip, with the reason.

When a decoder cannot read a rung, it puts a marker rung in its place. The marker rung is one row, wired from the rail, with this marker as its output. The marker acts as end() in the main program and as return() in a subroutine, so a program never runs past a rung that could not be read. encode() refuses the marker, so it is never pasted into CLICK.

Attributes

error: Why the rung could not be decoded, written or stripped, on one line. rung: Index of the rung it stands for, from 0. row, column: Where in that rung the failure is, both from 0 (column 0 is A, 31 is AF), or None when the failure is not at one row or cell. stop: What the marker acts as: "end" in the main program, "return" in a subroutine. to_the_end: True when decoding stopped at this rung. The marker then stands for this rung and every rung after it.

where property

where: str

Where decoding failed, as rung N, row R, or rung N when row is None.

The rung and the row count from 0.

headline property

headline: str

The first line of the marker rung's comment.

It reads UNDECODED: CLICK rung N, row R: <error>. The rung and the row count from 1. CLICK counts them the same way. The headline leaves out , row R when row is None. When to_the_end is True, the error is followed by ; decoding stopped, so this marker stands for CLICK rung N to the end.

refusal

refusal() -> str

Return why encode() refuses the marker.

laddercodec.UndecodedStop module-attribute

UndecodedStop = Literal['end', 'return']

What an Undecoded marker acts as: "end" in the main program, "return" in a subroutine.

laddercodec.UnknownCondition dataclass

An instruction cell on a condition column (A..AE) that the decoder could not read.

raw holds the instruction's own bytes, from cell offset +0x25 to the cell boundary. When decode_program builds one, raw holds the class name instead. The class holds no wire flags. If the cell also has a wire down, the decoder wraps the UnknownCondition in a :class:Tee.

laddercodec.UnknownInstruction dataclass

An instruction cell on the AF column that the decoder could not read.

The decoder makes one only when it cannot find an instruction blob in the cell. A blob that no parser accepts becomes a :class:RawInstruction instead. raw holds the instruction's own bytes, from cell offset +0x25 to the cell boundary.

laddercodec.Email dataclass

Bases: Instruction, AllowsRefusals

The Email instruction, with each field kept as a string under its CLICK tag id.

laddercodec does not interpret these fields. Their names, such as tag_3218, are provisional, and a later release will rename them.

Binary class name "Email" (type marker 0x2737).

to_csv

to_csv() -> str

Return the raw(Email,...) token, as RawInstruction.to_csv returns it.

A field that the decoded-fields form can't hold, such as two recipients separated by a comma, makes it the hex form.

from_csv_token classmethod

from_csv_token(token: str) -> Email

Parse raw(Email,0x2737,1,...) token.

laddercodec.Home dataclass

Bases: Instruction, AllowsRefusals

The Home instruction. laddercodec does not interpret its fields, and keeps each as a string.

Each field is named after CLICK's tag id for it, such as tag_3218. These names are provisional, and a later release will rename them.

In the binary, its class name is "Home" and its type marker is 0x2734.

from_csv_token classmethod

from_csv_token(token: str) -> Home

Parse raw(Home,0x2734,1,...) token.

laddercodec.PositionMove dataclass

Bases: Instruction, AllowsRefusals

The Position instruction, with each field kept as the text that its tag stores.

laddercodec does not interpret the fields. Each field is named after CLICK's tag id (tag_3218). These names are provisional, and a later release will rename them.

Its binary class name is "Position" and its type marker is 0x2736.

from_csv_token classmethod

from_csv_token(token: str) -> PositionMove

Parse raw(Position,0x2736,1,...) token.

laddercodec.VelocityMove dataclass

Bases: Instruction, AllowsRefusals

A Velocity instruction. Each field holds the string of one stored tag, and this library does not interpret it.

The fields are named after CLICK's tag ids (tag_3218). The names are provisional, and a later release will rename them.

Binary class name "Velocity" (type marker 0x2735).

from_csv_token classmethod

from_csv_token(token: str) -> VelocityMove

Parse raw(Velocity,0x2735,1,...) token.

laddercodec.constant_text

constant_text(
    value: int | float | str, *, hex: bool = False
) -> str

Return the constant text for a Python value, as a Math or a Copy source takes it.

The text is the one that the instruction stores for that value when you build it:

  • an int is a decimal integer, -2147483648 to 2147483647 (-7), or with hex=True hex digits then h, 0h to FFFFFFFFh (255 is FFh);
  • a float is the float32 value nearest it, in the fewest significant digits that read back as that value and stay within float32, printed as laddercodec prints a real (1000.0, 0.5);
  • a str is a text constant in quotes ("AB"). A quote in the str is written as it is, so 'A"B' is "A"B". A str of one character outside printable ASCII is that character's code, $ and two hex digits ($0D).

A single Copy and a Fill accept the text for a real of at most 7 significant digits. CLICK keeps 7 digits in a Copy and a Fill, and saves a real with more digits cut to 7. A Math accepts the text for a real that CLICK's Math saves as the same value (:attr:~laddercodec.operands.math_real.MathReal.kept). A Math, a Copy or a Fill refuses any other real, and its message names the constant to write. The largest float32 value is 3.402823466E+38. Write it as 3.402823E+38 in a Math, a Copy and a Fill.

Two choices are provisional until CLICK's behaviour is captured. A real that 7 significant digits print in exponent form, from 1E+07 up or below 1E-04, is written in Python's G form (1.5E+10). A real that needs more than 7 digits keeps them (0.33333334).

When you build the instruction, the field's own check decides whether it takes the text, such as an int above int16, a hex value above FFFFh or a real in a hex Math. This function does not check that. A value that no constant spells raises ValueError: an int outside the ranges above, a real that is not finite or is outside the float32 range, empty text, text longer than 128 characters, or a character above $FF. A bool, a float with hex=True or any other type raises TypeError.

laddercodec.replace

replace(inst: _T, /, **changes: Any) -> _T

Return a copy of inst, an instruction or a Rung, with the fields in changes set.

An instruction is frozen, so call this to get one that differs. Name each field as a keyword, as for dataclasses.replace. A name that is not a field of inst raises TypeError.

A value that is equal to the field's current value is no change. A value that the constructor would write in another form, such as "ds1" for "DS1", is a change.

If any field changes, replace builds the copy from its values as if you built it new, and every check of the instruction runs. It also builds again each Modbus target or Modbus address in a field that holds a refusal. A refused value raises the :class:~laddercodec.FieldError that building the instruction raises. It raises even for a code that read_csv(..., allow=...) allowed when it read inst. The copy has no stored blob, so encode() writes the blob of its values.

If no field changes, replace raises no refusal. The copy has the stored blob of inst, and the same refusals as inst:

  • encode() writes the blob and cell rows that inst was decoded from;
  • if inst was read with allow= and its refusals were allowed, encode() writes the blob of the copy's values;
  • otherwise, if inst has no stored blob, encode() raises the first refusal that the copy holds.

allow is not a field, so replace(inst, allow=...) raises TypeError. inst does not remember the allow= that it was built with. To build a changed copy that keeps a refusal, call the constructor with allow=.

A Rung is frozen too. replace(rung, instructions=...) returns a copy of the rung with those instructions. On a Rung, no check runs. The copy holds the cells that you pass, and the cells of rung that you do not change, as they are. replace does not build them again.