I needed to create a single block VI in LabVIEW that behaves like a MATLAB S-Function:
-
Initializes once at the start
-
Executes repeatedly (Run.vi) on every call
-
Terminates once at the end
Constraints:
-
No loops or buttons inside the individual VIs (
Start.vi,Run.vi,Terminate.vi). -
The user should only interact with a single VI (
The.vi) — no wiring multiple VIs. -
Execution is controlled entirely by LabVIEW’s Run and Stop Execution buttons.
-
Only the top-level user VI can contain a While Loop.
Solution Overview
-
Wrap each DLL in a simple VI:
-
Start.vi→ callsStart()DLL -
Run.vi→ callsRun()DLL -
Terminate.vi→ callsTerminate()DLL
-
-
Create
The.vi(S-function-style VI):-
Uses a Feedback Node to store current state (
INIT,RUNNING,TERMINATE). -
Uses a Case Structure with Enum-based selector:
-
INIT→ callsStart.vi, outputsRUNNINGto feedback node -
RUNNING→ callsRun.vi, outputsRUNNINGto feedback node -
TERMINATE→ callsTerminate.vi, outputsTERMINATEto feedback node
-
-
No loops or controls inside this VI; it executes one step per call.
-
-
Top-level user VI (
UserTop.vi):-
Contains a While Loop that repeatedly calls
The.vi. -
Provides the “forever execution” behavior.
-
Optional small Wait (ms) to avoid CPU spinning.
-
On loop exit, optionally calls
Terminate.vifor cleanup.
-
Key LabVIEW Techniques
-
Feedback Node: preserves state across calls
-
Enum-based Case Structure: allows multiple states (INIT, RUNNING, TERMINATE)
-
Case Structure output tunnel: wired to Feedback Node → determines next state
-
No internal loops in The.vi ensures the VI remains a single-call S-function style block
Execution Flow
-
First call → Feedback Node state = INIT → Start.vi executes → state updates to RUNNING
-
Subsequent calls → Feedback Node state = RUNNING → Run.vi executes repeatedly
-
When top-level loop stops → TERMINATE → Terminate.vi executes once
This approach mimics the MATLAB S-Function behavior perfectly: initialization, execution per timestep, and termination — all controlled externally by the top-level While Loop.
Advantages
-
Single block for the user → no multiple VIs to wire
-
Clear separation of states → easy to maintain
-
Compatible with DLL-based instrument drivers
-
No internal loops or user input needed → ideal for real-time applications
If you want, I can also draw a diagram for the post showing Feedback Node → Enum Case Structure → DLL calls, which will make it much easier for others on NI Community to understand your implementation visually.
Do you want me to make that diagram too?