MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do you decouple PLC code and define interfaces between modules?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do you decouple PLC code and define interfaces between modules?

As programs grow, "change one place, collapse everywhere" sets in — the root cause is wild cross-referencing: process logic reading another module's internals, one module's state scattered through B, C and D. Decoupling's goal: a module's internals may change freely and, as long as the interface holds, nobody outside notices. This article gives module division, interface definition, handshake protocol and the checking method — four pieces.

1. Dividing modules: by "responsibility", not by "data"

Module typeResponsibilityExamples
Equipment modulesManage one object's full lifeMotor module, cylinder module, axis module
Process modulesManage one process sectionLoading section, machining section, unloading section
Service modulesManage horizontal functionsAlarms, recipes, communication, parameters

The division test, one sentence: can this module's "job" be stated in one sentence? "Manages conveyor 2's start/stop and faults" can — it is a module; "handles some Ms and Ds" cannot — it is a junk drawer. Data inside a module is private; between modules only the interface area flows.

2. The interface area: the module's counter window

Each module defines a dedicated interface area (an interface DB / a dedicated D block) in three parts:

PartContentRights
Input windowCommands and setpoints others give meOthers write, this module reads
Output windowStatus and results I give othersThis module writes, others read
Handshake areaRequest/acknowledge flags, sequence numbersPer the protocol (below)

The iron law: beyond the interface area, internal variables are exclusive. A full-program search must confirm: module A's internals appear only in A's code. With that held, changing A's internals (devices, algorithms) touches nobody.

3. The handshake protocol: how modules "talk"

For cross-module actions (a process module commanding an axis module to position), use request-acknowledge, not internal peeking:

Process module: write TargetPos → set Req (request edge)
Axis module: on Req's rising edge → execute → set Done + increment the ack number, clear Req
Process module: wait for Done or timeout → continue / alarm

Four points: ① requests trigger on a rising edge (no double execution); ② acknowledgements carry a sequence number or use Req/Done alternation (so last time's Done is not misread); ③ a timeout is mandatory (a dead partner module must not hang the wait forever); ④ no two-way initiation (who commands and who serves is fixed; communication flows one way).

4. Data flow: one-way is king

FlowVerdictNote
Process → equipment (commands)NormalUpper layer directs lower
Equipment → process (status)NormalLower reports results
Equipment → equipment direct referencesDangerousRoute via upper-layer coordination or handshake
Lower directing upperForbiddenBreaks layering; report status and let the upper decide

Two devices need a mutual lock (must not move together)? The lock lives in a common process layer or a dedicated interlock module — not as A reading "B's internal flag". Interlock-rule changes then touch one place.

5. Common errors

SymptomCauseFix
Editing the motor module breaks the line elsewhereOthers referenced its internalsPrivatise internals; route through the interface area
Requests occasionally execute twiceLevel-triggered request, no edgeEdge trigger + acknowledge handshake
Module waits hang foreverHandshake without timeoutAdd timeout and timeout alarm
One interlock rule changed in three placesInterlocks scattered across modulesConsolidate into the interlock module
Interface area decays into chaosIt became the second junk drawerPartition the interface area too, ownership per word

6. Notes

Decoupling is "containing the blast radius of change": private variables confine a change's effects inside the module; the interface area makes dependencies explicit (who depends on whom, visible at a glance); the handshake protocol fixes the timing relations (no more relying on scan-order coincidence). With the three mechanisms up, big programs go from "change one, break many" to "change one, move one".

7. References

Modularity and information hiding are general software-engineering practice, landed in PLCs as FB instances + interface DBs (following IEC 61131-3 POU reuse); request-acknowledge with timeouts is standard industrial-communication convention; safety interlock implementation still follows GB 5226.1 / IEC 60204-1 — modularising control never moves the hard-wired safety requirements.

8. Cautions

⚠️ Three traps to avoid:
① The interface area becoming a public junkyard everyone writes — decoupling in name only;
② Handshakes without timeouts — one stuck module halts the whole line's waiting logic;
③ Safety interlocks "modularised" into control logic, forgetting the hard-loop requirement.

Three review questions for every new module: do its internals leak anywhere? does its handshake carry edge + acknowledge + timeout? are the safety interlocks still hard-wired? Three yeses and the module is "plugged in", not "welded in".

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL