IMD SolutionComputing LibraryShop main store

How-to

Modbus TCP from a third-party PLC to cloud MQTT on IOT2040

Edge computing and IIoT Published 7 min read By IMD Solution engineering team

Short answer

Yes. Modbus TCP has no vendor lock: any PLC that runs a Modbus TCP server on port 502 can be polled by an IOT2040, whatever its maker. One Modbus client node in Node-RED reads the PLC on X1 and one MQTT out node publishes on X2 to the broker over TLS on port 8883; OPC UA is needed only if the PLC itself runs an OPC UA server.

On this page
  1. Can an IOT2040 read Modbus TCP from another maker's PLC?
  2. What plugs into X1 and what into X2?
  3. Is a Modbus node enough or do you need OPC UA?
  4. How do you build the Node-RED flow from Modbus TCP to MQTT?
  5. How many holding registers fit in one read?
  6. How do you verify the data reaches the cloud?
  7. Questions asked

Yes, and it needs no gateway-maker hardware on the PLC side: any PLC that runs a Modbus TCP server can be read by an IOT2040 running Node-RED, whatever built it. Modbus TCP carries no vendor identity — a request is a unit ID, a function code, an address and a quantity. You need one Modbus client node polling the PLC on X1 and one MQTT out node publishing on X2. OPC UA comes into it only if your PLC actually runs an OPC UA server. The wiring, the flow, the request arithmetic and the proof tests follow, with example addresses you replace with your own.

Can an IOT2040 read Modbus TCP from another maker's PLC? #

Yes: any PLC that runs a Modbus TCP server on TCP port 502 answers the gateway, regardless of who built it, because the message format carries no vendor identity at all. A read request holds a unit ID, a function code, a start address and a quantity — nothing else. A widely published function code 03 example asks the server at address 17 for holding registers 40108 to 40110; put your own unit ID and register numbers in the same frame and it works the same way.

On the PLC side three things must be in place: the Modbus TCP server enabled in its Ethernet configuration, a written register map (which holding register holds which value), and a unit ID you set or read from that configuration.

On the gateway side you need nothing special. The Siemens support entry for the IOT20x0 downloads, 109741799, ties the real time example image and the PROFINET driver to one job: running the IOT2040 as a PROFINET controller. A Modbus TCP client is untouched by that requirement and runs on the IOT2000 example image with Node-RED.

What plugs into X1 and what into X2? #

The PLC's Ethernet cable goes to X1, the router or plant uplink goes to X2, and your commissioning PC either shares X1 through a small switch or uses the gateway's USB console cable — and the PLC's Modbus port never leaves the X1 side. Give X1 a static address on the PLC's subnet. Example plan: PLC at 192.168.0.10, gateway X1 at 192.168.0.1, mask 255.255.255.0 — replace both with your plant's addressing. X2 takes its address from the upstream router, or a static one from IT.

The gateway is then the only bridge between the two networks: Modbus requests stay on X1, and the only traffic leaving X2 is the gateway's own MQTT publish — never a forwarded Modbus port.

Is a Modbus node enough or do you need OPC UA? #

A Modbus node is enough: OPC UA client nodes only work when the PLC itself runs an OPC UA server, and a PLC that serves only Modbus TCP offers an OPC UA client nothing to connect to. The two names answer different questions. OPC UA is a read protocol toward the PLC; MQTT is the transport that carries values from the gateway to the cloud. They are not alternatives: the output side is always MQTT or the cloud's own output node, and the input side is OPC UA only if your PLC lists that server option in its datasheet or order code.

If it does, that variant is described in bridging OPC UA PLC data to MQTT with an edge PC, and the trade-offs between the read paths are compared in how Node-RED reads PLC data over OPC UA, MQTT or Modbus. If it does not, build the Modbus flow below and leave OPC UA out.

How do you build the Node-RED flow from Modbus TCP to MQTT? #

The flow is a short line of nodes: an inject node repeating every 5 seconds, a Modbus Flex Getter aimed at the PLC, a function node converting the raw words into numbers, a debug node during commissioning and one MQTT out node to the broker.

  1. Open the editor. On the IOT2000 example image Node-RED is already installed and the editor answers on port 1880. From the palette manager install node-red-contrib-modbus; the modbus-read, modbus-flex-getter, modbus-write and modbus-server nodes appear in the palette.

  2. Set the pace. Drop an inject node and set it to repeat every 5 s; that repeat is the poll rate.

  3. Point the getter at the PLC. Add a Modbus Flex Getter with a Modbus client configuration of type TCP: host is the PLC's address (example 192.168.0.10), port 502, unit ID 1. Set the read to function code 03, start address 0, quantity 100 — example values you replace from the PLC's register map.

  4. Convert the words. The getter returns a buffer of 16-bit words in Modbus byte order; a function node turns them into numbers:

    // registerBuffer: Buffer — the getter's raw response data (for example 200 byte for 100 registers)
    // registerValues: Array of number — starts empty, ends with one entry per 16-bit register
    // wordIndex: number — loop counter, initial value 0
    var registerBuffer = msg.payload;              // msg.payload is the runtime's name
    var registerValues = [];
    for (var wordIndex = 0; wordIndex < registerBuffer.length; wordIndex += 2) {
      registerValues.push(registerBuffer.readInt16BE(wordIndex)); // readInt16BE is the runtime's name
    }
    msg.payload = {
      device: "line1-plc",
      timestamp: new Date().toISOString(),
      holding_registers: registerValues
    };
    return msg;
    

    registerBuffer, registerValues and wordIndex are this example's own names. For 16-bit values above 32767 read unsigned with readUInt16BE instead, or counters will show negative.

  5. Publish. Configure the MQTT out node with the broker's host, port 8883 with TLS enabled, QoS 1 and a topic such as plant/line1/plc1/holding (example); the credentials come from the broker's device registration. Another cloud changes only this node — address, credentials, topic convention — while the Modbus half stays identical.

  6. Wire and deploy: inject to getter, getter to function, function to debug and to MQTT out. Writing in the other direction, MQTT commands down to Modbus TCP devices, is a different flow, covered in MQTT to Modbus TCP with Node-RED on an edge gateway.

How many holding registers fit in one read? #

One function code 03 request reads at most 125 holding registers — a quantity limit set by the Modbus application protocol specification — so a 100-register read is legal with room to spare. Worked example with the flow above:

  • Data: 100 registers × 16 bit = 1600 bit = 200 byte.
  • Response frame: 7-byte MBAP header + 1-byte function code + 1-byte byte count + 200 byte data = 209 byte.
  • Request frame: 7-byte header + 1-byte function code + 2-byte address + 2-byte quantity = 12 byte.
  • Line load at one poll every 5 s: (209 + 12) × 8 bit ÷ 5 s ≈ 354 bit/s, about 0.35 kbit/s — under a thousandth of one percent of a 100 Mbit/s port.

On the MQTT side, 100 numbers as JSON come to roughly 700 byte per message: 700 × 8 bit ÷ 5 s ≈ 1.1 kbit/s of uplink before TLS overhead. Neither number will ever limit you.

Decision: poll the whole register map in one getter while it fits in 125 registers. Past that, split the map into blocks of 125 or fewer with one getter per block, and offset their inject timers by half the poll period so two requests never race.

How do you verify the data reaches the cloud? #

Four checks prove the chain in order: a TCP check from the gateway to the PLC, a live array in the debug sidebar at the poll rate, a TCP check to the broker, and a subscriber that sees the messages land.

  1. PLC reachable: from the gateway console run nc -zv 192.168.0.10 502 with the example address; a reply reporting success means the TCP path to the server is open.
  2. Conversion correct: deploy and watch the debug sidebar — one array every 5 s with as many entries as the requested quantity. Prove it against a known value: if the PLC's first register currently holds 1200 (example), the array's first element must read 1200.
  3. Broker reachable: run nc -zv mqtt.example.com 8883 with the broker's real host name. If the plant firewall only allows outbound 443, MQTT over TLS on 443 needs ALPN, which why MQTT over port 443 to a cloud needs ALPN explains.
  4. End to end: subscribe to the topic with any MQTT client, watch messages arrive at the poll rate, then find the same values in the cloud's event view. If check 2 passes and this one fails, the fault is in the uplink or the credentials — the Modbus half is already proven.

Questions asked

Can one gateway poll several Modbus TCP PLCs?

Yes. Give each PLC its own Modbus client configuration with that PLC's address and unit ID, its own getter and its own inject timer, and offset the timers so requests to different PLCs do not stack up. The Modbus client works through requests one at a time, so with many PLCs stretch the poll period instead of shortening it, and keep each read within the quantity limit.

What changes when the cloud is not the one in the example?

Only the MQTT out node: the broker's host name, the TLS port, the credentials or client certificate from that cloud's device registry, and its topic convention. The Modbus client, the register read and the conversion function stay exactly as built. Where the cloud issues X.509 client certificates, load them in the MQTT node's TLS settings instead of a user name and token.

Can the same flow write setpoints back to the PLC?

Yes, as a separate branch: an MQTT in node on a command topic, a function node mapping the value to a register address, and a Modbus write node on the same client configuration. Write one register with function code 06 or a block with function code 16, then confirm on the next poll that the register reads back the written value. Restrict the command topic at the broker so only the intended publisher can use it.

Is plaintext MQTT on port 1883 good enough?

Only inside a network you already trust. Port 1883 carries no encryption, so anyone on the path can read the register data or inject messages, and the password travels in the clear. Use MQTT over TLS on port 8883 instead; the certificate is configured once in the MQTT node and the rest of the flow does not change.

Keep reading

  • How-to

    Bridge a cellular tracker's TCP stream into MQTT for AWS IoT Core

    Build a small ingest service on your edge PC: it terminates the tracker's raw TCP session, decodes each stored record, and republishes one MQTT message per fix over TLS on port 8883. Acknowledge a packet only after every publish is confirmed, or a coverage-gap batch is silently lost.

    Edge computing and IIoT

  • How-to

    MQTT to Modbus TCP with Node-RED on an edge gateway

    Yes: Node-RED with the node-red-contrib-modbus package can serve MQTT data to a SCADA client as Modbus TCP. Read the RTU device in Node-RED first, give the Modbus server node a port nothing else owns, and scale each MQTT payload to one integer per holding register before the first end-to-end test.

    Edge computing and IIoT

  • Explainer

    Edge gateway Modbus TCP polling over the WAN port

    When a data gateway supports MQTT as a client only, drop the broker hop: configure the Modbus TCP poll to target the device's IP address in the same subnet as the gateway's WAN interface. The gateway opens the TCP connection itself, so polled values keep updating with the internet link down. Verify by pulling the uplink and watching one known register.

    Edge computing and IIoT

Why Docker Ignition skips third-party modules on first startup