IMDHMIControls LibraryShop main store

How-to

Pass a PLC byte array to a C++ module via its TMC data area

PLC programming Published 9 min read

Short answer

A TwinCAT 3 PLC compiler refuses array arguments in an external function call with the error 'Structured value types not allowed in external function calls'. Pass the array instead through an Input-Destination data area declared in the module's .tmc file: declare a byte array member there, rebuild, instantiate the module, link the data area to a matching PLC array, and activate the configuration.

On this page
  1. Why does the PLC compiler reject the array in the call?
  2. How does a TMC data area move the bytes instead?
  3. What type and size must the PLC array use?
  4. How do you add the Input - Destination area to the .tmc file?
  5. How do you link the PLC array to the module instance?
  6. How do you prove the link with a pattern test?
  7. What breaks the mapping most often?
  8. Can you send a structure instead of a flat byte array?
  9. Questions asked

The fix for 'Structured value types not allowed in external function calls' is to stop passing the array as a call parameter and map it instead: declare an Input - Destination data area in the module's .tmc file, give it a byte-array member, link that member to a matching PLC array at the module instance, and activate the configuration. The call interface only carries elementary values; the data area carries everything cyclic. This article sizes the array, walks the TMC edit, the link and the activation, and proves the result with a one-element pattern test.

Why does the PLC compiler reject the array in the call? #

The build fails because a call from structured text into a C++ module is an external function call, and the compiler only permits elementary value types as arguments, so it rejects arrays and structures with the message 'Structured value types not allowed in external function calls'.

The message is raised while the XAE builds the PLC project, before anything reaches the runtime, so no runtime fault or ADS error accompanies it: the build output is the whole diagnostic. Two paths exist for moving the bytes and only one of them compiles. An array argument on the external call is refused. A data area declared in the module description works, because the bytes never cross the call interface at all - they cross a memory mapping that both sides touch every cycle. The decision is fixed at that point: keep BOOL, INT and REAL values as call parameters, and move the array through the data area.

How does a TMC data area move the bytes instead? #

The module declares an Input - Destination data area in its .tmc file, the configuration links that area to a PLC variable, and each cycle the PLC writes its array while the module reads those same bytes as its own input, with no function call involved.

The .tmc file is the module description file: it carries everything about the module that the TwinCAT system itself sees, and the TMC editor edits it. Its data areas are classified into input and output and, within that, into source and destination; input areas are what the module receives, and the wizard that generates a new C++ module already creates an Inputs area of the type Input - Destination for exactly this job. The exchange is cyclic rather than event-driven: the PLC task writes in its pass and the module reads in its own, the same write-then-read order the guide to what the PLC scan cycle does in each pass describes inside the PLC. Use the data area for every cyclic exchange and every array, and keep the call interface for scalars.

What type and size must the PLC array use? #

Match element type and element count exactly: the worked interface here is ARRAY[1..128] OF BYTE, 128 elements at 1 byte each, a total of 128 bytes, and the TMC member must total the same 128 bytes or the configuration refuses the link.

The link maps memory, not logic: a BYTE occupies the same 1 byte on both sides, so element type and element count decide the mapping, and the total length is element count times element size. Declare the PLC array in a global variable list or in a program variable that a task actually uses, so it lands in the task image and is offered for linking. The index base is notation, not mapping: ARRAY[0..127] OF BYTE covers the same bytes as the 1-based declaration.

Worked example, a 128-byte interface:

  • PLC side: ARRAY[1..128] OF BYTE - 128 elements of 1 byte each = 128 bytes total.
  • TMC side: the member declared as a byte array of the same element count, so the two totals both read 128 bytes and match.
  • Offset rule: element n sits at byte offset n - 1, so element 37 sits at offset 36.
  • Mismatch case: a TMC member of 64 elements totals 64 bytes against the PLC side's 128, and the configuration refuses the link; element count times element size is the whole check.

Decision: size both sides from the same number and change them together; there is no spare capacity to declare, because any difference is a refusal, not headroom.

How do you add the Input - Destination area to the .tmc file? #

Open the module's .tmc file from the TMC Files folder, select the Data Areas node in the TMC editor, add an area with the type Input - Destination, name it, give it your array type, then rebuild the C++ project so the generated headers include it.

  1. In the C++ project, open the folder TMC Files and open the module's .tmc file in the TMC editor.
  2. Select the module's Data Areas node and create a new data area with the + button.
  3. Set the area type to Input - Destination; the module wizard has usually already created an Inputs area of this type, so extend it unless the new block needs a name of its own.
  4. Add a variable to the area, name it, and declare its type as a byte array with the element count fixed in the sizing section.
  5. Rebuild the C++ project so the generated class and interface headers include the new member.
  6. Read the bytes in the module's cyclic code through that generated member.

Edit the file in the TMC editor rather than by hand where you can: the editor writes the XML the runtime expects, and a malformed hand edit stops the module from loading. Treat the .tmc file as the single definition of the interface; the module instance is described by it, so every link the PLC project makes is made against this file.

Build the PLC project first, then link the PLC variable to the module instance's input in the solution tree, and finish with Activate Configuration, which writes the links into the running system.

  1. Create the module instance under the C++ node of the solution tree.
  2. Declare the PLC array to match the worked interface - same element type, same element count - in a GVL or a program variable.
  3. Build the PLC project; the variable then appears under the PLC instance's Outputs in the solution tree, because the PLC writes it.
  4. Open the module instance and expand its Inputs; the data area member from the .tmc file appears there as a linkable input.
  5. Create the link from the PLC variable to that input.
  6. Run Activate Configuration to commit the mapping to the runtime.

Activate Configuration reloads the runtime and pauses the PLC task while it does, so activate from a state in which a paused task cannot move anything.

If the member shows under the instance's Outputs instead of its Inputs, the direction in the .tmc file was set wrong: fix the type, rebuild the C++ project, and refresh the instance. The state to finish in is one link, PLC variable on the module input, configuration activated; anything not activated is not mapped.

Write a known pattern into the PLC array, read the module side online, then change a single element: when only byte offset 36 moves after element 37 changes in the worked array, the mapping is proven correct at that offset.

  1. Fill each element of the PLC array with its index minus one: the first element gets 0 and the last gets 127, so every byte in the area is unique and a shift of even one position shows immediately.
  2. Go online with the module instance and read the linked input: it must read 0, 1, 2 and onward in order.
  3. Change only element 37, to 16#A5. The arithmetic says element 37 sits at byte offset 37 - 1 = 36, so exactly the byte at offset 36 changes on the module side and nothing else moves.
  4. If a whole block shifts, something sits ahead of the array, such as a prefix member or padding; if nothing changes, the configuration is not activated or the PLC task is not in run.

A breakpoint or a trace output in the module's cyclic code verifies the same way: dump the received buffer and compare it element by element. The pass condition is one element changed in, one byte changed out, at the offset the arithmetic predicts; anything else is a mapping fault to fix, not a limit of the data-area approach.

What breaks the mapping most often? #

Most failures are project state, not protocol: a TMC edit without a rebuild, a byte-count mismatch between the two declarations, or a link made before Activate Configuration. Work the table from top to bottom and the common cases are covered.

Symptom Likely cause What to do
The new input does not appear on the module instance The TMC was edited but the C++ project was not rebuilt, or the instance was not refreshed Rebuild the C++ project, then refresh or recreate the instance
The link is refused Element type or total byte count differs between the two declarations Recompute element count times element size on both sides and match them
The module reads all zeros The configuration was not activated after linking, or the PLC task is not in run Activate the configuration and confirm the PLC is running
Values sit one cycle behind The module task and the PLC task differ in cycle time or phase Compare both task configurations and decide which latency the interface needs
The PLC variable is not offered for linking The variable sits outside the task image Move it to a GVL or a program variable that a task uses

The first four rows cover nearly every failure a first integration hits, and the fifth is the declaration placement from the sizing section.

Can you send a structure instead of a flat byte array? #

Yes, if the TMC defines the identical layout and both sides agree on alignment, but the flat byte array is the safer default because it carries no padding, so keep the structure only when the module genuinely needs typed fields.

Alignment can insert padding between members, so the sum of the member sizes is not the total: check the total with sizeof on the C++ side against the declared byte count in the TMC before linking, and if any member is wider than a byte, confirm both compilers place it at the same offset. Keep the two declarations next to each other in your notes so a layout change edits both at once.

The decision: commit to the flat byte array for PLC-to-module bulk data, because it has no padding to disagree about and the pattern test proves it end to end; reserve typed structures for interfaces where you own both declarations.

Questions asked

How do I move data from the C++ module back to the PLC?

Declare a second data area in the .tmc file with the output direction, give it its own named member, and link it to a PLC variable at the instance; the link then runs module to PLC instead of PLC to module. The module wizard creates Inputs areas of the type Input - Destination, so pick the output kind for this side, rebuild, and make the links again.

Does a linked data area provide a handshake or a data-valid bit?

No. The mapping is a cyclic byte copy with no handshake, sequence number or validity flag, so the receiver cannot tell a repeated value from fresh data. If freshness matters, put your own marker inside the array: an incrementing counter in the last two bytes, for example, checked by whichever side must detect new data.

Can two PLC tasks write into the same linked array?

Keep one writer per data area. The mapping copies bytes every cycle and holds no arbitration between writers, so two tasks writing one area interleave in task order rather than program order, and the result depends on phasing you do not control. Split the interface into two areas, one per task, when two code paths must both send data.

What happens to existing links when the module code is updated?

The interface lives in the .tmc file, so treat a module update as an interface change: rebuild the C++ project, reopen the module instance, and confirm each link before you activate again. A member that keeps its name and its total byte count normally keeps its link; a renamed or resized member does not, and that link must be made again.

Can I still pass scalar values as call parameters?

Yes. The restriction covers structured value types such as arrays and structures as arguments; elementary values like BOOL, INT and REAL still cross the external call interface. A workable split is to keep commands and setpoints as call parameters and move anything cyclic or larger than a few bytes through the data area.

Keep reading

  • How-to

    Trigger a Raspberry Pi camera capture from CODESYS

    Trigger the Pi camera from CODESYS by starting from the shipped Camera.project example, passing only the arguments (no raspistill prefix), and resetting the trigger Boolean on the done feedback. The JPEG lands in /root/ and comes out through the CODESYS file browser; stop the camera stream first, because the camera is a single-owner device.

    PLC programming

  • Explainer

    Why CODESYS package libraries will not import into SoMachine

    SoMachine and EcoStruxure Machine Expert ship without the package manager that installs CODESYS Store package downloads, so the import cannot be forced from inside the tool. The supported route is source-level reuse: copy the function block source, its data types and its dependencies into the Schneider project, compile on the target, and keep the controller on its documented Schneider tool.

    PLC programming

  • Comparison

    Which IEC 61131-3 language fits a cyclic machine controller

    For a cyclic machine controller such as a telescope dome, write the control in structured text or ladder on the native IEC 61131-3 cyclic task and add SFC only for the step sequence. The interlock must be ANDed into each output every scan, so a lost permissive clears the drive command within one sweep, typically 10 to 20 ms. Reserve a TwinCAT C++ module for numeric work structured text cannot express, not for the interlock layer.

    PLC programming