Example Code

Ensure Serial Resource Closed When VI Closed from Exit Glyph

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

    Driver

  • NI VISA

Code and Documents

Attachment

 

Overview
The purpose of this VI is to ensure that the VISA resource is closed even if the "X" glyph on the upper right hand corner is used to stop a running VI.

 

Description
This code was developed in order to be sure that the VISA resource was closed whether the stop button was pushed or the X button was pushed on the upper right hand corner of the window to close the VI. The base program can read and write from a serial port during timeout events of an event structure, but will check for a close action from the user before timeout (every 5ms). If the user choses to stop or close the window, the COM port reference is automatically closed before the window exits. This will ensure that any kind of resource allocation issues will take place if the VI is exited without first stopping.

Requirements

Software:

  1. LabVIEW 2013 (or compatible)

Hardware:

  1. Serial Port

Steps to Implement or Execute Code

  1. Configure the COM port that you want to communicate with., You can change the serial settings if needed.
  2. Try writing and/or reading a serial command.
  3. Run the application.
  4. Stop the VI.
  5. Re-run the VI.
  6. Close the VI while running from the "X" glyph in the upper right corner.

 

Additional Information or References

To verify that the reference is closed, you can run the I/O Trace application while the VI is open. You should see the COM closed step at the end of the I/O Trace log as shown below.

VI Snippet

Serial Write Read and Stop 2013 NIVerified.png

 

 **This document has been updated to meet the current required format for the NI Code Exchange.**

Stephanie S.
Application Engineer
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.