Skip to content

Data Movement

For an introduction to the DSL vocabulary, see Core Concepts.

copy — copy single value

copy(Setpoint, DS[1])               # Copy tag to tag
copy(42, DS[1])                     # Copy literal to tag
copy(DS[1], DS[DS[0]])              # Indirect addressing: DS[pointer]
copy(DS[1], DS[1], oneshot=True)    # Execute only on rung rising edge

copy(), blockcopy(), fill() and pack_text() clamp an out-of-range value to the destination type's min/max and set system.fault.out_of_range. A Real or Inf into a Dint clamps with no flag. A NaN stores 0 in an Int and -2147483648 in a Dint. This is different from math(), which wraps.

copy() does not clamp in two cases:

  • A Word into an Int keeps its bits (65535 is -1).
  • A negative number from -32768 to -1 into a Word keeps its low 16 bits (-1 is 65535). Anything below -32768 stores 32768.

CLICK's editor accepts no constant into a bit, so the CLICK export rejects copy(2, C[1]) and fill(1, C.select(1, 4)). Set bits with out(), latch() or reset().

A pointer (DS[Ptr]) or an index expression (DS[Ptr + 1]) may use only Int and Dint tags, integer constants and integer operators (// rather than /). The same rule applies to the bounds of a select() that you give tags or expressions. Anything else raises TypeError when you build the program. Copy a Word or Real into an Int first.

A pointer that falls below the block sets system.fault.address_error, and the destination keeps its value. This applies whether the bad address is the source or the destination. Past the block's last address, a read gives zero and a write is dropped. No flag is set. A select() with tag or expression bounds outside the block, or with its start past its end, sets address_error and writes nothing. shift() and search() do the same.

Each math() or copy instruction clears out_of_range and address_error when it runs, then sets the one it trips. A math() that does not run (rung false, or a one-shot that does not fire) clears them too. A copy instruction that does not run leaves them alone. Coils, timers and counters also leave them alone. Read a flag on the rung right after the instruction.

blockcopy — copy a range

blockcopy(DS.select(1, 10), DS.select(11, 20))   # Copy DS1..DS10 → DS11..DS20

Named arrays also support whole-instance copies:

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

Source and destination ranges must have the same length.

blockcopy(DS.select(1, 3), DS[11])               # Copy DS1..DS3 → DS11..DS13

A single tag as the destination gives the first address. The copy writes as many elements as the source has, as a CLICK does. If they would run past the end of the block, pyrung refuses the program when you build it.

Into a TXT range, blockcopy() reads only a TXT range and takes no convert=. CLICK's Block Copy does the same. To write the text of numbers, copy() each number into its TXT.

fill — write constant to range

fill(0, DS.select(1, 100))          # Zero out DS1..DS100
fill(Setpoint, Alarms.select(1, 8)) # Copy tag value to all 8 elements
fill(5, Txt.select(1, 3))           # Txt1..Txt3 = "5"

An integer constant filled into a TXT range writes its text, the same as a plain copy() into one TXT. Each TXT holds one character. So fill() refuses a value of more than one character, such as 12 or "AB", as CLICK's editor does. To write text across a range, copy() it into the first TXT. Each character goes into the next TXT.

fill() refuses a number register or an expression as the value for a TXT range, as CLICK's editor does. Copy the value into one TXT with copy() instead.

fill(True, ...) and fill(False, ...) write "1" and "0" into a TXT range. copy() and fill() refuse a Bool tag into TXT, as CLICK's editor does. copy() also refuses True or False into a TXT. Copy the text "1" or "0" instead.

fill(1.5, Txt.select(1, 4))   # Txt1..Txt12 = "1111.5000000"

A Real constant into a TXT range writes its text with 7 digits after the point, as a CLICK does. Every TXT in the range gets the first character, and the rest of the number trails off past the end.

Here the text also changes Txt5..Txt12. validate_click_program() and the export warn about it. pyrung drops the characters that run past the end of the block or past TXT1000. The constant keeps 7 significant digits, the same as the export writes it. The range needs two or more TXTs and cannot use a pointer. To write a number's text into TXTs, copy() it into the first TXT with convert=to_text() instead.

fill(0xF, Txt.select(1, 4), hex=True)   # Txt1..Txt4 = "F"

This is CLICK's Fill of the hex constant Fh. Only CLICK 4.00 or later accepts it. hex=True takes one hex digit from 0 to 0xF, and only into a TXT range. hex=1 does the same. A larger number is refused, because CLICK's Fill takes one digit. hex=True refuses 0xE, because CLICK's Fill reads Eh as the real 0.0. Write fill("E", ...) to fill "E". A CLICK Fill of Eh imports as fill(0.0, ...).

Type conversion (copy converters)

Copy converters convert between numeric and text registers, and to_binary also moves bits between a Real and a Dint. They offer the same options as the CLICK PLC Copy Single dialog. Pass them as the convert argument to copy().

Text → Numeric

copy(ModeChar, DS[1], convert=to_value)    # CHAR '5' → numeric 5   (Copy Character Value)
copy(ModeChar, DS[1], convert=to_ascii)    # CHAR '5' → ASCII 53    (Copy ASCII Code Value)

to_value of a letter or other non-digit sets the out-of-range fault and leaves the destination alone. A blank TXT reads as 0.

A copy() or blockcopy() of a TXT into a number with no convert is to_value. A copy() of a number tag or a number constant into a TXT with no convert is to_text(). So copy(-5, Txt[1]) writes "-" and "5".

The CLICK export writes the converter that pyrung runs, because CLICK has no plain Copy between TXT and a number. A copy() of a TXT into a DF takes no convert. CLICK offers no converter there, and the export writes a plain Copy.

Numeric → Text

copy(DS[1], Txt[1], convert=to_text())                       # "123"           (Suppress zero)
copy(DS[1], Txt[1], convert=to_text(suppress_zero=False))    # "00123"         (Do not Suppress zero)
copy(DF[1], Txt[1], convert=to_text(exponential=True))       # "1.0000000E+04" (Exponential Numbering)
copy(DS[1], Txt[1], convert=to_text(termination_code=0))       # "123" + NUL     (Termination Code)
copy(DS[1], Txt[1], convert=to_text(termination_code="$0D"))   # "123" + CR      (Termination Code, hex)
copy(DS[1], Txt[1], convert=to_binary)                         # raw byte: 123 → '{' (Copy Binary)

To write an integer constant in hex, pass hex=True:

copy(0xAB, Txt[1], convert=to_text(hex=True))                        # "AB"
copy(0xAB, Txt[1], convert=to_text(hex=True, suppress_zero=False))   # "00AB"

In CLICK this is a Copy of the constant ABh. hex=True accepts only an integer constant from 0 to 65535 (0xFFFF). Without it, copy(0xAB, Txt[1]) writes "171".

You can export a constant with an E in its hex digits, such as 0xAE. The export writes 0xE as 0Eh and 0x1E as 01Eh. If you import a CLICK program that has such a constant, pyrung runs it as hex.

To export a set number of hex digits, pass that number instead of True:

copy(0xF, Txt[1], convert=to_text(hex=4))   # "F", exported as 000Fh

The number changes only what the export writes. The Copy still writes "F". The number must cover every digit of the constant, so hex=1 refuses 0x1E. When you import a CLICK constant with leading zeros, such as 000Fh, the import sets hex=4. The export then writes 000Fh again.

A Word is written in hex, so to_text() of 0x00AB is "AB", and "00AB" with suppress_zero=False.

copy(0x41, Txt[1], convert=to_binary(hex=True))   # "A"

to_binary(hex=True) stores the same character as to_binary. The CLICK export writes the constant in hex, as 41h. to_binary(hex=4) exports 0x41 as 0041h. An imported Copy Binary of a hex constant gets hex=True or a number of digits, so it exports unchanged. CLICK counts one TXT for each character of the constant, so FFFFh (5 characters) can start no higher than TXT996.

copy(DS[1], Txt[1], convert=to_binary(termination_code="$0D"))   # 123 → '{' in Txt1, CR in Txt2

With a termination code, to_binary writes the byte into the TXT and the code into the next TXT. It writes no other TXT. The code takes the same values as in to_text(). A value above 255 writes only its low byte. For example, 0x4142 writes "B" and sets no flag. A CLICK does the same. A termination code needs a TXT target, so copy() refuses it into any tag that is not a Char.

exponential applies to a Real source. suppress_zero applies to any other source. This matches CLICK's Copy dialog. A Real source ignores suppress_zero. The CLICK export rejects exponential=True from a source that is not a Real.

copy() refuses an expression into a TXT, such as copy(DS[1] + 1, Txt[1]), with or without convert=. CLICK takes an expression only in Math. Store the result in a number tag with math(), then copy that tag into the TXT.

termination_code appends a single ASCII character after the converted text. Pass an int (0–127), a one-character string, or a $XX hex string matching CLICK's native notation (e.g. "$0D" for carriage return). This matches the CLICK PLC Termination Code option (C0-1x and C2-x CPUs).

Leading zeros with string literals

In CLICK's programming software you can type 00026 directly into the source field to copy fixed-width text into text registers. Python won't allow leading zeros on integer literals, so 00026 is a syntax error. Use a string instead:

copy("00026", Txt[1])          # Txt1..Txt5 = "0", "0", "0", "2", "6"

A copy() into a TXT range such as Txt.select(5, 6) writes from the range's first TXT, the same as a copy into Txt[5]. A text longer than the range writes on past its end.

blockcopy and fill

blockcopy() supports convert= but only for text→numeric conversions (to_value and to_ascii). This matches CLICK PLC hardware, which limits block copy to those two modes.

blockcopy(CH.select(1, 3), DS.select(1, 3), convert=to_value)
blockcopy(CH.select(1, 3), DS.select(1, 3), convert=to_ascii)

fill() does not support convert=.

Converter reference

Converter Direction CLICK PLC equivalent copy blockcopy fill
to_value Text → Numeric Copy Character Value (Option 4b) yes yes no
to_ascii Text → Numeric Copy ASCII Code Value (Option 4b) yes yes no
to_text() Numeric → Text Copy Option 4a / 4c yes no no
to_binary Numeric → Text, Real ↔ Dint Copy Binary (Option 4a) yes no no

copy() refuses a converter that cannot store into its target, such as to_value into a Char. to_value and to_ascii write an Int, Dint or Word. to_text() writes a Char. to_binary writes a Char, a Dint or a Real.

copy(DF[1], DD[1], convert=to_binary)   # 2.5 → 1075838976 (0x40200000)
copy(DD[1], DF[1], convert=to_binary)   # 1075838976 → 2.5
copy(1.5, DD[1], convert=to_binary)     # 1
copy(40000, DF[1], convert=to_binary)   # 40000.0

Between a Real and a Dint register, to_binary moves the 32 bits unchanged. From a constant, it stores what a plain copy() stores. CLICK's Copy dialog offers Copy Binary into a number only from these sources, and copy() refuses any other:

  • into a Dint: a Real register, or a Real constant, such as 1.5
  • into a Real: a Dint register, or an integer constant outside -32768..32767

On the P1AM-200, a NaN copied from a Real into a Dint stores -1 (0xFFFFFFFF). CircuitPython's floats can't hold a NaN's bits.

copy() also refuses these pairs, because CLICK's Copy dialog does not offer them:

  • to_value or to_ascii from anything but a TXT
  • to_text() or to_binary from a TXT or a text constant
  • to_binary from a Real register or a Real constant into a TXT. Use to_text() instead.

to_value and to_ascii accept no arguments. Pass them bare, with no parentheses. to_binary accepts only hex= and termination_code=, and to_binary() also works. to_text() accepts keyword arguments for formatting options.

Pack / unpack

pack_bits(C.select(1, 16), DS[1])                         # C1 -> bit 0, C16 -> bit 15
unpack_to_bits(DS[1], C.select(1, 16))                    # bit 0 -> C1, bit 15 -> C16

pack_words(DS.select(1, 2), DD[1])                        # DS1 = low word, DS2 = high word
unpack_to_words(DD[1], DS.select(1, 2))                   # DD low word -> DS1, high word -> DS2

pack_text(Txt.select(1, 4), DH[1])                        # "ABCD" -> 0xABCD
pack_text(Txt.select(1, 6), DF[1])                        # "1e-2" -> 0.01
pack_text(Txt.select(1, 3), DS[1], allow_whitespace=True) # " 12" -> 12
  • pack_bits() packs a BOOL range into an INT, WORD, DINT, or REAL. Use up to 16 bits for INT/WORD, up to 32 bits for DINT/REAL. REAL destinations use the raw IEEE-754 bit pattern.
  • unpack_to_bits() reverses that mapping. REAL sources are unpacked from their raw IEEE-754 bit pattern.
  • pack_words() packs exactly two INT/WORD tags into one DINT or REAL. The first source is the low word.
  • unpack_to_words() reverses that mapping. It requires exactly two INT/WORD destinations.
  • pack_text() parses a TXT/CHAR range into an INT, WORD, DINT, or REAL.
    • INT/DINT parse signed decimal and stop at a decimal point ("12.5" is 12). Any other character that is not a digit sets the out-of-range fault and leaves the destination alone.
    • WORD parses hex, and REAL parses float text including exponential notation. A number past the Real range stores +Inf or -Inf and sets no fault.
    • Whitespace around the text sets the same fault unless you pass allow_whitespace=True.
  • All pack/unpack instructions accept oneshot=True.
  • There is no unpack_text(). To write a text literal into TXT/CHAR memory, use plain copy("HELLO", Txt[1]). It fans out across sequential tags and is cut at the end of the block. For numeric → text, use copy(..., convert=to_text()) or copy(..., convert=to_binary).