« Close

Datasheets and User Guides

App Notes

Software & Driver

 

8.3 - Error Handling with OnAlert

There are several ways to handle LabJack errors from within DAQFactory. The correct way depends a bit on what you are doing and whether you actually want to do something if an error occurs.

If you are just performing basic I/O using Channels without any scripting, then any LabJack errors will appear in the Command / Alert window. Streaming errors also appear this way. If you cannot see the Command / Alert window, go to View - Command / Alert from the DAQFactory main menu. This allows you to see the errors, but you can't perform any automated action if an error occurs.

If you need to monitor errors automatically and perform an action when one occurs, then you should use the OnAlert DAQFactory event. As an example, here is how you would make the background of Page_0 turn red if a LabJack error occurs, resetting to white with a button press acknowledgement:

1) Right click on SEQUENCES: in the Workspace and select Add Sequence. Call the new sequence OnAlert

2) In the sequence editor that appears, enter the following script:


if (find(strAlert,"D0050:",0) != -1) 
   page.page_0.backcolor = rgb(255,0,0) 
endif 

3) Click on Apply and Compile to save your changes

Now, to acknowledge the error and make the screen white again, we need a button:

4) Click on Page_0 in the Workspace under PAGES:

5) Right click on the page somewhere and select Buttons & Switches and then Button

6) Right click on the new button and select Properties.... Type Acknowledge for the Caption

7) Click on the Action tab, then select Quick Sequence for the action.

8) In the sequence editor under Quick Sequence enter:


page.page_1.backcolor = rgb(255,255,255) 

9) Click OK to save your changes.

Now if you have a few channels reading from the LabJack and an error occurs, Page_0 will turn red until the error stops and you click on the Acknowledge button you created. The easiest way to see this is to simply unplug the LabJack, then plug it back in.

Sample file: LJGuideSamples\OnAlert.ctl

How it works:

If you create a sequence called OnAlert, it will be called whenever a new alert is generated by DAQFactory and passes in strAlert as a private variable, which is the actual error message. Alerts almost always start with a letter and 4 digit code. For device alerts, the letter is always "D" and the 4 digit code is unique to the particular device. In the case of the U3 / UE9 driver, it is always 0050. The rest of the message can vary, but for the LabJack, you will get an error code afterwards which can be used to further parse for a particular code. The format of LabJack errors is:

D0050:yy:xxxx: Message

where xxxx is a 4 digit error code, and y is the device ID / number that caused the error, or 99 if its a generic, non-device specific error.

The exception to this are errors that occur from Channel Timing. These errors have the timing information in front.

So, in our OnAlert sequence, we look for the string "D0050:" and if it is found, then we know its a LabJack error. Find() returns -1 if not found and a positive number otherwise. Then we simply set the background color of Page_0 to red using the RGB() function. The RGB() function takes three parameters, the amount of red, the amount of green and amount of blue, each from 0 - 255, and returns the full color value.

When using Channels to read values from your LabJack, DAQFactory will continually retry the read at the Timing interval you specified in the channel even if an error occurs. Because of this, you can end up with the same error over and over again. This is why DAQFactory displays errors in the alert window and doesn't pop up a separate window as many applications would. When you are doing error handling with OnAlert, you have to keep this in mind. You would not, for example, want to fire off an email with every error. If you were reading an input once a second and the LabJack was unplugged accidentally for 3 minutes, DAQFactory would send 180 emails, one for each failed read.

Since sending emails on error is a common task, here is probably the best way to solve the repetitive email dilemma: instead of firing off an email with every alert, add each new alert to the body and have a second sequence send out the email every so often if alerts have occurred. The details of sending email from DAQFactory are described in the next chapter, but to build up the email body you'd do this in the OnAlert:


if (find(strAlert,"D0050:",0) != -1) 
   strEmailBody += strAlert + chr(13) + chr(10) 
endif 

The chr(13)+chr(10) add a carriage return / line feed to the end of each alert so that you don't get all your alerts strung out on a single line in your email.

Now you just need a sequence to fire off the email every so often, say 30 minutes. To do so, create a new sequence, probably marked AutoStart, with the following script:


global string strEmailBody = ""  // initialize the variable for accumulating errors
// put common email configuration stuff here:
email.strHost = "mail.mymail.com"
email.strUser = "[email protected]"
email.strPassword = "password"
email.strTo = "[email protected]
email.strFrom = "[email protected]"
// initial configuration done, so loop forever
while(1)
   // check if an alert has occurred
   if (strEmailBody != "")
      // if so, copy alerts into email body 
      email.strBody = strEmailBody
      // reset variable for new alerts
      strEmailBody = ""
      // and send email
      email.Send()
   endif
   // wait 30 minutes and try again
   delay(1800)
endwhile

Note that our OnAlert script will probably generate its own errors until you run our email sending sequence. This is because the email sending sequence declares the strEmailBody variable, which until then doesn't exist and so we get errors in OnAlert.