IMDHMIControls LibraryShop main store

Explainer

Reading PLC values from a TwinCAT OPC UA server: read vs subscribe

SCADA and data logging Published 6 min read

Short answer

The TwinCAT OPC UA server sends nothing to a client that has not asked: your C++ client must send a read for a one-time value, or create a subscription with monitored items for change notifications. A write is a separate request that succeeds only on a node the server exposes as writable, and only with a value of the node's own type.

On this page
  1. What the server exposes and how you find your nodes
  2. Read, subscribe or write
  3. Why a subscription beats polling, in message counts
  4. What OPC UA Part 4 requires of a subscription
  5. A worked timing check
  6. Writing values back
  7. When no value arrives, test in this order
  8. Questions asked

Nothing moves until your client asks. The TF6100 TwinCAT 3 OPC UA Server exposes PLC symbols as OPC UA nodes, but it pushes no temperature, pressure or setpoint value to a client that sits idle; delivery is always a response to a request from your side, for example a program built on the Unified Automation OPC UA C++ client. That gives you exactly three operations: a read, which returns the current value once; a subscription with monitored items, which delivers a notification whenever the server samples a change; and a write, which is a control command, not a way to obtain data. This article says which request to use, what OPC UA Part 4 requires of a subscription, and how to set the intervals so an update reaches your display inside the time it needs.

What the server exposes and how you find your nodes #

The TF6100 server publishes the PLC symbols that were selected for exposure as nodes in its address space. A symbol nobody exposed does not exist for OPC UA, and no read, subscription or write can reach it. Take the endpoint address and port from the server configuration rather than assuming a port from the protocol name.

Before writing client code, connect a general browsing client such as UAExpert to that endpoint and browse the address space. For each temperature, pressure and setpoint node, record the namespace and node identifier exactly as the browse result shows them, the data type the server reports, and the value together with its status. Display names are for humans; the C++ client addresses nodes by namespace and identifier, and a name like temperature proves neither the identifier nor the type. If the endpoint runs with security, the server must trust the client certificate before any session opens, the same step as trusting a Citect OPC UA client certificate on a B&R PLC.

Read, subscribe or write #

The three operations answer different needs, and the decision follows from what your application does with the value.

Request What the server does Use it when
Read Returns the node's value and status once, when the client asks A snapshot, a one-time check, or validating identity and type
Subscription with monitored items Samples the node at the sampling interval and sends notifications in one message per publishing cycle The application must see each change without asking for it
Write Checks the node's access level and the client's rights, then stores a value of the node's type You command a setpoint or another writable value

For a live temperature, pressure or setpoint display, the decision is a subscription: you want changes as they happen, without a request per value. Use reads first to validate each node's identity and type, and keep a read where the program needs a one-time snapshot, such as a value logged once per batch. A write belongs in the control path only.

Why a subscription beats polling, in message counts #

Worked example with example inputs: a client that polls 20 nodes with one read request every 500 ms sends 2 requests per second whether or not anything changed, which is 2 × 60 × 60 × 24 = 172,800 requests per day on that session, each answered by the server's real-time access. Put the same 20 nodes on one subscription as monitored items and the server sends a notification message only in a publish cycle where something changed, at most one message per publishing interval. That is the mechanism behind why OPC UA subscription updates beat polling for PLC input changes. The same trade-off, seen from a scripting client, is shown in a subscribe-versus-read comparison for OPC UA items in Node-RED.

What OPC UA Part 4 requires of a subscription #

OPC UA is specified in the IEC 62541 series, and the services model lives in Part 4. There, a subscription groups monitored items and sends its notifications in one message per publishing interval, and each monitored item names the node attribute to watch and the subscription that carries its notifications. The watched attribute can be any node attribute; for a process value it is the Value attribute. Two intervals control the timing. The sampling interval is the fastest rate at which the server samples its underlying source for data changes. The publishing interval sets how often the subscription's notification message goes out. Both exist only on subscriptions; a read is as fresh as the moment you send it.

The server may revise both intervals when you create the subscription and its items, and it returns the revised values in the create responses; your timing check must use those, because what you requested is only a request. One commissioning rule follows from how the two cycles interact: do not set the intervals equal. When the publish instant and the sampling instant coincide, the value may not have been sampled yet when the message is assembled, so the change misses that cycle and arrives a cycle late. Make the publishing interval an integer multiple of the sampling interval, at least double.

A worked timing check #

Worked example with example inputs: a display must show a setpoint change within 1 s. You request a sampling interval of 100 ms, a publishing interval of 200 ms (twice the sampling interval, so the multiple rule holds), and a queue of 10 samples per item. Worst case is a change that lands just after a sample: it waits up to 100 ms to be captured, then up to 200 ms for the next publish cycle, so the client callback fires within 100 ms + 200 ms = 300 ms plus network transport, inside the 1 s requirement with 700 ms of margin. Between publishes the server takes 200 ms / 100 ms = 2 samples, and the queue of 10 keeps both without discarding. After creation, take the revised intervals from the responses; if the server moved the sampling interval up, redo the sum with the revised number, and the decision stands as long as the total stays inside your requirement. Decision for this case: subscribe for the display, read once at startup to validate the node, and write only the setpoint.

Writing values back #

A write succeeds only when the server exposes the node as writable and permits that client to write; the value must match the node's declared type, and the response carries a status you must inspect. Send the write, check the status, then read the node back and compare, because the PLC program can override an accepted value one scan later and the read-back is the proof. Access level is also why writes fail on other servers, as in why OPC UA writes fail to an Omron NJ PLC. One safety note: a setpoint commanded over OPC UA carries no interlocks of its own, so range-check it in the PLC program and keep the interlocks there, where they run on every scan.

When no value arrives, test in this order #

  1. Session: does the client connect at all? Check the endpoint address and port against the server configuration, the network route, the firewall, and the certificate trust step.
  2. Browse: does the node exist? Confirm the symbol was exposed and that namespace and identifier match the browse result character for character.
  3. Read: does a single read return a good status and the expected type? Compare with UAExpert on the same node.
  4. Subscription: was it created and the item added, are the revised intervals and queue what you assumed, and is the client still processing notifications?
  5. Write: is the node writable, and does the value's type match the node's declared type?

If UAExpert reads the node and your C++ program cannot, compare endpoint, namespace, identifier and status between the two, and the difference is in your code; if both fail at the same hop, fix that layer before touching the application.

Questions asked

Does the TwinCAT OPC UA server expose every PLC variable as a node?

No. Only symbols selected for exposure appear in the address space, and the selection is made with the TwinCAT OPC UA Configurator, which comes in two variants: an interface integrated into the Visual Studio Shell and a standalone tool. A variable that was never exposed cannot be read, subscribed or written by any client, so a browse that misses your temperature or pressure symbol is a configuration miss, not a client fault.

Should the PLC convert temperature and pressure to strings before exposing them?

No. Expose the numeric type the PLC already holds and format it in the client. A node carries one declared type, so a REAL stays a REAL and the C++ side renders it for display. Converting to string in the PLC adds a formatting step to the scan and a parse on the client, and it hides the value's type from anyone who browses later. Reserve strings for text that is genuinely text, such as batch identifiers or alarm messages.

Can two C++ clients watch the same node at the same time?

Yes, independently. Each client opens its own session, and a subscription belongs to the session that created it: the server samples and queues per monitored item, so two subscriptions on one node each receive their own notifications at their own intervals. Nothing one client subscribes to is forwarded to another, and a client that only reads never benefits from someone else's subscription.

The first notification arrives, then updates stop. What broke?

Check the client before the server. Notifications are delivered through the client's own processing, so a callback that blocks or an application that stops servicing its session stalls every monitored item on it while the server keeps sampling. After that, compare the revised queue size and intervals in the create responses with what you assumed, and confirm the value at the source is really changing between samples. A value that changes and returns within one sampling interval leaves no trace.

Keep reading

  • Explainer

    Where an OPC UA server sits when Python reads PLC data

    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.

    SCADA and data logging

  • How-to

    Connecting a CODESYS HMI on Windows to an S7-1500 PLC

    Yes, it works. Use OPC UA with the S7-1500 as server and the CODESYS HMI as client when the PC only supervises; use a CODESYS Control Win runtime with target visualization over a PROFINET controller/device link when the PC must also run logic. The PROFINET path adds one single-device license on top of the runtime; the OPC UA path adds none.

    SCADA and data logging

  • How-to

    Expose PLC values to an OPC server through guest memory blocks

    Copy every value the OPC server needs into a Do-more guest block with a ladder MOVE, cast 32-bit reals at an even DLV address and configure the server tag as REAL, and let clients write outputs only to a V-memory staging word the ladder moves to the physical output. DLY is data, not an output alias, and Modbus clients read through MRX and MWX.

    SCADA and data logging