Section 4 Debugging Techniques 调试技术
推荐给好友
打印
加入收藏
更新于2008-05-16 18:43:25

Debugging Techniques

调试技术

Have you ever written a program with no errors in it? LabVIEW has many built-in debugging features to help you develop your VIs. This section explains how to use these conveniences to your best advantage.

你是否编写过一个不存在错误的程序呢?LabVIEW有内置很多调试工具可以帮助你完善你的VI程序。这一节介绍如何使用这些方便的工具使你的程序更为完善。

Fixing a Broken VI

修改一个不完整的VI



Run Button (broken)

运行程序(不完整)

A broken VI is a VI that cannot compile or run. The Run button appears as a broken arrow to indicate that the VI has a problem. It's perfectly normal for a VI to be broken while you are creating or editing it, until you finish wiring all the icons in the diagram. Sometimes you may need to Remove Broken Wires (found in the Edit menu) to clean up loose wires, but be careful not to delete wires you want!

一个不完整的VI无法编译或运行。运行按钮呈现为一个破损的箭头,表示VI存在问题。当你创建或编辑一个程序时,直到你完成所有程序框图中的图标和连线之前,它会一直显示为断裂状态。有时你可能需要用删除断线(在编辑菜单中查找)清除断开的连线,但要当心将你需要的连线也一并删除了!



To find out why a VI is broken, click on the broken Run button or select Show Error List from the Windows menu. An information box titled "Error List" appears, listing all errors for the VI. You can choose to see the error list for other open VIs using a menu ring at the top of the window. To find out more about a particular error, click on it. The Error List window will display more information. To locate a particular error in your VI, double-click on the error in the list or highlight it and press the Show Error button. LabVIEW brings the relevant window to the front and highlights the object causing the error (see Figure 5.6).

单击断开的运行按钮或菜单栏的查看 错误列表可以查找VI断开的原因,显示一个名为“错误列表”的信息框,列出VI中的所有错误。你可以点击窗口上部,选择其它打开的VI的错误列表。点击其中一个,可以显示更多的信息,以便更加了解错误细节。单击显示错误按钮或双击错误描述,可高亮显示导致错误的原因。(见图5.6)

Figure 5.6. Error List dialog

Warnings

警告



Warning Button

警告按钮

If you want extra debugging help, you can choose to Show Warnings in the Error List window by clicking in the appropriate box. A warning is something that's not illegal and won't cause a broken run arrow, but does not make sense to LabVIEW, such as a control terminal that is not wired to anything. If you have Show Warnings checked and have any outstanding warnings, you will see the Warning button on the Toolbar. You can click on the Warning button to see the Error List window, which will describe the warning.

如果你想获得更多的调试帮助,你可以选择错误列表窗口中的显示警告选项。一条警告并非错误也不会导致程序无法运行,但对于LabVIEW也没有什么意义,例如一个没有任何连线的控制终端。如VI 中含有警告且错误列表窗口中的显示警告复选框被选中,工具栏将包含警告按钮。通过点击警告按钮你会看到描述错误的错误列表窗口。

You can also configure LabVIEW's options to show warnings by default. Go to the Debugging section in the Options dialog box (accessed by selecting Tools>>Options . . .) and check the Show warnings in Error List by default box.

你也可以通过配置LabVIEW选项默认为显示警告。进入选项对话框中的调试部分(通过选择工具>>选项. . .)检查错误列表中的显示警告为缺省项。

Most Common Mistakes

常见的错误



Certain mistakes are made more frequently than others, so we thought we'd list them to make your life easier. If your Run button is broken, one of these might describe your problem. Please see Appendix E, "Resources for LabVIEW" for a link to the LabVIEW FAQ, a great on-line resource containing answers to frequently asked questions.

有一些错误出现的频率更多,所以我觉得应该将它们列举以便于大家。如果运行按钮处于断裂状态,或许会是其中的原因之一。请参见附录E,“LabVIEW资源”链接到LabVIEW常见问题解答,一个在线的包含常见问题解答的重要资源。

  • A function terminal requiring an input is unwired. You cannot leave unwired functions on the diagram while you run a VI to try out different algorithms.
  • 一个必需连接的函数接线端没有连线。不能再程序框图中放置一个未连线的函数,若运行VI会造成一个错误的算法。
  • The block diagram contains a bad wire due to a data-type mismatch or a loose, unconnected end, which may be hidden under something or is so tiny that you can't see it. The Remove Broken Wires command from the Edit menu eliminates the bad wires, but you might have to look a little harder to find a data-type conflict.
  • 框图程序包含的数据类型不匹配或不确切,未连接端口,它们可能隐藏或由于过于细小,常被人忽略。编辑菜单中的删除断线命令可以删除断线,但你会发现数据类型冲突是很难查找的。
  • A subVI is broken, or you edited its connector after placing its icon on the diagram. Use the Replace or Relink to subVI pop-up option to re-link to the subVI.
  • 子VI处于断开状态或是在程序框图中放置了图标后编辑了该子VI的连线。使用替换或重新连接子VI选项重新链接子VI。
  • You have a problem with an object that is disabled, invisible, or altered using a property node (which we'll talk more about in Chapter 13).
  • 一个无法使用、不可见或更改了使用权限的节点也会成为问题。(这一点我们将在13章进行更多讲述)
  • You have unwittingly wired two controls together, or wired two controls to the same indicator. The Error List window will bear the message, "You have connected a Control to a Control. Change one to an indicator," or "This wire connects more than one data source," for this problem. You can often solve it by changing one of those controls to an indicator.
  • 无意中将两个输入控件或显示控件连在一起。错误列表会显示信息,“你将一个输入控件与输入控件连接请将其中一个改为显示控件”或“这条连线连接了不止一个数据源,”对于这种问题,你可以通过修改其中一个输入控件为显示控件解决。
  • A subVI cannot be found, either because you don't have it (maybe someone sent you a VI without including the subVI) or because you changed the name of the subVI on disk and the calling VIs are not aware of the name change. When you change a VI's name using the Rename option of the File>>Save As . . . menu option, any caller in memory will "feel" the change and relink to the renamed VI. You must save each caller of the renamed subVI in order for the caller to "remember" the new name and location of the renamed subVI, the next time the caller is loaded from disk.
  • 一个子VI无法找到,也许是因为你就没有子VI(可能别人给你的VI不包含子VI)或许是你改变了磁盘中子VI的名称,调用VI没有察觉其名称的改变。当你使用重命名(文件>>保存为. . .菜单选项)改变一个VI的名称时,在内存中的调用程序会“得知”这一改变并重新链接至重命名子VI。你必须保存每一个调用重命名子VI的程序,以使调用程序“记得”重命名子VI新的文件名和路径,便于下次调用程序由磁盘装载程序。

Single-Stepping Through a VI

单步执行通过一个VI



Pause Button

暂停按钮

For debugging purposes, you may want to execute a block diagram node by node. Nodes include subVI's, functions, structures, code interface nodes (CINs), formula nodes, and property nodes. To begin single-stepping, you can start a VI by clicking on one of the single-step buttons (instead of the Run button), pause a VI by setting a breakpoint, or click on the Pause button. To resume normal execution, hit the Pause button again.

因为调试目的,你可能需要逐个节点地执行数据框图。节点包含子VI、函数、结构、代码接口节点(CINs)、公式节点和属性节点。你可以通过点击单步执行按钮单步执行程序(代替运行按钮),通过设置断点或点击中断按钮中断VI,再次点击中断按钮程序可以继续执行。

You may want to use execution highlighting (described next) as you single-step through your VI, so you can visually follow data as it flows through the nodes.

你也可以使用高亮执行(后续描述)单步执行整个VI程序,你会实时看到数据在连线和节点上流过的过程。

While in single-step mode, press any of the three step buttons that are active to proceed to the next step. The step button you press determines how the next step will be executed.

处于单步执行模式,点击三个步进按钮中的任何一个都能进入下一步,三个步进按钮各自决定如何执行下一步。



Step Into Button

单步步入按钮

Press the Step Into button to execute the first step of a subVI or structure and then pause at the next step of the subVI or structure. Or, you can use the keyboard shortcut: down arrow key in conjunction with <control> under Windows and <command> on Macs and <meta> on Linux.

点击单步步入按钮执行一个紧接着的子VI或结构,然后在在所执行子VI或结构体内的下一步前暂停。也可以使用键盘快捷键:Windows系统中用<control+↓>、Macs系统中用<command+↓>、Linux系统中用<meta+↓>。



Step Over Button

单步步过按钮

Press the Step Over button to execute a structure (sequence, loop, etc.) or a subVI and then pause at the next node. Or, you can use the keyboard shortcut: right arrow key in conjunction with <control> under Windows and <command> on Macs.

点击单步步过按钮执行一个结构(顺序、循环等。)或一个子VI,然后在下一个节点前暂停。也可以使用键盘快捷键:Windows系统中用<control+→>、Macs系统中用<command+→>。



Step Out Button

单步步出

Press the Step Out button to finish executing the current block diagram, structure, or VI and then pause. Or, you can use the keyboard shortcut: up arrow key in conjunction with <control> under Windows and <command> on Macs.

点击单步步出按钮完成当前执行的程序流程框图块、结构或VI,然后暂停。也可以使用键盘快捷键:Windows系统中用<control+↑>、Macs系统中用<command+↑>。

Execution Highlighting

高亮显示执行过程 



Execution Highlighting Button

高亮显示执行过程按钮

Sometimes it's nice to see exactly where your data is and what's happening to it. In LabVIEW, you can view an animation of VI block diagram execution. To enable this mode, click on the Execution Highlighting button in the Toolbar.

有时,很高兴能看到当前数据在那里发生了什么。在LabVIEW中,你能看到程序框图执行的动画。点击工具栏里的高亮显示执行过程按钮,就能够进入这种模式。

As data passes from one node to another, the movement of data is marked by bubbles moving along the wires. You will notice that highlighting greatly reduces the performance of a VI. Click again on the Execution Highlighting button to resume normal execution. Figure 5.7 shows a VI running with execution highlighting enabled.

如同数据从一个节点通过到下一个节点,数据的运动显示为气泡沿着连线移动。你会发现使用高亮显示时,VI的执行速度会明显降低再一次点击高亮显示按钮会转入标准状态继续执行。图5.7显示正在执行高亮显示状态的VI。

Figure 5.7. The block diagram of a running VI that has Execution Highlighting enabled, showing "data bubbles" flowing through wires and the values of wires appearing in tip strips

图5.7. 正在执行高亮显示状态的VI程序框图,显示“数据泡”沿着连线流动,并且连线的数据值在提示条上显示

Node values are automatically shown during execution highlighting, as in the previous illustration, if you select Auto probe during execution highlighting from the Debugging menu of the Options dialog.

如上图所示,如果在选项对话框中调试菜单上选择高亮显示过程时自动探测值,执行高亮显示时节点的值会自动显现。

You commonly use execution highlighting in conjunction with single-step mode to gain an understanding of how data flows through nodes. When these two modes are used together, execution glyphs on the subVI's icon indicate which VIs are running and which are waiting to run.

通常使用高亮显示和单步执行是为了了解数据流是如何通过节点的。当这两种模式同时使用时,执行子VI图标的图像将显示哪一个VI正在运行,哪个VI还在等待运行。



Be aware that data in independent (parallel) "chunks" of the diagram (those not having data flow dependencies) may not always flow in the same sequence on subsequent executions or iterations. And sometimes "race conditions" that cause problems under normal execution (with execution highlighting turned off) may disappear completely with execution highlighting turned on and appear to behave as "expected," thus hiding the buggy behavior. A race condition refers to a situation in your program where two or more independent processes are trying to write data at the same time to a variable (thus, they "race" against each other). You will see more examples of race conditions, and how to avoid them, in the latter chapters of this book. If you see such quirky behavior, look for race conditions as the possible cause.

要注意到各自独立的数据在程序框图(那些不存在数据流依存关系的)并非总是以同样的次序并发执行或迭代。有时"竞争状态"使得导致问题的原因在执行(高亮显示进程关闭时)中被隐藏,甚至在高亮显示进程开启时完全消失,表现的和所“期望”的一样,因而隐藏了程序错误。竞争状态是指在程序中两个或更多独立的进程都设法在同一时间对同一变量写数据(因此,它们彼此“竞争”)。 我们该如何避免呢?在本书接下来的章节里你会看到更多竞争状态的例子,如果你看到如此奇怪的状况,很可能就是竞争状态造成的。

Setting Breakpoints

断点工具



Don't panicbreakpoints do not "break" a VI; they only suspend its execution (similar to the Pause button) so that you can debug it. Breakpoints are handy if you want to inspect the inputs to a VI, node, or wire during execution. When the diagram reaches a breakpoint, it activates the pause button; you can single-step through execution, probe wires to see their data, change values of front panel objects, or simply continue running by pressing the Pause button or the Run button.



Breakpoint Tool

断点工具

To set a breakpoint, click on a block diagram object with the Breakpoint tool from the Tools palette. Click again on the object to clear the breakpoint. The appearance of the breakpoint cursor indicates whether a breakpoint will be set or cleared.

在工具选板上选取断点工具,单击程序框图上的对象,就放置了断点。再次点击断点即可清除断点。查看断点图标可以判断一个断点是否被设置或清除。

Depending on where they are placed, breakpoints behave differently:

所在位置不同,断点的作用不同:

  • If the breakpoint is set on a block diagram, a red border appears around the diagram and the pause will occur when the block diagram completes.
  • 如果将断点放置在一个框图上,则沿框图周围显示红色边沿而且当框图执行后程序暂停。



    Set Breakpoint Cursor
    设置断点标志
  • If the breakpoint is set on a node, a red border frames the node and execution pauses just before the node executes.
  • 如果将断点设置在节点上,该节点的图标边框变为红色,而且在执行该节点前程序暂停。
  • If the breakpoint is set on a wire, a red bullet appears on the wire and any attached probe will be surrounded by a red border. The pause will occur after data has passed through the wire.
  • 如果将断点设置在连线上,一个红色标记显示在连线上,而且任何一个相连的指针会被红色边框环绕。当数据流过该连线时程序暂停。



    Clear Breakpoint Cursor
    清除断点标志

When a VI pauses because of a breakpoint, the block diagram comes to the front, and the object causing the break is highlighted with a marquee.

当一个VI由于断点暂停时,程序框图会会弹出,并且导致暂停的对象会高亮显示。

Breakpoints are saved with a VI but only become active during execution.

断点能够与VI一起保留,但只在被执行时起作用。



Saving VIs with breakpoints is generally not advised, because they are intended for debugging your VIs. It is easy to inadvertently save a VI after setting a breakpoint. If you accidentally do so, you may be puzzled the next time you or a colleague opens your LabVIEW project and finds that a saved breakpoint is suspending execution of a VI.

通常不建议保存带有断点VI程序,因为它们只是用于调试你的VI的。设置的断点很容易被忽视而将VI保存。如果你偶然为之,可能下次你或你的同事打开LabVIEW对象时会发现,被保存的断点会使VI挂起。



You can easily find breakpoints in your VIs using the Find dialog, which can be opened by selecting Edit>>Find and Replace from the menu or using the shortcut <ctrl-F> (Windows), <command-F> (Mac OS X), or <meta-F> (Linux). From the Find dialog, select Search for: Objects, Object: Others>>Breakpoint, and define your Application Instance and Search Scope options.

可以选择编辑>>查找或是用快捷键<ctrl-F> (Windows), <command-F> (Mac OS X), or <meta-F> (Linux)打开查找对话框。在查找对话框选择查找:对象,对象:其他>>断点 定义你的应用程序实例和查找范围选项,很容易在你的VI中发现断点。

Suspending Execution

执行挂起

You can also enable and disable breakpoints with the Suspend when Called option, found in the Execution menu of VI Properties . . . (which you access from the icon pane pop-up menu in a VI's front panel). Suspend when Called causes the breakpoint to occur at all calls to the VI on which it's set. If a subVI is called from two locations in a block diagram, the subVI breakpoint suspends execution at each call.

你可以在VI属性>>执行中选取调用时挂起,设置程序在被调用时断点是否起作用(由VI的前面板的菜单栏弹出选单中选择进入)。当设置过的VI中被调用断点产生中断时程序挂起。如果子VI在一个程序框图中的两个位置被调用时,则每次调用断点被执行时挂起。

If you want a breakpoint to suspend execution only at a particular call to the subVI, set the breakpoint using the SubVI Node Setup . . . option. Pop up on the subVI icon (in the block diagram of the calling VI) to access this option. You will learn more about VI setup options in Chapter 15, "Advanced LabVIEW Features."

如需中断子VI的一个特定调用,右键单击程序框图中该子VI的节点,并从快捷菜单中选择子VI节点设置。选中调用时挂起复选框,则仅在该子VI的实例中中断执行。在第15章“LabVIEW高级属性”中你将了解更多VI设置选项。

Using the Probe

使用探针



Use the probe to check intermediate values in a VI that executes but produces questionable or unexpected results. For instance, assume you have a diagram with a series of operations, any one of which may be the cause of incorrect output data. To fix it, you could create an indicator to display the intermediate results on a wire, or you can leave the VI running and simply use a probe. To access the probe, select the Probe tool from the Tools palette and click its cursor on a wire, or pop up on the wire and select Probe. The probe display, which is a floating window, first appears empty if your VI is not running (unless you have pressed the Retain Wire Values button, which we discuss shortly). When you run the VI, the probe display shows the value carried by its associated wire. Figure 5.8 shows a VI block diagram with probes placed on two of the wires.

使用探针可以检查VI运行过程中的中间值是否存在问题或是意想不到的结果。例如:假如一个框图内含的一系列操作中的任何一个出错,都可能会导致错误的数据输出。为了更正它,你可以设置一个显示控件来显示数据线上的中间结果,或是简单地使用一个探针。在工具选板上选择探针,然后在数据线上点击,即可插入一个探针。或是在数据线上点击鼠标右键选择探针。当VI没有运行时,探针的显示浮动窗是空的(除非你点击了保留数据线值按钮,这里不多讨论)。当你运行VI时,探针显示窗中会显示相关数据线上的值。如图5.8所示一个程序框图上有两个探针分别放置在两条数据线上。

Figure 5.8. A block diagram with probes placed on two of the wires

图5.8. 一个在两处数据线上放置了探针的框图程序

You can use the probe with execution highlighting and single-step mode to view values more easily. Each probe and the wire it references are automatically numbered uniquely by LabVIEW to help you keep track of them. A probe's number will not be visible if the name of the object probed is longer than the Probe window itself; if you lose track of which probe goes with which wire, you can pop up on a probe or a wire and select Find Wire or Find Probe, respectively, to highlight the corresponding object.

使用探针高亮执行和单步执行模式,使观察数值变得更加容易。每一个探针和连线独立自动显示,使你更加明了。如果探针的被测对象的名字的长度超出探针窗口本身,那么探针的数值就无法看到了;如果找不到探针窗口位置,可以在相关的数据连线或探针上点击鼠标右键,选择查找探针。相应的对象会高亮显示。

You cannot change data with the probe.

使用探针是不会改变数据的。

 

<<PREV    NEXT>>




 
关于我们 | 诚邀加盟 | 客户服务 | 相关法律 | 网站地图 | 友情链接 | 服务信箱:service@eefocus.com
© 2006 与非门科技信息咨询(北京)有限公司 All Rights Reserved.