IMDHMIControls LibraryShop main store

Explainer

Why a Linux C++ app must not share KBUS with CODESYS on a PFC200

PLC programming Published 8 min read By IMDHMI controls team

Short answer

On a PFC200, CODESYS and a Linux C++ program cannot both open the KBUS that runs the local I/O modules — one bus, one owner. Keep I/O acquisition and interlocks in CODESYS and hand values across the boundary with shared memory or a C-compatible library built from the BSP example, so the cyclic task's execution time stays bounded and the C++ process sends its JSON to the server on its own schedule.

On this page
  1. One KBUS has one owner
  2. Five shortcuts that break instead of helping
  3. Publish cyclic values through shared memory
  4. Hand commands over with a C-compatible library
  5. Use messages instead of memory
  6. What the boundary costs in time
  7. Build the C++ side against the matching BSP
  8. The decision, case by case
  9. Questions asked

The KBUS that links a PFC200 controller to the I/O modules behind it has one owner at a time. If a CODESYS 3.5 project runs the I/O on your 750-8206, a Linux C++ application on the same controller must not open that bus itself — it exchanges data with the PLC program instead. The workable architecture: CODESYS samples the inputs, evaluates the interlocks and drives the outputs, and publishes values through shared memory or a small C-compatible library; the C++ process reads them, does its own processing and posts JSON to the server on its own clock; commands travel back as requests the PLC validates before any output moves. Below: why the bus cannot be shared, what each interface costs, and the numbers that decide whether the design holds.

One KBUS has one owner #

On the 750-8206 with the 750-530, 750-430 and 750-600 modules mounted directly behind the CPU, KBUS is the controller-internal bus that carries all I/O traffic. Whichever program services it builds the process image: it samples the input modules cyclically, writes the output modules cyclically, and publishes the result as the I/O the control logic reads.

That servicing role is exclusive. On this platform the owner can be the CODESYS 3.5 runtime, an alternative runtime such as FORTE, or a custom KBUS application — one of them, never several at once. A C++ program that opens KBUS beside a running CODESYS project does not create a faster route to the modules; it puts two writers on one bus and leaves two conflicting versions of the process image on the machine, and neither one can be trusted.

Opening KBUS from C++ is therefore a different architecture, not a coexistence mode: the custom application replaces the PLC runtime as bus owner, which suits a device that will never run a PLC program. Once a CODESYS project owns the I/O, the C++ side never touches the bus; everything it needs crosses the boundary as data.

Five shortcuts that break instead of helping #

Every shortcut in this table skips the interface definition and pays for the skip somewhere worse.

Shortcut What breaks Working replacement
Open KBUS from C++ while CODESYS runs Two bus owners, two process images, outputs nobody can trust CODESYS stays owner; values cross the boundary as data
Post JSON from inside the cyclic PLC task One slow server consumes hundreds of task cycles Sockets, retries and timeouts live in the Linux process
Both sides write shared variables ad hoc Torn multi-field updates; fresh and stale values indistinguishable Fixed layout, one writer per field, sequence counter
Copy a generic ARM binary onto the controller Wrong ABI or missing libraries; loader refuses, runtime may be disturbed Build against the BSP that matches the firmware
Hand a C++ class to CODESYS directly The runtime expects a C-compatible interface delivered as a library, not objects Follow the BSP library example; keep the surface small

The first row is the exact question this article answers. The second is the failure mode that shows up weeks into operation, when the server gets slow and the machine changes character.

Publish cyclic values through shared memory #

CODESYS publishes a shared memory communication example project for exactly this boundary; its Linux-side file is myshm.c, and it shows a controller process and an ordinary Linux process exchanging data through a memory segment. Publishing a block of PLC values for an outside consumer is the same pattern as exposing PLC values to an OPC server through guest memory blocks, with the C++ program sitting in the consumer's seat.

Keep the layout fixed and the writing rules explicit. One workable layout:

Field Type Writer Meaning
seq 32-bit counter CODESYS increments once per publish
di 16-bit word CODESYS digital input image
value 32-bit integer CODESYS the defined value once both inputs are high
cmd 32-bit integer, 0 when idle C++ requested output command
cmd_sts 8-bit code CODESYS accepted, rejected or superseded

Field names and types here are an illustrative layout, not a documented API. CODESYS writes the data fields first and seq last, once per task cycle; the C++ process maps the segment, writes only its own fields, and treats the block as fresh when seq advances between two reads spaced by more than one task cycle. After a runtime restart the counter resets, the C++ side sees it stall or run backwards, and it holds off issuing commands until the counter moves forward again.

Hand commands over with a C-compatible library #

The board support package for the PFC200 line includes the example codesys_lib_demo-0.1, which demonstrates providing a library that a CODESYS project can call. That is the second working interface: compile the C++ side into a native library with a C-compatible surface — flat functions, simple types, no exceptions or objects across the boundary — and let the PLC task call it where it needs a value or hands one over.

The discipline that matters: a function called from the PLC task runs inside that task, so its work is bounded by design. Copy a request out of a queue, copy a status in, return. No socket, no wait, no retry loop inside a PLC-callable function.

Use messages instead of memory #

When the C++ side may later move off the controller, or you prefer a standard protocol at the boundary, run a Modbus TCP client in the C++ process against the CODESYS runtime over the controller's own loopback interface. The manufacturer documents PFC200 controllers operating as Modbus master and slave, so the runtime-side role is a documented capability, and what you must configure on the PLC side for Modbus TCP covers the server setup the runtime needs. The cost per exchange is a transaction round trip plus a timeout — which is exactly why the client call stays in the Linux process on its own schedule, never inside the cyclic task.

What the boundary costs in time #

Two computed numbers decide whether the design holds: the cyclic task's worst-case execution time and the age of the data the C++ side acts on.

Blocking work inside the task. Take an illustrative task period of 10 ms with an execution budget of half a period, 5 ms, and a JSON post sitting in the cyclic logic behind a server timeout of 2,000 ms. One call that hits the timeout holds the task for 2,000 ms / 10 ms = 200 cycles; that single cycle overruns its 5 ms budget by 1,995 ms, and every output the task drives stops updating for the whole timeout. Moving the socket to the Linux process leaves the PLC side one bounded memory write per cycle instead.

Data age through shared memory. Keep the same 10 ms task period, with CODESYS sampling and publishing once per cycle, and let the C++ process read at the top of a 50 ms loop. Worst case an input changes just after a sample: up to 10 ms to detect and publish, plus up to 50 ms until the C++ process acts, so 10 ms + 50 ms = 60 ms worst-case age. A consumer that accepts 100 ms old data is served; a 25 ms requirement rejects 60 ms, and the fix sits on the consumer side: match the C++ loop to the task period and the sum halves to 20 ms, which clears 25 ms with margin. The PLC cycle stays where the control loop needs it.

That second calculation is also the sanity check on the whole boundary: if no poll interval satisfies the requirement, the requirement belongs inside the PLC program, not across the exchange. The same discipline appears in isolating a blocking CAN library in its own real-time task: the wait lives outside the deterministic part of the machine.

Build the C++ side against the matching BSP #

The C++ side only runs if it is built for the controller. The board support package from the manufacturer carries the cross-compiler setup, the target headers and libraries, the hardware-access documentation and the example projects — codesys_lib_demo-0.1 among them — and you obtain the package matching your installed firmware through the manufacturer's official support channels. One documented generation of that line targeted Linux 3.6.11 with glibc 2.16 and the armhf ABI; treat those versions as a historical baseline to check against, not the identity of your installation. Read the firmware and runtime versions off the controller, then pick the matching package.

Bring the environment up in stages: record the model, firmware and runtime versions; cross-compile a minimal executable against the package and run it by hand through the documented deployment mechanism; add the shared-memory or library code only once that binary starts cleanly. A prebuilt ARM binary works only when its ABI and libraries match the target, and dropping libraries into the controller's system directories can disturb the runtime — use the libraries the package provides, where it documents them.

The decision, case by case #

CODESYS runs the I/O and logic, C++ posts JSON — the case in hand. CODESYS owns KBUS. Cyclic values cross through shared memory, commands come back as validated requests, and the socket conversation with the server lives entirely in the Linux process.

C++ must command outputs. It writes the request field or calls the bounded library function; the PLC validates the request against operating mode and interlocks, drives the output through its own logic, and reports accepted, rejected or superseded. One authority touches the outputs, and the PLC can say why a request did nothing.

No PLC program wanted. The C++ application becomes the KBUS owner, replaces the runtime, and the interlocks are rewritten in C++. That is a deliberate architecture for a device that never runs a PLC program — not a hybrid to attempt while a runtime is installed.

Interlocks and emergency-stop behavior that protect people or machinery stay in the CODESYS program or in the wired safety circuit, never in the C++ process across the exchange.

Questions asked

Can the C++ application open KBUS when CODESYS is stopped, and hand the bus back later?

Only by explicit arrangement, never concurrently. Stopping the CODESYS runtime releases the bus, and a custom application can then service the modules itself; the handover must be one-directional, so while the runtime holds KBUS the C++ side never opens it, and while the C++ side owns the bus the runtime must not start. There is no arbitration between two live owners, which is why the exchange architecture exists at all.

How do I verify the real data age at commissioning?

Measure it rather than trust the estimate. Toggle one input, timestamp the change in CODESYS and the reaction in the C++ process, and record the worst case over a few hundred repetitions with the server under load. Compare the measured age against the consumer's requirement and adjust the C++ poll interval accordingly; the estimate of one task period plus one poll interval tells you what to expect, and the measurement tells you what you actually have.

What happens to the exchanged data when the CODESYS runtime restarts?

The sequence counter resets, so the C++ side sees it stall or move backwards, discards the block and waits until it advances again over two reads spaced by more than one task cycle; that handshake is the whole recovery procedure. The output modules take whatever initial state the PLC program applies at startup, so the C++ side should re-read the input image and reconcile any pending commands instead of assuming the pre-restart state persists.

Could CODESYS build the JSON and post it itself?

It can assemble the text, since JSON is string work and PLC string handling copes with small payloads. What it must not do is hold the cyclic task open while the socket transaction runs: a slow server turns one post into hundreds of missed task cycles, and no protocol choice makes a synchronous wait deterministic. Assemble data in the PLC if that is convenient, and let the Linux process own the connection, the retries and the timeout.

Can shared memory and the C library run side by side?

Yes, and they fit different directions. Shared memory suits cyclic values flowing out of the PLC program; the library suits bounded, event-driven handoffs such as queuing a request in. Keep each field owned by exactly one side and keep every library function bounded in its work. The risk to the task budget comes from blocking work inside the task, not from having two interfaces at the boundary.

Keep reading

  • Troubleshooting

    PLC-5/80E loses scan list on remote I/O after power cycle

    Auto-negotiate selected on Ethernet Channel 2 of a Series F Revision A PLC-5 clears the chassis scan list on the remote I/O scanner channels whenever the processor power cycles. Disable auto-negotiate and hard-set 100 Mbps full duplex, or flash Revision A.1 or newer; Auto-Config only rebuilds the list after each event.

    PLC programming

  • How-to

    What an S7-1200 upload recovers and what the IO devices need

    The documented upload of an S7-1200 recovers the CPU program and the controller-stored PROFINET configuration, including device names, IP addresses and module arrangements. It recovers nothing stored in the ten third-party IO devices: install each vendor's GSDML description separately and back up each device's own settings with its vendor tool, then verify byte counts and addresses online before building on the project.

    PLC programming

  • Troubleshooting

    RF180C on an S7-300: hardware address, not hardware identifier

    An ET200S IM151-8 has no hardware identifier for the RF180C, only the configured hardware address, which the Ident block takes from the compiled device configuration. For an MDS D100's user memory range 0000 to 006F inclusive, ADR_TAG is 0 and LEN_DATA is 112 decimal; 111 leaves the last byte out. STATUS 16#DF80_B00A on a write is the transaction's status code, not a presence problem.

    PLC programming