LabWindows/CVI

Error Checking

LabWindows/CVI functions return status codes in one of two ways—either by a function return value or by updating a global variable. In some cases, LabWindows/CVI uses both of these methods. In either case, it is a good idea to monitor these values so that you can detect an error and take appropriate action. You can monitor the status of functions, and when a function reports an error, pause the program and report the error to the user through a pop-up message. For example, LoadPanel returns a positive integer when it successfully loads a user interface panel into memory. However, if a problem occurs, the return value is negative. The following example shows an error message handler for LoadPanel.

panelHandle = LoadPanel (0, "main.uir", PANEL);

if (panelHandle < 0) {

ErrorCheck ("Error Loading Main Panel", panelHandle, GetUILErrorString (panelHandle));

}

When a function reports status through a separate function, as in the RS-232 Library, check for errors in a similar way. In this case, the status function returns a negative value when the original function fails.

bytesRead = ComRd (1, buffer, 10);

if (ReturnRS232Err() < 0) {

ErrorCheck ("Error Reading From ComPort #1", ReturnRS232Err(), GetRS232ErrorString(ReturnRS232Err()));

}

Notice that the preceding function also returns the number of bytes read from the serial port. You can compare the number of bytes read to the number you request, and if a discrepancy exists, take the appropriate action. Notice that the error codes differ between the RS-232 Library and the User Interface Library. The Status Reporting by LabWindows/CVI Libraries and Instrument Drivers section describes how each LabWindows/CVI library reports errors.

After your program detects an error, it must take some action to either correct the situation or prompt the user to select a course of action. The following example shows a simple error response function.

void ErrorCheck (char *errMsg, int errVal, char *errString)

{

char outputMsg[256];

int response;

Fmt (outputMsg, "%s (Error = %d).\n%s\nContinue? ",

errMsg, errVal, errString);

response = ConfirmPopup ("Error Check", outputMsg);
if (response == 0)

exit (-1);

}