LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Implement Start/Run/Stop Tasks in a DLL Without Using Loops in LabVIEW?

Hi everyone,
I'm developing a custom component for my company using C DLLs + LabVIEW, and I need some clarification on the expected behavior.

Goal

I want to create a simple component with three tasks, all implemented in a C DLL:

  1. Start Task — runs once when the component starts

  2. Run Task — runs continuously and outputs data every iteration

  3. Stop Task — runs once when termination is triggered, and it must stop the Run Task

These three functions will be wrapped into a custom subVI that a client user will place inside their own While Loop in the final application.

The Problem

I was told that inside my subVI’s block diagram I am NOT allowed to use any loops (no While Loop, no For Loop).
But my Run Task must execute continuously

I asked:

If I don’t include any loop inside my subVI, how does the Run Task run continuously?

The answer I received was:

The client user will place your subVI inside THEIR While Loop, so your Run Task will be executed repeatedly by the user’s loop.

 

this is from the vi im doing , i tried to use a for loop but this is not how it must be done , im not allowed to use any type of loops 

thesara_0-1763556489935.png

 

0 Kudos
Message 1 of 17
(412 Views)

The "requirements" that the "calling routine" calls three LabVIEW VIs to Start, Do Once, and End the LabVIEW DAQ task sacrifices the strengths of LabVIEW to run its code concurrently.  Better would be to have the caller send "Start", "Run", and "Stop" commands, where "Run" has the LabVIEW code (how do I say this, run) until it receives the "Stop" command (there might be an extra "loop", but that should be easy to handle).  With luck, this will run concurrently with the rest of the program, preserving accurate timing within the multiple "Run" states.

 

The description of the LabVIEW routine sounds like a State Machine, a very common and powerful LabVIEW construct used for Data Acquisition and Data "Generation" (i.e. Waveforms, etc.) where deterministic timing is important.  If timing is not important (take a sample, wait some ill-defined time, take another sample, wait, etc.), then having the non-timed C loop call the well-timed LabVIEW function can be employed, but timing will (probably) not be accurate.

 

Bob Schor

0 Kudos
Message 2 of 17
(394 Views)

@thesara wrote:

I was told that inside my subVI’s block diagram I am NOT allowed to use any loops (no While Loop, no For Loop).
But my Run Task must execute continuously

I asked:

If I don’t include any loop inside my subVI, how does the Run Task run continuously?

The answer I received was:

The client user will place your subVI inside THEIR While Loop, so your Run Task will be executed repeatedly by the user’s loop.

 

this is from the vi im doing , i tried to use a for loop but this is not how it must be done , im not allowed to use any type of loops 

thesara_0-1763556489935.png

 


 

According to your picture, the iteration is done in the caller, all three modes of your dll must do their task and return immediately. the Run mode is called continuously, not running continuously internally. I guess that's how they are planning to use it. If everything should be done with the same dll, you simply need an input that defines the desired mode. (e.g. an Enum with 0:Start 1:Run, 2:Stop)

 

Of course what you show us is one ugly VI with a completely pointless value property node. Using the terms "iteration" and "no loops" in the same paragraph is contradictory and silly. 

 

Maybe you want to go back and fully show the requirements document. How are they planning to use the code? What's the purpose? How much LabVIEW knowledge and experience do you and they have?

0 Kudos
Message 3 of 17
(363 Views)

Well it seems like all they want you to do is put the proper DLL inside a "LabVIEW wrapper" (a VI)

 

I would make three separate VIs using the parts I circled 

newCapture.PNG

 

You could name them "Start Task.VI", "Run Task.VI", and "End Task.VI"

 

Don't use the Property Node in the Run Task VI just a standard indicator.

 

Then it's up to your client to use them properly in their program. 

 

I have to add that you are basically making a "LabVIEW Instrument Driver" and you might want to take a look at the Instrument Driver Guidelines for tips

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 4 of 17
(344 Views)

i dont understand the requirnment exactly but what i have been told is :

your system should detect whene to start based on the excution button of labview and then excute starting task one time only and then the run task will start excuting continuesly and it will stop with the stop excutin button of the labview , i cant just put the running task inside a while loop cus its not eparable ,its one subvi :

thesara_0-1763892426815.png

so i dont really understand what to do eaxctly 

0 Kudos
Message 5 of 17
(267 Views)

i want to implement an s function style block in labview , how ?

0 Kudos
Message 6 of 17
(278 Views)

Hi Theresa,

 


@thesara wrote:

i want to implement an s function style block in labview , how ?


What have you tried so far and where are you stuck?

 

(Maybe the CDS module might be useful, where the "S" abbreviates "Simulation".)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 17
(276 Views)

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:

  1. No loops or buttons inside the individual VIs (Start.vi, Run.vi, Terminate.vi).

  2. The user should only interact with a single VI (The.vi) — no wiring multiple VIs.

  3. Execution is controlled entirely by LabVIEW’s Run and Stop Execution buttons.

  4. Only the top-level user VI can contain a While Loop.


Solution Overview

  1. Wrap each DLL in a simple VI:

    • Start.vi → calls Start() DLL

    • Run.vi → calls Run() DLL

    • Terminate.vi → calls Terminate() DLL

  2. 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 → calls Start.vi, outputs RUNNING to feedback node

      • RUNNING → calls Run.vi, outputs RUNNING to feedback node

      • TERMINATE → calls Terminate.vi, outputs TERMINATE to feedback node

    • No loops or controls inside this VI; it executes one step per call.

  3. 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.vi for 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

  1. First call → Feedback Node state = INIT → Start.vi executes → state updates to RUNNING

  2. Subsequent calls → Feedback Node state = RUNNING → Run.vi executes repeatedly

  3. 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?

 
 
0 Kudos
Message 8 of 17
(239 Views)

Hi Theresa,

 


@thesara wrote:
  1. 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 → calls Start.vi, outputs RUNNING to feedback node

      • RUNNING → calls Run.vi, outputs RUNNING to feedback node

      • TERMINATE → calls Terminate.vi, outputs TERMINATE to feedback node

    • No loops or controls inside this VI; it executes one step per call.


This sounds like a typical "action engine" aka "LV2-style global" aka "functional global variable": this is a very simple VI pattern and easy to implement!

(The "No … controls" is plain wrong as you NEED controls to input data/commands into this VI!)

 


@thesara wrote:

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



These are basic LabVIEW features/techniques so you should have heard about them in your school lessons (or in basic LabVIEW courses as offered in the header of the LabVIEW board)…

 

Again: what have you tried and where are you stuck?

(Especially as you already got a very specific Solutions item list!)

 


@thesara wrote:

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?


Yes, I want YOU to create YOUR VI.

It's your homework, so please show some effort on your own…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 17
(230 Views)

@thesara wrote:

i want to implement an s function style block in labview , how ?


Start with defining what an s function is?

Do you have the formula? What are the inputs and outputs?

 

I have no idea what you mean by "style"? Are you talking about icon design?

0 Kudos
Message 10 of 17
(215 Views)