微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 测试测量 > 虚拟仪器 > Stop multiple While Loops - An example

Stop multiple While Loops - An example

时间:12-13 整理:3721RD 点击:
近来忙碌,看到大家讨论的热闹,不禁技痒,写了两个小程序。LV 8.2.1写的。
前面有人问While loop的问题。学习LabVIEW编程,最先遇到的一个名词大概是data flow(是不是叫“数据流”?)。data flow的一个优点就是多线程,所以我们看其他人写的稍微大一点的程序,很多都是有好几个并排的while loops. 当然这样的结构并不一定是最合适的。如果一个VI内有多个while loops,最好是分成几个VI来做。深究起来while loop里面有很多有意思的话题。很常见的就是如何中止这些循环。
最简单的办法当然是用一个Boolean,当一个循环中止的时候,令这个Boolean置位,然后其他循环随即中止。请看附件stop_multi_loops_Boolean.vi。这个方法的缺点是要创建很多局部变量。另一个办法是用队列。当一个循环中止,向队列里写一个flag,其他循环看到这个flag之后也随即停止。我个人倾向使用後一种方法,原因是循环是用队列的data flow,也就是用generic LV functions 来控制的,而不是用局部变量。这样结构更清楚,debug的时候也方便一些。
这个问题也许太小儿科了,我只是希望对初学者能有些帮助。

 stop_multi_loops_Boolean.vi

 stop_multi_loops_queue.vi

赞~

我的LV是8.0,看不了@_@

我觉得用局部变量不如用全局变量
局部变量对应一个control,只是访问其值感觉比较奇怪,局部变量不对应control就无所谓了。而且全局变量和queue都能用在多vi通信上,就是控制分布在几个vi内的while loop协同工作

那在while loop中能不能设定暂停呢

可以这样:
循环正常运行,里面加个“暂停”按钮值判断:如果按下暂停则while循环直接返回不执行任何代码,否则执行目标代码

全局变量和局部变量的效率有多少差别?

把昨天写的例子稍微扩展了一下,大家可以看到为什么我偏爱使用队列。这个例子还教给大家怎么动态调用。
如果你动态调用其他VI,可以使用同一个队列中止所有这些平行的VI。当然使用全局变量也可以实现这样的功能,但还是那句老话,尽量避免使用全局变量。而且使用全局变量增加了编程的工作量。也就是说你必须额外创建一个全局变量的VI,然后需要控制的while loops里面调用这个全局变量,then you have multiple copies of the global references。而使用queue,则程序是self-contained。
请原谅我夹杂英语的帖子。这些年来其实英语并没有提高,反而中文是大大地退步了。惭愧!

 stop_WhileLoops_extended.rar

说实话。。。。看着真费劲。。。。

说实话还是全局变量好学易懂

俺小老板用全局变量和文件夹管理来实现类似面向对象的特性,软件结构还是很不错的说

我觉得第二种不好,维护性不好。
.55

看了一下队列的,没大看懂
第二种方式我就在用,感觉也还可以啊
效率和可维护性是需要相互平衡的,一味追求效率,牺牲了可维护性,将来也会是噩梦的

“但这种方式没见别人用过”,If you have even not see such a structure...
OK, let's go back to the very beginning when you start LV. In the "Get Started" window, you can "New...", right? Then in "Create new" you can see "design patterns" under "from template". Do you see? Both the structures you are talking about are there.
The difference between State Machine and Producer/Consumer, simply speaking, is the former is more suitable for simple user interface, and the latter can handle relatively complicated user interface.
The producer loop is to, but only to, respond the user actions. That means, when the loops detects a user action, it only generates an order (in fact by adding an element to the queue), without really doing anything responding to the user action. On the other hand, the consumer loop gets the order (the queue element), then it runs the corresponding case. Suppose the user opens a data file. When the producer loop detects the operation, it adds "open file" to the queue, and goes back to wait for the next user operation. As soon as the consumer loop gets the "open file" element, this loop will executes the "open file" case, if there is such a case. However, if this is a really huge file, it may take a long time for the consumer loop to finish this case. If the user has another operation, say, calculate the FFT, then the producer loop,  of course, will catch the operation without any problem, and then add "calculate FFT" to the queue. At this moment, the consumer loop is still executing "open file". So "calculate FFT" case will NOT be executed until the last one is finished. To conclude this, the producer/consumer structure will not miss any response to the user operation, although the consumer loop may response a little bit later.
On the other hand, the State Machine structure only has one loop. All the detections to the user operations are done in the default case. Therefore, if the same problem happens, when the true response case is busy, then the user operation will be ignored.
Question. If we just simply modify the State Machine structure, we still can prevent such problem from happening. Then, how to do such a modification?    

上一篇:看看Intel用lv
下一篇:入门问题!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top