« Close

Datasheets and User Guides

App Notes

Software & Driver

 

8.5 - Error Handling in Scripts

OnAlert works great for errors triggered by Channel Timing, by streaming, and to handle disconnects. If, however, you are using the various LabJack functions like AddRequest(), GoOne(), eGet(), and others in script, then an alert won't appears in the Command / Alert window, and OnAlert won't be called. When working in script, then, you have two other choices. The first is to simply look at the return value for each call to AddRequest(), GoOne(), GetResult(), etc. If an error occurred in calling the function, the error code is returned. Since 0 is LJE_NOERROR, which also means "false" in DAQFactory, we can simply look for a non-zero (or "true" in DAQFactory) value:


private err
err = AddRequest(...)
if (err)
   // error occurred! 
endif

This method is OK for single functions like eGet and ePut, but can get real cumbersome if you have a lot of function calls. Fortunately, you can use the GetNextError() function instead. This function allows you to cycle through any errors in a block of LabJack function calls. A block is a group of AddRequest() followed by a single GoOne() and optionally any GetResult() calls. With each successive call to GetNextError(), the next error from the list of errors that occurred in the block is returned along with some details. Once the function returns LJE_NOERROR (which is just 0), then we know that there are no more errors. Here's an example:


AddRequest(0, LJ_ioPUT_CONFIG, LJ_chAIN_RESOLUTION, 14, 0, 0) 
AddRequest(0, LJ_ioPUT_AIN_RANGE, 2, LJ_rgBIP5V, 0, 0) 
AddRequest(0, LJ_ioPUT_AIN_RANGE, 3, LJ_rgBIP5V, 0, 0) 
GoOne(0) 
private err 
private ch 
private io 
while(GetNextError(0,@io,@ch,@err)) 
   ? Format("Error occurred on channel type: %d, io type: %d, code: %d",ch,io,err) 
endwhile 

The first three lines are the calls to the LabJack driver to configure the first found LabJack. We then declare three private variables to hold our error information. The GetNextError() function shows our first example of passing variables by reference in DAQFactory, which is used in many of the LabJack functions. Putting the @ in front of the parameter when calling GetNextError() indicates that the reference to that variable is being passed to the function, allowing the function to actually change the value of that variable. GetNextError() is a little unique in that it both returns the error code and sets a variable to the error code in a single call. This allows us to look for LJE_NOERROR in our loop, while still retrieving the error code.


while(GetNextError(0,@ch,@io,@err)) 

This line loops for as long as GetNextError() returns a non-zero value, meaning there are errors. If no errors occurred, the code inside the loop will not execute. If an error did occur, the I/O type (for example, LJ_ioPUT_AIN_RANGE), the channel (for example 2), and the error code are put in our private variables. The next line:


    ? Format("Error occurred on channel type: %d, io type: %d, code: %d",ch,io,err) 

Simply prints a formatted message with those values. Please see the DAQFactory User's Guide on using the Format() function. You may want to perform something more sophisticated when an error occurs.

If you want to the same error handling for all your script, you can put the error handling code in a separate function.

To do so, create a new sequence and put the error handling code there:


private err 
private ch 
private io 
while(GetNextError(0,@io,@ch,@err)) 
   ? Format("Error occurred on channel type: %d, io type: %d, code: %d",ch,io,err) 
endwhile 

Then, when you want to check for errors, simply call the sequence as a function. For example, if we called our new sequence "ErrorHandler", our original script would become:


AddRequest(0, LJ_ioPUT_CONFIG, LJ_chAIN_RESOLUTION, 14, 0, 0) 
AddRequest(0, LJ_ioPUT_AIN_RANGE, 2, LJ_rgBIP5V, 0, 0) 
AddRequest(0, LJ_ioPUT_AIN_RANGE, 3, LJ_rgBIP5V, 0, 0) 
GoOne(0) 
ErrorHandler() 

Note: A single call to eGet() or ePut() or any of the other e functions is a block, so you should stick with looking at the return value of these functions to determine if an error occurred.

This type of error handling is demonstrated in the example files for all the LabJack specific examples in the following sections.