Section 12 The While Loop + Case Structure Combination While Loop与Case Structure的结合
推荐给好友
打印
加入收藏
更新于2008-05-16 21:23:47

The While Loop + Case Structure Combination

While Loop与Case Structure的结合

Like most tools, you need a combination of them to build something non-trivial. You can't really build anything with just a hammer or just a saw. But, if you combine the capabilities of these two, you can do some amazing things. Throw in a tape measure and you're unstoppable! LabVIEW structures (our tools, in this case) are the same way. We rarely use them independently. We start with a single While Loop, our high-powered circular saw that keeps spinning until the job is done. Then we drop in a Case Structure (or Event Structure, as we will learn in Chapter 13, "Advanced LabVIEW Structures and Functions"), our high-powered pneumatic nail gun, for nailing down all the cases that our software must handle. Analogies aside, the While Loop + Case Structure combination is a very powerful combination, from which you can build upon to create just about any software application that your mind can imagine.

将如同大多数的工具一样, 你需要它们的组合来构建一些不同寻常的东西. 你不能就靠一把锤子或锯子来完成构建任务. 但是, 如果你能结合这两样东西的能力, 你将可以做一些令人惊异的事情来. Throw in a tape measure and you're unstoppable! LabVIEW的结构 (这时就是我们的工具) 也是同样. 我们很少单独地使用它们. 我们从一个单While Loop开始, 它是我们的高性能的圆锯用来保持轮轴的运转直到完成任务. 然后放下Case Structure(或者是Event Structure, 将在第13章学习), 它就是我们的高功率气枪, 用来定止住那些我们的软件必须处理的所有分支. 类推之, While Loop + Case Structure的结合是一种强大的组合, 从你能创建的上述的组合到创建一种你能够想到的任何应用程序.

The Main Loop

主循环

Nearly all applications written in LabVIEW will have at least one main loop that will keep looping until the program terminates, due to a stop button press, error, or other exit condition. Figure 6.74 shows an example of a While Loop, along with a standard wait timer (to avoid hogging the CPU) and a stop button.

几乎所有的LabVIEW编写的应用程序都会至少有一个主循环, 该循环将保持运行直到程序终止, 因为一个停止按钮被按下, 一个错误出现, 或是其它的退出条件发生了. 图6.74示范了一个While Loop的例子, 并随带了一个标准的定时器(避免拱起CPU)和一个停止按钮.

Figure 6.74. Wait (ms) timer used in a While Loop to avoid hogging the CPU

图6.74. Wait (ms)定时器使用在While Loop中, 以避免拱起CPU

To this loop, we add a Case Structure and connect a button called Do work. In the TRUE case of the Case Structure, we place our workfunctions or subVIs that do something useful. Figure 6.75 shows an example of this, which opens a "Hello World" dialog each time the Do work button is pressed. (Note that Do work is configured for Mechanical Action>>Latch When Released, so that it will rebound to FALSE after its terminal is read on the block diagram. This ensures that the dialog only appears once, for every button push.)

在这个循环中, 我们增加一个分支结构并连接一个称之为Do work的按钮. 在该分支结构的真值(TRUE)分支中, 放置工作函数或子VI. 图6.75显示了这个例子, 该例在每次Do work按钮被按下的时候就打开一个”Hello World”的对话框. (注意Do work按钮已经被配置成为Mechanical Action>>Latch When Released, 所以当它的端子在框图中被读取数值后将回弹为FALSE. 这将保证对于每次按钮按下后, 对话框只会出现一次)

Figure 6.75. Case Structure inside a While Loop, used to execute work when the user presses a button

图6.75. 分支结构在While Loop中, 用于当用户按下一个按钮时执行工作

Spend a minute and let this concept really sink in. This While Loop + Case Structure combination is the key to building software applications in LabVIEW.

花一点时间并使这个概念被真正地理解了. While Loop + Case Structure的组合是LabVIEW中创建应用程序的一个关键.

Handling Multiple Work Items in a While Loop

处理While Loop中的多任务

OK, you're ready for the next step. How do you handle multiple buttons? The easy way is to just add more Case Structures (well, it starts out easy, but gets tricky as you add more and more Case Structures), as shown in Figure 6.76.

好了, 你将准备下一步. 你如何处理多个按钮? 简单的方法就是增加更多的Case Structure (的确, 是很简单, 但是当越来越多的Case Structure增加时却会使得事情变得棘手而要慎重对待了), 如图6.76

Figure 6.76. Two "work handling" Case Structures inside a While Loop

图6.76. 两个”工作处理”的分支结构在While Loop的中

Now we know how to handle multiple work items in our While Loop + Case Structure combinationhandling N work items means that we will need N Case Structures. But what happens when we run out of room on our block diagram? It is easy to see that this "just add another Case Structure" pattern does not scale up past a handful of work items (although we have seen some block diagrams where people have tried to see how far this pattern scales and, believe us, it's not pretty).

现在我们知道如何在While Loop + Case Structure组合里处理多任务了, 处理N项任务就意味着我们需要N个分支结构. 但是当我们用光了框图的空间将会怎样呢? 很容易看到的就是这个”增加另外的Case Structure”的图案没有按比例增大到超越一些工作项目(尽管我们曾经看到一些这样的程序框图, 程序员设法使得它看起来有如何的大, 相信我们, 这是不恰当的)

So, how do we solve this scalability problem? The answer is simple. We need to use a single Case Structure with multiple cases: one case for handling each work item. In order to achieve this, we will need to build a Boolean array of button values and search for the index of the element that is TRUE, as shown in Figure 6.77. (You will learn about arrays in Chapter 7.) In simple terms, we will figure out which button number was pressed and wire this button number into the case selector terminal of the Case Structure.

因而, 如何解决这个比例的问题? 答案也简单. 我们就需要使用单个的分支结构但带有多个分支: 一个分支对应于处理一个任务. 为达到这个目的, 我们需要创建按钮的布尔量数组并查找真值(TRUE)元素的索引, 如图6.77.(你将在第7章中学习数组). 在简单的条件下, 我们可以算出是哪一个按钮被按下了并连线该按钮的值到分支选择器端子上

Figure 6.77. One Case Structure with multiple cases handling multiple tasks inside a While Loop

图6.77. 在While Loop中的一个分支结构, 单有多个分支来处理不同的任务


Don't feel the need to understand the array manipulation happening in Figure 6.77. This will make a lot more sense after you learn about arrays in Chapter 7.

现在不需要你理解发生在图6.77中的数组操作, 这些将会在第7章中学习.

In this example, Build Array (found on the Programming>>Array palette) converts two Booleans into a 1D array of Booleans (see Figure 6.78).

在本例中, 创建数组Build Array函数 (Programming>>Array)转换了两个布尔量到一维数组(1D array)里 (见图6.78)

Figure 6.78. Build Array

图6.78. 创建数组

In this example, Search 1D Array (found on the Programming>>Array palette) tells us which button number was pressed (see Figure 6.79):

在本例中, 搜寻一维数组Search 1D Array函数(Programming>>Array模板中)在将告诉你哪一个按钮被按下了.(见图6.79)

1: No button was pressed

无按钮被按下

0: Say something was pressed

表示某个按钮被按下

1: Play a sound was pressed

播放一个声音

Figure 6.79. Search 1D Array

图6.79. 搜寻1维数组

Now, if we ever want to expand our code, we simply add another button, expand the build array node (add another terminal to the bottom) and wire our button to it, and then add another case for handling the button press.

现在, 如果我们想扩展我们的代码, 我们只要增加另外的按钮, 扩展创建数组Build Array的节点(增加另外的端子)并连接按钮上, 然后再添加处理某按钮按下动作的分支.

Incidentally, you can run this example if you like. It's in the EVERYONE\CH06 directory on the CD, called While Loop with Case Structure Example.vi.

你能够运行这个例子. 该例子在CD光盘的EVERYONE\CH06目录下, 称为While Loop with Case Structure Example.vi.



Note that we put an error message dialog in the Default frame of the Case Structure. This will alert us if we should ever add another button, but forget to add a case to handle it.

注意我们在分支结构的默认框架中放置了一个错误标志信息的对话框. 如果我们增加了一个按钮, 但是忘记了添加相关的处理分支的话, 这个错误标志信息就可以给我们警示.

It is a common practice to use an enum datatype, wired to the case selector terminal, instead of a "plain" I32 numeric. The example is shown in Figure 6.80.

这儿是一个使用枚举数据类型的普通实例, 连线到分支的选择器端子上, 以代替”plain” I32类型的数子. 该例显示如图6.80中.

Figure 6.80. "Enumerating" Case Structure cases for handling multiple work items in a While Loop

图6.80. 在While Loop 中的“列举”分支结构的分支用于处理多个工作项目


Note that this is using some advanced functions and techniques. Don't feel the need to understand everything shown here. Just make a mental note and then come back once you are ready to review it again.

注意到这里使用了一些高级的函数与技术. 但现在可以不必完全理解这些. 只要有这些概念的印象并且在将来还会接触到这些内容.

            

When adding more work items to a While Loop + Case Structure combination that uses an enum type (as shown in Figure 6.80), you will need to remember to add additional elements to the enum constant before you can add the additional case to the Case Structure.

当增加了许多的工作项目到While Loop + Case Structure组合中并使用枚举数据类型的时候(如图6.80), 你要记得在给分支结构增加额外的分支之前需要给枚举常数添加额外的元素.

Adding Efficiency: Wait on Front Panel Activity

增加效率: 等待面板活动



Wait For Front Panel Activity function

等待面板活动的函数

Let's take a slight detour away from the Event Structure, for just one moment, to discuss a very simple option for improving the efficiency of a While Loop that polls front panel controls.

让我们先绕开事件结构一会儿, 先来讨论一个简单的选项用来改善While Loop的效率, 查询前面板上控件.

We can use the Wait For Front Panel Activity function (found on the Programming>>Dialog & User Interface palette) to wait until front panel activity is detected, instead of using the Wait Until Next ms Multiple function to poll the user interface at a periodic interval.

我们可以使用 Wait For Front Panel Activity(等待面板活动的函数, 在Programming>>Dialog & User Interface模板中)用来等待运行直到前面板上活动被检测到, 代替使用Wait Until Next ms Multiple函数在一个周期的间隔内查询用户界面.

Figure 6.81 shows how this simple drop-in replacement significantly improves the performance. With three button presses, the While Loop executes only 13 times (this number will vary, depending on how long you hold down the Do some work? button).

图6.81显示了如何这个简单的置换明显地改善了性能. 只有三次的按钮点击败, While Loop仅执行了13次循环(这个数值可能是不一样的, 依赖于你按下Do some work?按钮的长久程度).

Figure 6.81. Wait For Front Panel Activity used in a While Loop to improve efficiency

图6.81. 在While Loop中使用Wait For Front Panel Activity来提高效率


<<PREV    NEXT>>




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