Often, when you get stuck in HALCON with an exception thrown by an operator, it is helpful to take a look at the low level error messages. These messages provide additional information about the problem. For example, when you are trying to connect to a GigE Vision camera for the second time, HALCON returns an error message which tells you that the “device cannot be initialised”. If we take a look at the low level errors, we get the hint that “maybe the device is already in use”. So let us take a look at the options HALCON provides to retrieve these useful low level error messages.
If you are working with HDevelop, the low level error messages are automatically logged in the background. You can access the log of the latest low level error messages via the menu Window ? Open Output Console. All HDevelop messages are logged in this output console. These messages contain information about the execution of the HDevelop script, low level errors as well as HALCON errors. The low level errors are indicated by a small yellow rectangle.
In many cases, it can be useful to get an immediate response when the low level error message appears, and we do not want it to be logged in the output console only. Here, the low level error messages can be activated via the operator set_system(‘do_low_error’, ‘true’).
Low level errors will then be shown in a Windows message box and pause the program as soon as they occur on Windows systems.
These message boxes are a nice way to get the information directly. If you do not want to interrupt the running program and cannot monitor the Output Console constantly, HALCON can write the low level error messages directly into a text file.
To do so, first open a text file with the HALCON operator open_file. In order to tell HALCON that the low level error messages should be written to this text file you hand over a tuple to set_system(‘do_low_error’,…) consisting of the string ‘file’ and the FileHandle. Please keep in mind that before your application or HDevelop script ends you have to deactivate the low level errors and also close the opened file.
open_file (‘LowLevelErrors.txt’, ‘output’, FileHandle)
set_system (‘do_low_error’, [‘file’, FileHandle])
* Your HALCON code …
set_system (‘do_low_error’, ‘false’)
close_file (FileHandle)
In some applications writing logs to the hard drive is no option since it can take too long. For such scenarios it is even possible to define a callback function that is called whenever a low level error appears. Note that this is only possible in the HALCON language interfaces but not in HDevelop. So let us have a look at how to register a callback to receive the low level error messages in C++. First, we have to define a function that handles the low level error message:
Herror LowErrorCallbackProc(const char* err_text)
{
// Do something with the low level error message
return H_MSG_OK;
}
To register this function as low level error callback you have to call the following code from your C++ application:
HTuple LowLevelCallback = “callback”;
LowLevelCallback.Append((Hlong)LowErrorCallbackProc);
SetSystem(HTuple(“do_low_error”), LowLevelCallback);
Whenever a low level error message appears, the previously defined function will be called with the low level error message as argument.