Explainer
Where an OPC UA server sits when Python reads PLC data
Short answer
No IEC 61131 function block transports data by itself: OPC UA (IEC 62541) needs a server that maps PLC variables into an address space, and a classic S7-300 has none, so the endpoint comes from a server or gateway on a computer beside the CPU. Point Python at that server's opc.tcp endpoint, keep actuator control and the comm-loss fallback in the PLC, and accept Python's results as requested setpoints.
On this page
No IEC 61131 function block transports data by itself. OPC UA (IEC 62541) delivery comes from a server — software that maps PLC variables into an address space and answers client sessions over an endpoint — and a classic S7-300 CPU does not run that server. For your Python-plus-PLC machine, the endpoint belongs on a computer beside the controller: a server or gateway with a documented driver to the CPU, which your Python code then connects to. This article fixes where that server sits, how to tell a real UA endpoint from an OPC Classic ProgID, what the PLC must keep for itself, and how to time the fallback for when Python goes quiet.
A function block is not a transport #
Three standards get tangled in this question, and only one of them moves data. IEC 61131-3 defines PLC programming languages, function blocks among them. IEC 61499 defines a different, event-driven function-block architecture. IEC 62541 defines OPC UA: sessions, services such as read, write and subscription, and the address space those services act on. Part 3 of IEC 62541 gives the address space's objective — one standard way for a server to represent its objects to clients — and splits variables into Properties and DataVariables.
A programming standard says nothing about whether a controller implements a UA server or client; that is a firmware capability, either documented or absent. A function block is a software unit with inputs, outputs and internal behavior, and its variables become remotely readable only when a communication implementation maps them into an OPC UA address space: a named set of nodes plus the references between them. Support for IEC 61131 programming and for PROFINET CBA establishes neither OPC UA functionality nor native IEC 61499 execution; each is a separate capability, verified against the documentation of the exact CPU and firmware.
So "is there a function block that speaks UA?" collapses into a better question: what on this machine runs the UA server?
Three places the server can sit #
- In the controller. Current-generation CPUs ship a UA server in firmware; you configure the exposed variables and the endpoint in the engineering project. No classic S7-300 CPU has one and no option module adds one, so do not spend an evening searching its block libraries.
- On a computer beside the controller. A server or gateway application with a documented PLC driver polls the CPU over its native protocol and republishes the data as UA nodes. This is the arrangement to plan for an S7-300. When that gateway lives in the cabinet next to the CPU, the machine for it is an industrial mini PC, running the driver and the UA stack around the clock.
- In a function-block runtime on the server. IEC 61499 runtimes such as 4diac FORTE ship OPC UA function blocks: publish and subscribe blocks that expose the runtime's own variables, client blocks that reach other servers. Those blocks run on your external computer and expose that runtime's data. They are not blocks for the S7-300's program, and the PLC's values still have to reach the runtime over a link it supports before any of theirs appears over UA.
The reverse arrangement — the PLC as UA client pushing to a server — exists on CPUs that document a client stack; starting an OPC UA client connection from PLC code describes that pattern, and the classic S7-300 documents no client either. For your machine, commit to the middle option: an external server or gateway, with Python pointed at its endpoint.
A Classic ProgID is not a UA endpoint #
The draft code calls Client("Matrikon.OPC.Simulation"): a UA client handed a Classic name. A ProgID identifies a registered Windows COM component; a UA endpoint is a URL in the opc.tcp scheme, and it comes from the server's own configuration or its discovery response — never from a ProgID.
OPC Classic rides COM/DCOM and carries Windows with it; OPC UA has its own wire protocol and its own security model of endpoints, policies, sessions and certificates. Installing a Python UA library does not make a Classic server speak UA, and choosing Linux for the compute host does not remove a Classic server's Windows dependency: that server must run on Windows, and a Linux Python process reaches it only through a UA wrapper placed in front of it.
The OpenOPC test loop is a Classic client reading simulation tags from Matrikon.OPC.Simulation.1 — a separate Classic connectivity check, not an endpoint for the UA client. Fix one bug before blaming any transport: that loop creates its client object as opc and then calls read on OPC, and Python treats those as two different names.
Then work in this order:
- Classify the interface on the server you actually have: UA endpoint or Classic ProgID. A Python UA library speaks only UA; OpenOPC speaks only Classic, and only on Windows.
- Take the endpoint URL, security policy and certificate rules from the server's configuration or its discovery output.
- Connect with a matching policy, a client certificate the server trusts and a user identity it accepts, without weakening the production security setup — the trust step is the one covered in trusting the Citect OPC UA client certificate on a B&R PLC.
- If the session opens but no value can be found, you have an addressing problem, not a transport one: browse the live server instead of copying a sample path.
What stays in the PLC #
The division of labor is not negotiable. The PLC keeps actuator control, permissives, operating modes, local limits and the response to communication loss. Python supplies requested setpoints and supervisory commands, never unconditional actuator authority, because a Python process carries solve time, operating-system scheduling and network delay that the PLC's local cycle does not have.
Document the interface before choosing any transport: which measured inputs Python needs, which outputs it may request, engineering units, valid ranges for every value, a status word for the calculation itself, and the rule for accepting a result — a fresh flag or sequence counter the PLC checks before acting on a setpoint.
The exposure mechanism is the server's business: a driver polling the CPU over its native protocol, or mapped memory the server reads, as in exposing PLC values to an OPC server through guest memory blocks. What you own is the mapping document: which PLC variable becomes which node, with unit, range and scale. The numeric prefixes in a browse path like ["0:Objects", "1:MyObject", "2:MyVariable"] are namespace indexes of that sample server, not PLC rack or memory addresses; your tags will sit wherever the gateway's mapping puts them, so resolve them by browsing the live server.
One safety note where it belongs: enable remote writes only after you have pulled the cable and watched the PLC take its fallback, because the comm-loss response is a machine behavior that lives in the PLC program, not in the supervisory computer.
Example: setting the PLC watchdog for a 1 s poll and an 8 s solve #
Inputs:
- Poll period: 1 s. This is the period of the polling loop in the test code, which sleeps 1 s between reads.
- Solve time: 8 s. An illustrative value for a machine-learning solve of this kind on your server class; replace it with what you measure.
- Rule: set the PLC's comm watchdog at three times the nominal result period. Two missed results is a network blip; three in a row is a dead supervisor, and the loop comes home.
Arithmetic:
- Nominal result period = solve time + up to one poll = 8 s + 1 s = 9 s.
- Watchdog = 3 × 9 s = 27 s, rounded up to 30 s, the next value you will actually dial into a PLC timer.
- Check: two consecutive missed results cover 2 × 9 s = 18 s, comfortably inside the window, so normal jitter rides through; the third miss reaches 27 s and trips.
Result: a 30 s watchdog in the PLC, reset by every accepted fresh result, with the acceptance rule from the interface document deciding what counts.
Decision: program the 30 s watchdog now. When it trips, the PLC holds local control on the setpoint table it already uses in manual and raises the stale-data alarm; it re-accepts remote setpoints only after three consecutive fresh, in-range results. Above 30 s without an accepted result, the PLC owns the loop. If your measured solve time makes 3 × (solve + poll) longer than the process tolerates unsupervised, shorten the solve or demand fresh results less often — never stretch the watchdog to fit the solver.
Bringing it up in order #
- Stand up the server or gateway and prove the PLC leg first: read a changing measurement in the server's own diagnostics until it tracks the PLC value and the connection reports healthy.
- Connect Python with the real endpoint, a matching security policy and a trusted certificate.
- Browse the live server, resolve every tag's path, and finish the mapping document with units and ranges.
- Pull the cable, watch the fallback act, and only then enable remote writes.
- Mirror the stale condition upstairs as an operator alarm, the discipline covered in how to create a SCADA PLC communication alarm for link loss.
The decision, case by case. If your CPU's documentation lists a built-in UA server, connect Python to it directly and skip the gateway. On a classic S7-300 — your machine — no such server exists and no block creates one: run an external server or gateway with a supported S7 driver, point Python at its opc.tcp endpoint, and leave control, limits and the watchdog in the PLC. And if you want UA function blocks specifically, that means a 4diac FORTE-style runtime on the server, fed from the PLC over a link it supports — never a block in the 300's folder.
Questions asked
Can the S7-300 itself act as the OPC UA client instead?
Only on CPUs whose firmware documents a UA client; the classic S7-300 documents neither server nor client, so reversing the roles does not help on this machine. Where a client stack does exist, the PLC opens the session and pushes or subscribes itself, which removes the gateway but puts session handling, certificates and reconnect logic inside the PLC program and its cycle. That trade suits a controller with a supported client stack and spare cycle budget, not this one.
Would Modbus TCP be simpler than OPC UA for this link?
For a small register map, yes: Modbus TCP has no sessions, certificates or address space to configure, both ends come down to a handful of settings, and mature Python libraries exist for it. It stops paying off when you need typed, structured data with units and scales, or several clients reading the same model, which is what a UA address space buys. A dozen registers and one client is a Modbus job; a documented data model with several consumers is a UA job.
What does the number prefix in a UA browse path mean?
It is the namespace index of that node's identifier, not a PLC address. Index 0 is reserved for the OPC UA standard namespace; the server's own and vendor namespaces receive other indexes, assigned in a namespace array the server publishes. Those indexes belong to that server's configuration and can shift after a reconfiguration, so resolve your PLC tags by browsing the live server and record the namespace array in the mapping document.
If 4diac FORTE ships OPC UA blocks, can it replace the gateway?
Only partially. FORTE runs on the external computer, and its publish, subscribe and client blocks expose FORTE's own variables over UA. The PLC's data must arrive inside FORTE first, over a fieldbus or link FORTE actually supports for that CPU, which is a separate thing to prove. With a working path to the S7 data, FORTE can serve as the UA server and the separate gateway goes away; without one, it adds a layer without moving a single value.