docs: structures, enumerations and arrays in Python and C++ function blocks - #15
Closed
thiagoralves wants to merge 1 commit into
Closed
docs: structures, enumerations and arrays in Python and C++ function blocks#15thiagoralves wants to merge 1 commit into
thiagoralves wants to merge 1 commit into
Conversation
DOPE-584 gave native blocks full IEC type parity, and the docs still described the old world: base types and flat arrays only. Two new pages cover what a structure, an enumeration and an array actually look like on each side, because the two languages do not agree and the differences are silent when you get them wrong. Every example here was compiled and run on an SLM-RP4 (Runtime v4) through openplc-cli, with the values read back over the debugger. The findings that would not have survived guesswork: - Member spelling is opposite between the two languages. Python keeps the spelling from the Variables Table (`mot.speed`); C++ sees the compiler's uppercase (`mot.SPEED`). Getting this wrong in Python raises AttributeError at runtime, not at build time. - A Python list is indexed by the IEC index, so `ARRAY [1..2]` is a list of length 3 whose element 0 is None. Iterating it naively hands you a None. - A multi-dimensional array is `grid(i, j)` in C++, not `grid[i][j]`. - A scalar enumeration pin in C++ needs assigning to the raw enum first; `mode.get()` returns a wrapper that will not cast to int. An array element is already the raw enum and needs no such step. - A function block instance must be declared under VAR to be driven from Python. Declared as an Input its pins still arrive, but writes never reach the PLC, so a TON sits at zero with nothing to explain why. - DATE is a count of days while TIME, TOD and DT are nanoseconds. Also documents the missing-import failure mode, which reports itself as "PLC runtime has stopped." while the PLC is plainly still running: the wrapper's liveness check calls os.kill, `os` was never imported, and the NameError is reported as a stopped runtime. Corrects one claim on the existing C++ page: ordering comparisons work directly on STRING variables, without going through .get(). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01UaSZK4LqFWtZpERcqnZ8uQ
Contributor
Author
|
Folded into #14 — the two overlapped on Everything from this branch is now on #14 as |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two pages covering the types DOPE-584 unlocked for native blocks, and corrects what the existing pages got wrong. The docs still described the old world — base types and flat arrays only — while structures, enumerations, multi-dimensional arrays and arrays of structures all work now.
New pages
Everything here was run on hardware
Not written from reading the source. Each example was compiled and uploaded to an SLM-RP4 (Runtime v4) with
openplc-cli, and the outputs read back through the debugger — 35 C++ patterns and 26 Python checks. Several of the findings contradict what I would have written from the code alone:mot.speed); C++ sees the compiler's uppercase (mot.SPEED).AttributeErrorat runtime, not a build error. I hit this writing the examples.ARRAY [1..2]is a list of length 3 whose index0isNone.for row in grid:hands you aNonefirst.grid(i, j), notgrid[i][j]for multi-dimensional arrays in C++.[i][j]does not compile.MODE m = mode;first.mode.get()returns a wrapper that will not cast toint.VARto be driven from Python.DATEis days;TIME/TOD/DTare nanoseconds.D#1970-01-11→10,T#2s→2000000000.Also documents a failure mode that lies about itself: drop one of the four required imports and the block exits reporting "PLC runtime has stopped." while the PLC is plainly still running. The wrapper's liveness check calls
os.kill,oswas never imported, and theNameErroris reported as a stopped runtime. Easy to hit, since the imports look unused.Correction to an existing page
cpp-structure.mdsaid ordering comparisons on STRING need.get()on both sides. They work directly on the variables; verified by compiling both forms.Overlap with #14 — please read
#14 is open and touches two of the same files. It documents shared globals and function block instances, and its C++ instance section is correct — including that
ton0();is what makes the timer advance, which my hardware run confirms independently.This branch is cut from
main, so it does not include #14. Consequences:cpp-structure.mdandpython-restrictions.mdare small but sit near docs: native function blocks — globals, instances, and the full IEC type surface #14's edits, so whichever merges second will need a trivial rebase.Not covered
STRING[n](a declared length) is still the transport's 126-character budget, so the docs state the cap rather than the declared length. Deferred to strucpp by DOPE-584.ARRAY [-10..10]) are legal IEC 61131-3 but rejected project-wide by both the parser and the variables table, so they are not documented as usable.🤖 Generated with Claude Code
https://claude.ai/code/session_01UaSZK4LqFWtZpERcqnZ8uQ