Debug

Different ways are available to find errors within a TcCOM module created with MATLAB®/Simulink®, or to analyze the behavior of the module within the overall architecture of the TwinCAT project.

Debugging in the block diagram

If the block diagram was exported during generation of the TcCOM module, it can be displayed in the TwinCAT development environment and used for debugging within the corresponding module instance, for example. To do so, the block diagram uses the Microsoft Visual Studio debugger, which can be linked with the TwinCAT runtime via the TwinCAT debugger port. Attach the debugger as described in the C++ section under Debugging.

Prerequisites for debugging within the block diagram are:

Setting breakpoints in the block diagram

1. After attaching the debugger to the TwinCAT runtime, the possible breakpoints are assigned to the blocks in the block diagram and represented as points. Clicking on the desired breakpoint activates it, so that execution of the module instance is stopped next time the associated function block is executed. The color of the point provides information about the current state of the breakpoint:
2. Additional information, such as the corresponding C++ code section, can be found in the tooltip for the breakpoint:
Debug 2:
Debug 3:

Breakpoints are not always assigned to a single function block. In many cases, the functions of several blocks are consolidated in a code section or even a line in the underlying C++ code. This means that several blocks can share the same breakpoint. Therefore, activation of a breakpoint in the block diagram may also result in changes in the point display in other blocks.

Evaluating exceptions

If exceptions occur during processing of a TcCOM module, such as division by zero, the point at which the exception occurred can be shown in the block diagram. To this end, the TcCOM module must meet the above requirements, and the C++ debugger must be enabled in the TwinCAT project (TwinCAT 3 C++ Enable C++ debugger). After the debugger has been attached, which may be done before the exception has occurred or indeed after, the block that caused the exception is highlighted in the block diagram, provided the line of code responsible for the exception can be allocated to a block. The name of the function block is shown in red, and the function block itself is marked in bold.

Debug 4:

Manual evaluation of exceptions without source code

Even if the module source code is not available on the engineering system or the C++ debugger was not activated, you can highlight the error location in the block diagram once an exception has occurred.

Typically, an error message will always be generated when an error occurs, indicating the source file and the line in the source code. In many cases, this information can be used to allocate an exception to a block in the block diagram. To do this, you can proceed as follows:

A prerequisite for highlighting the error location within the block diagram is that debug information was generated (option Export block diagram debug information in the coder settings under Tc Advanced).
3. From the context menu of the block diagram select the entry Provide exception data:
Debug 5:
4. In the dialog that opens, enter the source code file and line number provided in the error message:
Debug 6:
5. The name of the function block associated with the line number is displayed in red, and the function block itself is marked in bold:
Debug 7: