Execute Command Line Arguments from LabVIEW

Updated Dec 10, 2020

Environment

Software

  • LabVIEW

Operating System

  • Windows
  • Mac OS X
  • Linux

You can execute command line code from LabVIEW using the System Exec VI. This tutorial walks you through configuring the System Exec VI. 

For questions on commands and their syntax, consults your OS manufacturer's documentation. For instance:

  1. Launch LabVIEW and open a new VI (File >> New VI)
  2. On the block diagram, place a System Exec VI (Functions palette >> Connectivity >> Libraries & Executables >> System Exec VI)
  3. Right-click the command line terminal and select Create Constant. Input the argument needed to launch the command line (e.g. For Windows, cmd)
  4. Right-click the standard input terminal and select Create Constant. To the string constant, add the command line arguments you want to run.
    • Determine whether you would like LabVIEW to open the command prompt window.
      • By default, the command prompt window does not open. LabVIEW will open it in the background, run the command, and return the result. This can be adjusted by using /k before the code sent to the command line (see example below).
    • In certain circumstances, it is necessary to include the new line character (\r\n) after the command.  The termination character that you should use will depend on your operating system.
    • You can run multiple commands by using the & character.
  5. Determine whether you would like to halt code execution until the command prompt returns a result. Configure wait until completion? parameter accordingly.
    • By default. LabVIEW will wait until the result is returned before continuing with the program (i.e. wait until complete? is true by default).
  6. (Optional) Continue configuring the VI, using LabVIEW content Help as a guide for the other non-required parameters.
  7. (Optional) To view the command line output in LabVIEW, right-click the standard output and select Create Indicator.

The following code executes the dir command on the C:\ directory and outputs the result to a String Indicator. The command line does not open, and the program halts code execution until the command has returned results.

 Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram. 

Alternatively, the implementation shown below will open a command prompt window and execute the dir command on the C:\ within the command prompt. The /k argument forces the command prompt window to stay open so that you can see the results of the dir command. Using this method, LabVIEW and the command prompt window will run asynchronously. In other words, after starting the command window and passing the command to it, LabVIEW will continue executing the next VI in your code while the command prompt window responds to the dir command.  

 Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.