IMDHMIControls LibraryShop main store

How-to

Multi-instance Modbus blocks and MB_DB links in an S7-1200

PLC programming Published 7 min read

Short answer

Both Modbus_Comm_Load and Modbus_Master belong in instance data, and both can be declared as multi-instances inside your wrapper function block: keep optimized block access and pass the Modbus_Master instance to the MB_DB input of Modbus_Comm_Load. One MB_CLIENT instance serves every register range on the same server as long as each request starts only after the previous one finishes. Move the wrapper to standard block access only when something in the project addresses data by absolute offset.

On this page
  1. Why the Modbus instructions need instance data
  2. Build the wrapper step by step
  3. Optimized versus standard block access
  4. One MB_CLIENT instance for every range on one server
  5. Worked example: budget one serial polling cycle
  6. The decisions
  7. Questions asked

Yes: declare Modbus_Comm_Load and Modbus_Master as multi-instances inside your wrapper function block, keep optimized block access, and one MB_CLIENT instance covers every register range on the same server. Nothing in the instruction set forces the wrapper to standard block access — connect the Modbus_Master instance to the MB_DB input of Modbus_Comm_Load, compile, and treat the clean compile as the acceptance test for the arrangement. Because your first real test happens on site, the steps below prove in software everything software can prove: build the wrapper, gate the polling on serial initialization, sequence the TCP ranges between completions, and budget what the serial port delivers per cycle, with the arithmetic shown.

Why the Modbus instructions need instance data #

A Modbus transaction spans many PLC scans: the request input goes true, the instruction drives the port or the TCP connection, the response arrives, and only then does the instruction report completion. That state must survive between calls, so both serial instructions run with instance data, and a function has none. Placed in the program, Modbus_Master is assigned an instance data block automatically; selecting multi-instance in the call options puts that same data inside the wrapper's instance DB instead of a separate block. The master's instance DB carries its transaction state, status and error values, and working registers — which is also why a second slave on one port gets its own master instance rather than a reused one.

Modbus_Comm_Load is the setup half: it selects the serial communication module, sets the communication parameters and parameterizes the port for the master that follows. Its MB_DB input receives the reference to the Modbus_Master instance that will use that port, and inside a function block you connect that reference symbolically, as named data of the same parent instance, rather than assembling a DB-number pointer by hand. That symbolic reference is why optimized block access normally fits this arrangement.

Build the wrapper step by step #

  1. Create the wrapper as a function block and leave optimized block access enabled, the editor's default. Plan one call of Modbus_Comm_Load and one of Modbus_Master inside it, both declared as multi-instances in the call options.
  2. In the load call, select the serial communication module and set the port parameters the slave expects — for example 19200 baud, even parity, one stop bit. Take them from the slave's own documentation, not from memory.
  3. In the master call, set the slave address, function code and data buffer for each range exactly as the instruction interface defines them.
  4. Connect the Modbus_Master multi-instance to the MB_DB input of Modbus_Comm_Load.
  5. Compile. A clean compile with optimized access is the acceptance test for the whole arrangement: keep optimized access and move on.
  6. Gate the polling: hold the master's request input false until the load instruction reports completion without error, and start each following request only after the previous one completes. If the completion output never arrives at all, work through Why modbus_comm_Load Done stays false on an S7-1200 G2 before touching the master logic.
  7. Only if the selected instruction versions reject the nested reference at compile time, move Modbus_Comm_Load and Modbus_Master back to their own instance data blocks and keep the sequencing logic in the wrapper. Encapsulation is a preference; a valid instruction interface is a requirement.

Prove the startup and restart path on site with the machine in a state where an unconfigured or lost link cannot move anything, and keep any safety function out of this polling logic: a safety function is validated on its own terms, not by communication health.

Optimized versus standard block access #

Optimized access lets the compiler lay the block's data out and hands every reference to you symbolically. Standard access fixes the layout so other software can address the data by absolute offset, and those absolute references are its entire drawback: the editor cannot retarget them when a declaration changes, so every interface edit puts them back on your checklist by hand. The instructions' own MB_DB reference is symbolic, so nothing about the communication itself needs standard layout.

Situation Decision
The wrapper compiles with optimized access Keep optimized access; standard layout buys nothing here
Some project object reaches the block's data by absolute offset Switch that one block to standard access and re-verify every absolute reference after each interface change
The instruction help requires the master's data buffer to sit in a standard-layout block Keep the wrapper optimized and give the buffer its own standard block; the rule concerns the buffer's block, not the wrapper

The third row is the one people over-apply: a layout rule aimed at the buffer does not justify making the whole wrapper standard.

One MB_CLIENT instance for every range on one server #

An MB_CLIENT instance owns one TCP connection, the one described by the connection data you fill with the server's address and port. Register ranges are transaction parameters: address, length, read or write, and buffer. So the answer is yes — read one range, wait for completion, then point the same instance at the next range. Change those parameters only between completed transactions, never while the instance reports busy, and no second connection ID is needed.

A second MB_CLIENT instance is a second connection, not another line in a polling list, and two connections from one client into a server that allows one is a known way for reads to stop arriving — the mechanics are in Why two MB_CLIENT instances stop Modbus TCP reads from one server. Two protocol rules bound the transactions themselves. One read carries at most 125 registers, the ceiling function codes 03 and 04 define, so a larger map is split into sequential calls; what a read that ignores the ceiling looks like is documented in Why MB_CLIENT returns Modbus TCP error 8383 on multi-register reads. And if cycle time hurts later, parallelism pays off across separate endpoints, not across the ranges of one server — the trade-off is worked through in Why parallel Modbus TCP clients beat one sequential poller.

Decision: one MB_CLIENT instance per server, all ranges sequenced through it; a second instance only for a genuinely separate endpoint the server demonstrably accepts.

Worked example: budget one serial polling cycle #

Example inputs, illustrative except where the protocol fixes them: three ranges read back to back through the one Modbus_Master — 20 holding registers (function code 03), then 10 holding registers (function code 03), then 30 input words (function code 04) — on a port set to 19200 baud, 8 data bits, even parity, one stop bit, with the slave answering 10 ms after each request. The protocol fixes the rest: a Modbus RTU character is 11 bits, the silent gap between frames is 3.5 characters, and one read stays inside the 125-register ceiling.

  • Byte time: 11 bits ÷ 19200 bit/s = 0.573 ms; frame gap: 3.5 × 0.573 ms = 2.0 ms.
  • Range 1: request 8 bytes, response 5 + 2 × 20 = 45 bytes, total 53 bytes = 583 bits; 583 bits ÷ port rate = 30.4 ms.
  • Range 2: 8 + (5 + 2 × 10) = 33 bytes = 363 bits → 18.9 ms.
  • Range 3: 8 + (5 + 2 × 30) = 73 bytes = 803 bits → 41.8 ms.
  • Latency and gaps: 3 × 10 ms + 3 × 2.0 ms = 36.0 ms.
  • Cycle: 30.4 + 18.9 + 41.8 + 36.0 = 127.1 ms, so every range refreshes 1000 ÷ 127.1 ≈ 7.9 times per second.

Two sensitivities move that number. Each register removed from a read cuts 2 bytes = 22 bits, about 1.1 ms of wire time. Raising the port to 115200 baud, where both devices support it, divides the 1749 wire bits down to 1749 ÷ 115200 bit/s = 15.2 ms and drops the gap to the protocol's fixed 1.75 ms value that applies above 19200 baud: the cycle becomes 15.2 + 3 × (1.75 + 10) ≈ 50 ms, about 20 refreshes per second.

With the example inputs, size the machine logic around 8 refreshes per second per range, start each request on the completion of the previous one, and buy speed back by shrinking reads at about 1.1 ms per register or by the port rate. If the port serves several slaves, the same budget covers them all, sequenced — the scheduling pattern is in How to poll many Modbus RTU slaves from one serial master, and the reason one port never gets faster with more devices is in Why Modbus RTU drive polling on one serial port is slow.

The decisions #

  • Instance structure: multi-instances inside the wrapper when it compiles with the MB_DB reference connected; separate instance data blocks when your instruction versions reject the nesting, with the sequencing logic kept in the wrapper.
  • Block access: keep optimized access everywhere except a block whose data is addressed by absolute offset, and re-verify those references after every interface change.
  • TCP: one MB_CLIENT instance per server, ranges sequenced between completions, reads split at the 125-register ceiling; a second instance only for a second endpoint the server demonstrably accepts.
  • Serial: budget the cycle as wire time plus slave latency and frame gaps — 127.1 ms for the three-range worked example — and hold all polling off until the load instruction completes.

Questions asked

Can I call the same wrapper block twice, once per Modbus device?

Each call of a function block needs its own instance data. A second call that shares the first call's instance DB lets two calls write the same transaction state and working registers, so both links misbehave. Give the second call its own instance DB, or copy the wrapper and give the copy its own instance. Inside one instance, sequence the devices rather than running parallel requests.

How often should Modbus_Comm_Load run?

Once. The load instruction selects the communication module, sets the serial parameters and prepares the port for the master; after it completes successfully, nothing needs it again until the configuration changes or the CPU restarts. Call it from a one-shot at startup, keep master requests inhibited while it runs, and re-run it deliberately when needed rather than every scan.

Does one MB_CLIENT instance also cover writes to several ranges?

Yes. The instance owns the connection; each transaction carries its own range, length, direction and buffer. Write to one range, wait for completion, then point the same instance at the next read or write. Changing direction between completed transactions needs no new connection ID, and the same busy-check discipline applies as for reads.

Which serial settings must match the slave?

The port parameters in the load call must equal the slave's: baud rate, parity and stop bits. Modbus RTU fixes the character at 11 bits either way: eight data bits with a parity bit plus one stop bit, or eight data bits without parity plus two stop bits. A mismatch usually shows up as silence rather than an error code, so compare both ends line by line before you travel.

Keep reading

  • How-to

    Set up a CLICK Plus as Modbus TCP client to an S7-1200

    Yes: make the S7-1200 the Modbus TCP server with MB_SERVER mapped to a data block of INTs, and make the CLICK Plus the client with Send and Receive instructions aimed at the CPU's IP address and port 502. Write the shared register map first; the S7 logic copies physical inputs into it and applies its output words to the real outputs.

    Industrial networks

  • Troubleshooting

    Why an HMI Modbus TCP write leaves the S7-1200 DB unchanged

    The write most likely never reached the register you think it did: the HMI's 400001 is a one-based display number, while the Modbus request carries a zero-based offset that the server block maps onto DB102.DBW0. Confirm the server block status and data area, confirm station 3 points at the S7-1200 rather than the motion controller, match the driver's register convention, and prove the path with one known value written to a single word.

    Industrial networks

  • Troubleshooting

    Why MB_MASTER on an S7-1200 reports Modbus error 16#8188

    Error 16#8188 on an S7-1200 MB_MASTER is an operating-mode fault, not a serial link failure. Set MODE 1, DATA_ADDR 30001, a length of 32 and MB_ADDR 1 to read the slave's D000 through D01F block as input registers 30001 through 30032, and hold EN TRUE through the REQ pulse.

    PLC programming