Example Code

My New Years Resolution is to use the default case as a catch, and not as an implementation.

Code and Documents

Attachment

Introduction

The purpose of this example is to demonstrate the best practice of only using the default case in a case structure to catch an unexpected occurrence, and recover gracefully from it. This example is based upon a TCP Server and Client. The client connects to the server and is able to pass it a series of messages which the Server will respond to depending upon the implementation of the case structure. This implementation causes the state of a second loop to change depending upon the message passed to it.

What is a “catch”?


A ‘catch’ in LabVIEW, as well as other languages, is a piece of code that deals with exceptions to what is normal when an application is running. This special code deals with anomalous events that require special handling so as to not disrupt the running of the application, and to reset the application to a safe state, or recover gracefully and restart normal operation of the program.

The Default Case


The default case of a case structure is the case where any non-explicitly implemented case is dealt with. Any non-explicitly specified stimulus can be seen as anomalous, and suitable implementation can be defined within the default case to act as exception handling.

Example Overview


This example is based around a TCP Client and Server. The Client initiates a connection to the server and is able to pass a series of messages to it. The server will then act accordingly based upon the implementation defined in the case structure. If it recognises the message, and this corresponds to a defined case, then it will change the state of the application based upon this and respond to the client. If the message is not recognised by the server, it will respond to the client notifying it that the instruction is invalid, and will reset the application into a safe state by using the default case of the case structure.

The benefits of this are twofold, of stability and maintainability. If you have defined a method of handling unexpected and anomalous inputs, this lends itself to a more stable and robust application. Maintainability is improved as you can encapsulate this exception handling inside the case structure. 

Instructions for Running


  1. ) Open the DefaultCaseServer and DefaultCaseClient VI’s.
  2. ) Run the DefaultCaseServer vi.
  3. ) Run the DefaultCaseClient vi, and select messages to send to the server.
  4. ) Note the response on the server VI in response to the three messages. Using “Message_One” or “Message_Two” will result in valid operational states. “Message_Three” will result in an error response and the server being reset to a safe state.

Requirements


Hardware: None

Software: LabVIEW 8.6 Minimum

VI Snippet:

Client:

DefaultCaseClient.png

Server:

DefaultCaseServer.png

My Other New Year's Resolution is to...

James Kent | Applications Engineer | National Instruments UK & Ireland
w: uk.ni.com | ireland.ni.com

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

Contributors