How to Go Back and Execute a Section of LabVIEW Code Again If a Condition Is Met

Updated Aug 21, 2023

Reported In

Software

  • LabVIEW Base
  • LabVIEW Full
  • LabVIEW Professional
  • LabVIEW for Education
  • LabVIEW NXG

Issue Details

I have a LabVIEW program where I need to go back in my code and execute a section of code that has already executed if a particular condition is met. I have not been able to achieve this using cables. How can I get my code to do this?

Solution

In order to get a LabVIEW program to repeat the execution of a block depending on conditions that change during runtime, the state machine design pattern can be used. The state machine is composed by states that are represented by the code in each case of a Case Structure in LabVIEW, and the result of the execution of each case can be used to define the next state to execute. A change in any condition of your interest can be evaluated during any state and it can be used to get the program to transition back to the case in which the code you want to execute again was placed. 

To understand the general structure of this design pattern, refer to Using a State Machine (Event Driven) Architecture. In LabVIEW, you can either generate the state machine code from scratch or use the template project. To create a project from the template:
  1. Go to File>>Create Project or click the Create Project option from the LabVIEW splash screen.
  2. In the Create Project window, click on Templates on the left and then choose Simple State Machine from the options on the right.
  3. Click Next and define a Project Name and the project location in the Project Root field. 
  4. Click Finish and open the Main VI file.
  5. Proceed to replace the components in the Front Panel and Block Diagram as suggested in the template. 

 

Additional Information

LabVIEW follows a Data Flow model that enables the user to control the execution order of the code through the connection of wires in between sections of code. Each section of code will execute only when all its inputs are available and will provide output data until all the code in the structure is done executing. This means that any section of code that has already executed cannot be forced to execute again through cables, since this would break the data flow described. Additional structures like While Loops and Case Structures need to be used to change the execution flow and repeat the execution of code as required.