Queued Message Handler Documentation.html 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>队列消息处理器</title>
  6. <style type="text/css"> <!--
  7. /***************************************************/
  8. /* MINIMAL STYLES */
  9. /* The following section defines styles that every HTML Help project should need. */
  10. /* Specifies white background color for page and font style for text. */
  11. body {background-color:#FFFFFF; font-family:Verdana, sans-serif; font-size:8pt; color: #000000;}
  12. /* Use P for normal paragraphs. */
  13. p { margin-top:6.00pt; margin-bottom:6.00pt; }
  14. br { font-size:4.00pt; }
  15. /* Use H1 for all topic headings. */
  16. h1 { margin-top:3.00pt; margin-bottom:3.00pt; font-size:150%; font-weight:bold; }
  17. /* Use H2 for second-level headings. */
  18. h2 { margin-top:9.00pt; margin-bottom:3.00pt; font-size:125%; font-weight:bold; }
  19. /* Use H3 for third-level headings. */
  20. h3 { margin-top:6.00pt; margin-bottom:3.00pt; font-size:100%; font-weight:bold; }
  21. /* Use H4 for fourth-level headings. */
  22. h4 { margin-top:6.00pt; margin-bottom:3.00pt; font-size:100%; font-weight:bold; }
  23. /* H5 and H6 have the same definition as H4 because you should not need this level of heading in one topic. If you need to use H5 or H6, consider breaking up your topic into more than one topic. */
  24. h5 { margin-top:6.00pt; margin-bottom:3.00pt; font-size:100%; font-weight:bold; }
  25. h6 { margin-top:6.00pt; margin-bottom:3.00pt; font-size:100%; font-weight:bold; }
  26. /* Deprecated. Use P for normal paragraphs. */
  27. p.Body { }
  28. /* Use the Anchor style class for graphic references on a line by themselves. */
  29. p.Anchor { font-size:125%; }
  30. /* Use the Indent style classes to indent a paragraph. If you need to indent text below a list item, use <br><br> to start the new paragraph within the same set of <li></li> tags. If you need to indent a list within another list, nest the indented list within the first list's set of <ol></ol> or <ul></ul> tags. */
  31. p.Indent1 { margin-left:12.00pt; margin-bottom:3.00pt; }
  32. p.Indent2 { margin-left:24.00pt; margin-bottom:3.00pt; }
  33. p.Indent3 { margin-left:36.00pt; margin-bottom:3.00pt; }
  34. p.Indent4 { margin-left:48.00pt; margin-bottom:3.00pt; }
  35. p.Indent5 { margin-left:60.00pt; margin-bottom:3.00pt; }
  36. /* Use the LI style for all list items. */
  37. li { margin-top:3.00pt; margin-bottom:3.00pt; }
  38. /* Use the OL style for numbered lists. You do not have to type the number for each list item in a numbered list. */
  39. ol { margin-left:22.00pt; margin-top:3.00pt; margin-bottom:3.00pt; text-indent:0pt; list-style-type: decimal; }
  40. /* Use the OL style for numbered lists. Nested lists will use the bullet types according to the nesting scheme below */
  41. ol ol {list-style-type:lower-alpha}
  42. ol ol ol {list-style-type:decimal}
  43. ol ol ol ol {list-style-type:lower-alpha}
  44. ol ol ol ol ol {list-style-type:decimal}
  45. ol ol ol ol ol ol {list-style-type:lower-alpha}
  46. ol ol ol ol ol ol ol {list-style-type:decimal}
  47. ol ol ol ol ol ol ol ol {list-style-type:lower-alpha}
  48. /* Use the EquationNum style class for numbered lists of equations. You do not have to type the number for each list item in a numbered list. */
  49. ol.EquationNum { margin-left:36.00pt; text-indent:0pt; list-style-type: decimal; }
  50. /* Use the List-abc style class for lettered lists. You do not have to type the letter for each list item in a lettered list. */
  51. ol.List-abc { margin-left:22.00pt; text-indent:0pt; list-style-type:lower-alpha; }
  52. /* Use the UL style for bulleted lists. You do not have to type the bullet for each list item in a bulleted list. */
  53. ul { margin-left:22.00pt; text-indent:0pt; margin-top:3.00pt; margin-bottom:3.00pt; list-style-type: disc; }
  54. /* Use the UL style for bulleted lists. Nested lists will use the bullet types according to the nesting scheme below. */
  55. ul ul {list-style-image: none;list-style-type: circle}
  56. ul ul ul {list-style-image:none;list-style-type:disc}
  57. ul ul ul ul {list-style-image: none;list-style-type: circle}
  58. ul ul ul ul ul {list-style-image:none;list-style-type:disc}
  59. ul ul ul ul ul ul {list-style-image: none;list-style-type: circle}
  60. ul ul ul ul ul ul ul {list-style-image:none;list-style-type:disc}
  61. ul ul ul ul ul ul ul ul ul{list-style-image: none;list-style-type: circle}
  62. /* Use the List-Box style class for bulleted lists with boxes instead of bullets. You do not have to type the box for each list item in a box list. */
  63. ul.List-Box {list-style-image: none;list-style-type: square}
  64. table { font-size:8pt; }
  65. /* Use the Borderless style class for tables that do not need borders, such as for 2-column or 3-column lists with no headings. */
  66. table.Borderless { border:none; }
  67. /* Use the Bordered style class for tables that need borders. */
  68. table.Bordered { border-width: 1pt; border-style: solid; border-color: #000000; border-collapse: collapse; }
  69. /* Use the TD style for table cells in Borderless or Borderless-Wide tables. */
  70. td { vertical-align:top; padding:3px; }
  71. /* Use the Bordered style class for table headings and cells in Bordered or Bordered-Wide tables. */
  72. .Bordered { border-width: 1pt; border-style: solid; border-color: #000000; }
  73. /* Use the Icon style class for table cells that contain note, caution, warning, or tip icons, or LabVIEW datatype terminals. */
  74. td.Icon { width:40px; }
  75. /* Use the TH style for table heading cells in Borderless or Borderless-Wide tables. */
  76. th { font-weight:bold; padding:3px; }
  77. /* Use the Left-Align style class for table headings and cells that you want to left align instead of center align. */
  78. .Left-Align { text-align:left; }
  79. /***************************************************/
  80. /* CHARACTER FORMATS */
  81. /* The following section defines character formats that every HTML Help project should need. */
  82. /* CHARACTER FORMATS Updated to conform with the CSE HTML Validator Pro */
  83. /* The following section defines character formats that every HTML Help project should need. */
  84. /* Use the Dark-Red format for warnings or cautions. */
  85. .Dark-Red { color: #800000 }
  86. /* Use the Monospace format for code or syntax examples. */
  87. .Monospace { font-family: "Courier New",monospace; font-size: 100%; }
  88. /* Use the Monospace-Bold format for messages and responses that the computer automatically prints to the screen. */
  89. .Monospace-Bold { font-family: "Courier New",monospace; font-weight: bold; font-size: 100%; }
  90. /* Use the Monospace-Italic format to denote text that is a placeholder for a word or value that the user must supply. */
  91. .Monospace-Italic { font-family: "Courier New",monospace; font-style: italic; font-size: 100%; }
  92. /* small class */
  93. .smallFont { font-size:87.5%; }
  94. /* Bold class */
  95. .Bold { font-weight: bold; }
  96. /* Italic class */
  97. .Italic { font-style: italic; }
  98. /* Use the Platform format to denote a specific platform. */
  99. .Platform { color: #0000FF; font-weight: bold; }
  100. /* Use the Symbol format for characters not in the Verdana character set. Use this format sparingly. When possible, you should use the correct ASCII code for the symbol or use a graphic to recreate the symbol. */
  101. .Symbol { font-family: Symbol; }
  102. /* Use the Red-text format to call attention to text that needs information added or edited by techcomm */
  103. .Red-text {color: #FF0000;}
  104. /* Use the Green-Underline format for the green defintion in the conventions topic */
  105. .Green-Underline {color: #007700; text-decoration : underline;}
  106. /* Use the glossButton format for the glossary buttons used in the glossary topic */
  107. .glossButton { font-size:12px; color:black; }
  108. /* Use for text sections and hyphenated words that should not break at line wraps */
  109. .nobreak{white-space:nowrap}
  110. /* Use for template instructions. */
  111. .instructions{color:red; font-style:italic}
  112. /*Use for words and text sections that do not need to be localized */
  113. .DNL { }
  114. /* The following styles define the color of links. */
  115. a:link { color: #007700 }
  116. a:visited { color: #7F007F }
  117. a:link:hover { color: #FF0000 }
  118. a:link:active { color: #FF0000 }
  119. /***************************************************/
  120. /* Forms */
  121. form { margin-top:0pt; margin-bottom:3pt; color:black; }
  122. select { font-size:100%; color:black; }
  123. /*******************************************************/
  124. /* Margins */
  125. /* Use these styles for block-level elements inside tables cells or list items where you do not want extra padding at the top or bottom of a cell. */
  126. .noTopMargin { margin-top:0pt;}
  127. .noBottomMargin { margin-bottom:0pt;}
  128. /***************************************************/
  129. /* CODE STYLES */
  130. /* The following section defines styles that you need to format entire sections of code or syntax examples. If you have just a few words you need to format as a code or syntax example, use the Monospace character format. */
  131. p.Code { margin-top:3.00pt; margin-bottom:0.00pt; font-family: "Courier New",monospace; }
  132. p.Code1 { margin-left:12.00pt; margin-top:3.00pt; margin-bottom:0.00pt; font-family: "Courier New",monospace; }
  133. p.Code2 { margin-left:24.00pt; margin-top:3.00pt; margin-bottom:0.00pt; font-family: "Courier New",monospace; }
  134. p.Code3 { margin-left:36.00pt; margin-top:3.00pt; margin-bottom:0.00pt; font-family: "Courier New",monospace; }
  135. --> </style>
  136. <script type="text/javascript"> // define hyperlinks and function for launching a URL in a browser
  137. var L_walkthrough = "http://www.ni.com/video/2728/en";
  138. function WWW(url)
  139. {
  140. var urlWindow;
  141. urlWindow = window.open(url, "New", "directories=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes");
  142. }
  143. </script>
  144. </HEAD>
  145. <body>
  146. <h1>队列消息处理器</h1>
  147. <p>&ldquo;队列消息处理器(QMH)&#8221;模板便于并行执行多段代码,以及在段与段之间发送数据。每段代码表示一个任务,如采集数据。其设计模式和状态机相似。这种设计模式可以将每个任务分成多个状态。</p>
  148. <p>QMH模板是生产者/消费者设计模式的一种实现,其中用户界面(生产者)负责生成消息,任务(消费者)负责处理消息。但在QMH模板中,也可从消费者循环生成消息。</p>
  149. <p>该模板包含一个生产者循环和一个消费者循环。用户可根据需求<a href="#add_task">添加消费者循环</a>。</p>
  150. <h2>系统要求</h2>
  151. <p>LabVIEW基础版、完整版或专业版开发系统</p>
  152. <h2>使用条件</h2>
  153. <p>OMH模板用于多个任务并行执行的应用程序,且速率通常各不相同。例如,连续采集、记录并显示两个信号(一个RS-232信号和一个模拟信号)的应用程序。这些信号发生的速率各不相同,因此应用程序必须包含两个并行运行的循环。此外,每个循环分为以下状态:</p><ul><li>初始化数据采集硬件</li>
  154. <li>采集数据</li>
  155. <li>将采集数据记录至磁盘</li>
  156. <li>在波形图表中显示采集到的数据</li>
  157. <li>将硬件设置为安全状态</li>
  158. <li>停止数据采集并关闭硬件</li></ul><p>该应用程序需要一个交互式用户界面,即应用程序执行其他命令时,用户可以单击按钮。因此,应用程序需要第三个并行循环,用于持续监控前面板事件,例如下列命令:</p><ul><li>开始RS-232采集</li>
  159. <li>停止RS-232采集</li>
  160. <li>启用RS-232记录</li>
  161. <li>禁用RS-232记录</li>
  162. <li>开始模拟数据采集</li>
  163. <li>停止模拟数据采集</li>
  164. <li>启用模拟数据记录</li>
  165. <li>禁用模拟数据记录</li></ul><p>QMH模板提供一个编写此类应用程序的起点。</p>
  166. <h2>概述</h2><img src="loc_queued_message_handler.gif"><p>该模板重复执行下列步骤:</p>
  167. <ol>
  168. <li>用户与前面板交互,导致事件处理循环(EHL)中的事件结构生成一个消息。LabVIEW将消息存储在队列中。</li>
  169. <li>消息处理循环(MHL)从消息队列中读取消息,将消息清除。</li>
  170. <li>消息本身是一个字符串,匹配MHL条件结构的一个分支。因此,读取消息将执行条件结构中对应的分支。该分支对应于一个消息,因而被称为<em>消息框图</em>。</li>
  171. <li>(可选)消息框图生成另一个消息,存储在消息队列中。</li>
  172. </ol>
  173. <p>注:</p><ul><li>EHL为生产者循环。MHL为消费者循环。这两个循环并行运行,且由消息队列连接,便于循环之间进行通信。</li>
  174. <li>消息队列是一个LabVIEW队列,存储用于处理的消息。EHL将消息发送至该队列,而非直接发送至MHL。因此,MHL未处理消息时,EHL也可生成消息。每个消息队列属于一个单独的MHL。</li>
  175. <li>MHL的每次循环都将读取消息队列中最早的消息,然后执行相应的消息框图。虽然MHL主要负责处理消息,它也可以生成消息。</li>
  176. <li>每个循环通过特定的错误处理器子VI处理错误。如队列消息处理器在生产者循环中遇到错误,则LabVIEW将不停止队列消息处理器并显示错误消息。如队列消息处理器在消费者循环中遇到错误,则LabVIEW将停止队列消息处理器并显示错误消息。</li>
  177. <li>应用程序可包含多个MHL。每个MHL对应于应用程序执行的一个任务,如采集或记录数据。</li>
  178. <li>注意上面程序框图中的<strong>UI数据</strong>簇。该簇是MHL中每个消息框图可访问和修改的数据。本模板中,该簇被定义为一个自定义类型:UI Data.ctl。每个自定义类型属于一个单独的MHL。</li></ul><h2>运行本模板</h2>
  179. <ol>
  180. <li>在<strong>项目浏览器</strong>窗口,打开并运行Main.vi。</li>
  181. <li>单击前面板输入控件,观察显示控件<strong>显示</strong>中列出的消息。</li>
  182. </ol>
  183. <h2>修改本模板</h2>
  184. <h3>确定需求</h3>
  185. <p>下表总结了修改本模板时必须作出的设计决定:</p>
  186. <table class="Bordered">
  187. <tr class="Bordered">
  188. <th class="Bordered">设计决定</th>
  189. <th class="Bordered">范例</th>
  190. <th class="Bordered">详细信息</th>
  191. </tr>
  192. <tr class="Bordered">
  193. <td class="Bordered">确定待添加的MHL数量。每个MHL定义一个和其他任务并行执行的任务。</td>
  194. <td class="Bordered">有一个采集数据同时记录数据的应用程序。<br><br>该应用程序包含两个任务:数据采集和数据记录。因此,需要使用两个消息处理循环。</td>
  195. <td class="Bordered"><a href="#add_task">创建消息处理循环</a></td>
  196. </tr>
  197. <tr class="Bordered">
  198. <td class="Bordered">对于每个MHL,需确定要添加的消息框图。<br><br>消息框图是MHL中条件结构的分支。每个分支对应于任务可能处于的一个状态。因此,确定待添加的消息框图前,先将每个任务分成多个状态。</td>
  199. <td class="Bordered">希望将数据采集任务分成三个状态:Initialize、Acquire Data和Exit。因此,需要在采集数据的MHL中创建这些消息框图。<br><br>把数据记录任务分为三个状态:初始化、记录和关闭。因此,需要在MHL中创建这些记录数据的消息框图。</td>
  200. <td class="Bordered"><a href="#add_diagram">创建消息框图</a></td>
  201. </tr>
  202. <tr class="Bordered">
  203. <td class="Bordered">确定MHL的消息框图所需的数据。</td>
  204. <td class="Bordered">数据采集MHL的每个消息框图需要访问一个硬件引用。&quot;Initialize&quot;消息框图需打开该参考,&quot;Acquire Data&quot;消息框图使用该参考采集数据,&quot;Exit&quot;消息框图则关闭该参考。</td>
  205. <td class="Bordered"><a href="#change_ui_data">定义消息处理循环所需的数据</a></td></tr>
  206. <tr class="Bordered">
  207. <td class="Bordered">确定每个消息框图的执行时间。消息框图在MHL收到相应的消息后执行。因此,需要决定何时将每个消息发送至MHL。可从前面板控件或消息框图发送消息。</td>
  208. <td class="Bordered">添加一个按钮。按下该按钮时,发送初始化消息至数据采集MHL。<br><br>然后要初始化消息框图发送采集数据消息至同一个MHL。</td>
  209. <td class="Bordered"><a href="#add_control">添加发送消息至消息处理循环的控件</a>或<a href="#send_message">发送消息至消息处理循环</a>,具体依据用户是否希望用控件发送消息。</td></tr>
  210. <tr class="Bordered">
  211. <td class="Bordered">确定是否希望&quot;Exit&quot;消息停止每个MHL。&ldquo;消息出队&#8221;VI使用该消息,原因是它可以关闭MHL。</td>
  212. <td class="Bordered">希望每个MHL收到&quot;Stop&quot;消息(而不是&quot;Exit&quot;消息)时关闭。</td>
  213. <td class="Bordered"><a href="#change_stop_msg">修改停止消息处理循环的消息</a></td></tr>
  214. <tr class="Bordered">
  215. <td class="Bordered">必须确定是否需要忽略EHL或MHL中某些特定的错误。</td>
  216. <td class="Bordered">从消息队列读取消息时,希望忽略网络超时错误。</td>
  217. <td class="Bordered"><a href="#ignore_errors">忽略事件处理循环和消息处理循环中的错误</a></td></tr>
  218. </table><a name="add_task"></a><h3>创建消息处理循环</h3>
  219. <p>消息处理循环(MHL)表示应用程序可与其他任务并行执行的任务,如采集数据或记录数据。每个MHL可被分成多个子任务,对应于各个状态。MHL由下列部分构成:</p><ul><li>一个消息队列</li>
  220. <li>一个从消息队列读取消息的While循环</li>
  221. <li>对于每个可能被读取的消息,还有一个包含分支(也称消息框图)的条件结构,其中,每个消息对应于任务的一个状态</li>
  222. <li>(可选)MHL的每个消息框图可访问的数据</li></ul><p>按照下列步骤,添加一个MHL:</p>
  223. <ol>
  224. <li>本模板包含一个自定义类型,定义了放置所有消息队列引用句柄的簇。默认情况下,该自定义类型只有一个队列。按照下列步骤,为该自定义类型添加一个队列:<ol>
  225. <li>在<strong>项目浏览器</strong>窗口,打开Message Queue.lvlib:Create All Message Queues.vi。</li>
  226. <li>右键单击<strong>所有消息队列</strong>常量并打开自定义类型:<br><br><img src="loc_open_msg_queue_typedef.png"><br><br>LabVIEW会启动<strong>控件编辑器</strong>窗口。</li>
  227. <li>展开<strong>消息队列</strong>簇的边框。</li>
  228. <li>创建簇中<strong>UI</strong>队列引用句柄的一个副本。</li>
  229. <li>重新命名这个新的队列引用句柄。例如: <br><br><img src="loc_new_task_typedef.png"></li>
  230. <li>选择<strong>文件&raquo;应用改动</strong>,关闭<strong>控件编辑器</strong>窗口。<strong>消息队列</strong>自定义类型将包含新添加消息队列。</li>
  231. </ol>
  232. </li>
  233. <li>修改Create All Message Queues.vi,执行下列步骤: <ul><li>获取消息队列引用</li>
  234. <li>将该队列捆绑至<strong>消息队列输出</strong>簇</li>
  235. <li>(可选)向新MHL发送初始消息</li></ul> <br>下面的截图显示了一个完成上述步骤的代码范例:<br><br><img src="loc_create_two_queues.png"></li>
  236. <li>(可选)如MHL需要访问数据,创建按一个表示该数据的自定义类型。</li>
  237. <li>在Main.vi中,创建表示该任务的消息处理循环:<br><br><img src="loc_new_task_loop.png"> <br><br>注:<ol>
  238. <li>注意在步骤2中,<strong>新任务</strong>队列引用句柄已被绑定至<strong>消息队列输出</strong>簇。上面的代码显示了该簇在何处解除捆绑,以及<strong>新任务</strong>队列的连线分支与<strong>新任务</strong>MHL的连线方式。</li>
  239. <li>注意在步骤2中,可选择将初始消息发送至消息队列。步骤2中的代码显示了初始消息<span class="Monospace">Initialize</span>。上面的代码显示了收到该消息时执行的消息框图(Initialize)。</li>
  240. <li>注意在步骤4中,可选择创建一个自定义类型。上面的代码显示了该自定义类型<strong>新任务数据</strong>的连线方法,以便MHL可以使用。</li>
  241. <li>如希望新任务向<strong>UI</strong>队列发送消息,应将UI队列引用句柄的连线分支与<strong>新任务</strong>循环相连。</li>
  242. <li>上面的代码显示了&ldquo;假&#8221;常量与While循环的条件接线端相连。每个MHL中,只有一个消息框图可以使MHL停止执行。这种设计可以避免应用程序意外关闭或不完全关闭,确保下列情况: <ul><li>MHL关闭前,关闭代码仅在右侧运行。</li>
  243. <li>关闭代码总是运行直到彻底完成。</li></ul></li>
  244. <li>为使Main.vi的程序框图更简洁、可读性更强,可将每个MHL封装到一个子VI中。为进一步组织项目,还可将每个子VI、所有支持的VI及其数据自定义类型放入项目库。关于该设计的范例,见<strong>创建项目</strong>对话框中的&ldquo;连续测量和记录&#8221;范例项目。</li>
  245. <li>上图中的MHL无需访问&ldquo;停止事件&#8221;连线。模板中的MHL使用该连线执行&ldquo;触发用户事件-停止&#8221;VI,该VI将关闭事件处理循环。其他MHL无需执行此操作。</li>
  246. </ol>
  247. </li>
  248. <li><a href="#add_diagram">添加消息框图</a>至MHL中的条件结构。为减少出错和非预期行为,确保每个MHL包含下列消息框图:<ol>
  249. <li>负责对任务进行初始化的消息框图;比如,该框图可能连接至硬件设备,或为数据记录打开文件。</li>
  250. <li><a href="#handle_unrecognized">处理无法识别的消息</a>的消息框图。</li>
  251. <li>执行时释放消息队列并停止循环的消息框图。例如:<br><br><img src="loc_stop_task.png"><br><br>默认情况下,触发该消息框图的消息为<span class="Monospace">&quot;Exit&quot;</span>,用户可<a href="#change_stop_msg">修改停止消息</a>。</li>
  252. <li>(可选)如应用程序要求MHL停止执行任务,但保持活动状态(以便重新开始任务),则需要创建一个使用&ldquo;清空队列&#8221;函数的消息框图,删除全部待处理的消息。<br><br>例如,在连续测量应用程序中,可使用<strong>开始测量</strong>和<strong>停止测量</strong>按钮。单击<strong>开始测量</strong>初始化MHL,该循环通过不断向自身发送同样的消息实现继续执行。在该应用程序中,单击<strong>停止测量</strong>应同时停止测量并清空消息队列,但不会停止MHL。如未清除消息队列,单击<strong>停止测量</strong>将不会产生任何影响。消息队列包含更多消息以便继续测量,同时每个消息还会生成同一类型的其他消息。</li>
  253. </ol>
  254. </li>
  255. <li>在EHL中添加代码,发生错误或应用程序停止时命令新的MHL停止执行,即执行步骤5c中创建的消息框图。将该代码添加至事件结构和&ldquo;错误&#8221;分支,如下图所示:<br><br><img src="loc_stop_new_mhl.png"></li>
  256. <li><a href="#send_message">发送消息至MHL</a>。</li>
  257. </ol><a name="add_diagram"></a><h3>创建消息框图</h3>
  258. <p>消息框图是条件结构的一个分支,位于消息处理循环内部,负责处理特定的消息,类似于状态机的一个状态。</p>
  259. <p>消息框图是条件结构的一个分支,用一个字符串标记。当MHL收到和该标签匹配的消息时执行。</p>
  260. <p>按照下列步骤,创建一个消息框图:</p>
  261. <ol>
  262. <li>对于表示任务的MHL,在条件结构中添加一个分支。<table class="Borderless"><tr><td class="Icon"><img src="noloc_tip.png"></td><td><strong>提示</strong>&nbsp;&nbsp; 如在模板中的MHL添加消息框图,可创建&quot;---Copy This Frame---&quot;分支的副本,自动将数据和错误连线包含在新消息框图中。</td></tr></table>
  263. </li>
  264. <li>对于触发该消息框图执行的消息,可在条件选择器标签中输入其文本。例如,下面的消息框图将在LabVIEW从消息队列中读取到<span class="Monospace">&quot;New Message&quot;</span>时执行:<br><br><img src="loc_new_message_diagram.png"></li>
  265. <li>添加收到该消息时需要执行的代码。完成该步骤时,需注意下列事项: <ul><li>如需访问和修改任务数据,可调用&ldquo;按名称解除捆绑&#8221;和&ldquo;按名称捆绑&#8221;函数:<br><br><img src="loc_access_task_data.png"></li>
  266. <li>为确保跟踪到所有错误,可调用&ldquo;合并错误&#8221;函数,将分支中所有节点的错误合并。</li>
  267. <li>如需访问消息数据,可将<strong>消息数据</strong>输入隧道连线至&ldquo;变体至数据转换&#8221;函数:<br><br><img src="loc_convert_variant.png"></li>
  268. <li>如需向消息队列发送消息,可调用&ldquo;消息入队&#8221;VI。</li></ul></li>
  269. <li>如需执行消息框图,可<a href="#send_message">发送消息</a>至MHL。发送的消息必须和步骤2中输入的消息框图标签匹配。</li>
  270. </ol><a name="change_ui_data"></a><h3>定义消息处理循环所需的数据</h3>
  271. <p>模板中的<strong>UI Data.ctl</strong>是一个自定义类型,定义了MHL可访问的数据簇:</p><img src="loc_ui_data.png"><p>按照应用程序的实际需求,可对该自定义类型进行修改。例如,若MHL中多个消息框图需要修改同一个布尔输入控件,可添加一个布尔输入控件至该自定义类型。</p>
  272. <p>存在多个MHL时,应为每个MHL创建一个自定义类型。这种设计确保了一个MHL无法访问另一个MHL的数据,因而可以避免出错。</p><a name="add_control"></a><h3>添加发送消息至消息处理循环的控件</h3>
  273. <ol>
  274. <li>在前面板上添加一个输入控件。</li>
  275. <li>(可选)如需消息框图通过编程修改该控件,可<a href="#bundle_control">捆绑控件引用句柄</a>至MHL的自定义类型。</li>
  276. <li>在事件处理循环中给事件结构添加一个事件分支。</li>
  277. <li>配置事件结构:当新按钮的值发生变化时,触发事件:<br><br><img src="loc_value_change_event.png"></li>
  278. <li>单击<strong>确定</strong>。LabVIEW会在事件结构中创建一个事件分支。</li>
  279. <li>拖曳新控件程序框图接线端至该事件分支的内部,将接线端与事件相关联。</li>
  280. <li>在事件分支中添加代码,<a href="#send_message">发送消息至MHL</a>。</li>
  281. </ol><a name="send_message"></a><h3>发送消息至消息处理循环</h3>
  282. <p>消息本身是字符串,用于命令MHL执行一个消息框图。消息由EHL生成并存储在消息队列中。MHL的每次循环都读取消息队列中最早的消息,然后执行相应的消息框图。</p>
  283. <p>按照下列步骤,发送消息至MHL:</p>
  284. <ol>
  285. <li>确定应用程序将要发送消息的部分,以及将要接收消息的MHL。可以从EHL或消息框图发送消息。</li>
  286. <li>确定MHL收到该消息时执行的消息框图。确保该消息框图存在,且和希望发送的消息名称一致。如消息框图不存在,可<a href="#add_diagram">创建消息框图</a>。</li>
  287. <li>在Main.vi中,访问表示接收方MHL消息队列的连线。将由&ldquo;创建所有消息队列&#8221;VI的<strong>消息队列输出</strong>簇返回的连线解除捆绑,访问该连线。Main.vi已经包含了解除捆绑UI队列引用句柄的代码,如下图所示:<br><br><img src="loc_message_queue_wire.png"> <br><br>展开&ldquo;按名称解除捆绑&#8221;函数,访问所有MHL消息队列的连线。</li>
  288. <li>在应用程序将要发送消息的部分,创建下列代码:<br><br><img src="loc_enqueue_generic_message.png"><br><br>上述代码中,<span class="Monospace">Message</span>是指和步骤2中确定的与消息框图相匹配的文本,&ldquo;消息队列引用句柄&#8221;是指步骤3中确定的连线。<table class="Borderless"><tr><td class="Icon"><img src="noloc_note.png"></td><td><strong>注</strong>&ldquo;消息入队&#8221;VI可通过模板的<strong>项目浏览器</strong>窗口或快速放置功能找到。</td></tr></table>
  289. </li>
  290. <li>(可选)如需指定该消息代替队列中已经存在的其他消息,可将&ldquo;真&#8221;常量连线至&ldquo;消息入队&#8221;VI的<strong>优先消息?</strong>输入接线端:<br><br><img src="loc_enqueue_priority_message.png"><br><br>高优先级的消息通常为紧急停止的情况预留。这些高优先消息将被放置在消息队列的头部,这样放置可以确保这些消息被优先读取。此模板只允许一个消息作为高优先级消息。默认情况下,此模板使用退出消息做为高优先级消息。</li>
  291. <li>(可选)如需在发送消息时附带发送数据,可将需要发送的值连线至&ldquo;消息入队&#8221;VI的<strong>消息数据</strong>输入接线端。该接线端可连接任何类型的数据。例如,下列代码在发送消息时附带发送一个双精度浮点型数值:<br><br><img src="loc_enqueue_message_with_data.png"><br><br>由于该接线端的数据类型为变体,LabVIEW在输入接线端处显示了一个强制转换点。</li>
  292. </ol><a name="change_stop_msg"></a><h3>修改停止消息处理循环的消息</h3>
  293. <p>消息本身是字符串,可以按实际需要创建或修改消息,不需要修改自定义类型。但本模板定义了一个消息:Message Queue.lvlib:Dequeue Message.vi定义了<span class="Monospace">Exit</span>消息。</p><img src="loc_exit_message.png"><p>如希望通过其他消息(而不是<span class="Monospace">Exit</span>消息)关闭MHL,应对该VI中的消息进行修改。</p><a name="handle_unrecognized"></a><h3>处理无法识别的消息</h3>
  294. <p>确保MHL的所有条件结构都包含一个<strong>默认</strong>消息框图。该框图中的代码在MHL读取到没有对应的消息框图时执行。包含一个<strong>默认</strong>消息框图十分重要,因为消息是用户编程时输入的字符串,而非从枚举型中选择的值。</p><a name="ignore_errors"></a><h3>忽略事件处理循环和消息处理循环中的错误</h3>
  295. <p>按照下列步骤,可为EHL和MHL指定其各自的错误处理器子VI可以忽略的错误。</p>
  296. <ol>
  297. <li>在<strong>项目浏览器</strong>窗口中,打开Error Handler - Event Handling Loop.vi或Error Handler - Message Handling Loop.vi(取决于LabVIEW需要忽略哪里的错误),显示程序框图。</li>
  298. <li>定位标签为<strong>忽略错误(静态列表)</strong>的数组常量:<br><br><img src="loc_qmh_ignore_errors.png"></li>
  299. </ol>
  300. <p>在数组中添加错误代码,可忽略EHL或MHL中的错误。每个数组元素中设定一个错误代码。待忽略的错误取决于应用程序。例如,通过网络读取队列时,可能需要忽略超时错误。</p>
  301. <p>如需在运行时动态设定忽略的错误,可将错误处理器子VI的<strong>忽略错误(运行时)</strong> 输入与数组输入控件相连。每个数组元素中设定一个错误代码。</p>
  302. <p>LabVIEW将所有未设定为可忽略的错误将被作为常规错误。如果经常在EHL发生错误,LabVIEW发送错误消息到消息队列。可以定义MHL如何使用错误消息。如果经常在MHL发生错误,LabVIEW发送退出消息到消息队列。确保通过执行退出消息图停止QMH。在MHL中的常规错误可能在QMH中状态未知。</p><a name="bundle_control"></a><h3>启用消息处理循环编程修改控件</h3>
  303. <p>如需启用MHL编程修改某个控件,应创建一个该控件的引用句柄,并将其捆绑至存储该MHL数据的自定义控件。下面将UI Data.ctl作为一个例子。</p>
  304. <ol>
  305. <li>创建控件引用句柄并将其放入MHL的&quot;Initialize&quot;分支中。</li>
  306. <li>打开UI Data.ctl,展开簇以便放置引用句柄。</li>
  307. <li>在&ldquo;Initialize&#8221;分支中,展开&ldquo;按名称捆绑&#8221;函数的新接线端,并将控件引用句柄连线至该接线端:<br><br><img src="loc_bundle_new_button_ref.png"></li>
  308. </ol>
  309. <p>此时,所有可访问UI Data.ctl的消息分支都可以访问该控件的引用句柄。例如,以下代码中显示了&quot;Disable Button&quot;消息框图,该框图通过上述控件引用句柄禁用前面板的按钮并使其变灰:</p><img src="loc_disable_new_button.png"> <a name="rel_info"><h2>相关信息</h2></a><p>关于本模板使用的LabVIEW概念或对象的详细信息,请在LabVIEW中单击<strong>帮助&raquo;LabVIEW帮助</strong>,查看<em>LabVIEW帮助</em>。当光标移动到各个LabVIEW对象上时,<strong>即时帮助</strong>窗口中显示对象的基本信息。选择<strong>帮助&raquo;显示即时帮助</strong>,打开<strong>即时帮助</strong>窗口。</p>
  310. <p>关于修改本模板以用于测量应用程序的范例,见<strong>创建项目</strong>对话框中的&ldquo;连续测量和记录&#8221;范例项目。</p>
  311. <p>关于本模板开发者走查的详细信息见<a href="javascript:WWW(L_walkthrough)">ni.com</a>。</p>
  312. <hr>
  313. <h2>法律信息</h2><!--Copyright--><p><strong>版权</strong></p>
  314. <p>&copy; 2014-2020 National Instruments. 版权所有</p>
  315. <p>根据版权法,未经National Instruments公司事先书面同意,本发行物不得以任何形式(包括电子或机械形式)进行全部或部分复制或传播,包括影印、录制、储存于任何信息检索系统中,以及翻译。</p>
  316. <p>National Instruments公司尊重他方的知识产权,也恳请用户能给予我们同样的尊重。NI软件受版权法及其他知识产权法的保护。在将NI软件用于复制为他方所有的软件或其他资料的任何场合,NI软件仅可用于在符合许可证或其他法律限制的情况下复制上述资料。</p>
  317. <p><strong>最终用户许可协议和第三方法律声明</strong></p>
  318. <p>安装结束后,可在下列位置找到最终用户许可协议(EULA)和第三方法律声明:</p><ul><li>法律声明位于<span class="Monospace">&lt;National Instruments&gt;\_Legal Information</span>和<span class="Monospace">&lt;National Instruments&gt;</span>目录下。</li>
  319. <li>EULA位于<span class="Monospace">&lt;National Instruments&gt;\Shared\MDF\Legal\license</span>目录下。</li>
  320. <li>如需使NI产品生成的安装程序中包含法律相关信息,请查阅<span class="Monospace">&lt;National Instruments&gt;\_Legal Information.txt</span>。</li></ul><p><strong>美国政府的有限权利</strong></p>
  321. <p>如果客户隶属于美国政府的一个机构、部门、或其他单位,则本手册所涵盖的技术信息的使用、拷贝、复制、发布、修改、披露或传递应当受到适用于民间机构的联邦采购条例52.227-14和适用于军用机构的联邦国防采购条例补充规定252.227-7014和252.227-7015中有限权利条款的约束。</p>
  322. <p><strong>IVI Foundation版权声明</strong></p>
  323. <p><span class="DNL">Content from the IVI specifications reproduced with permission from the IVI Foundation.</span></p>
  324. <p><span class="DNL">The IVI Foundation and its member companies make no warranty of any kind with regard to this material, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The IVI Foundation and its member companies shall not be liable for errors contained herein or for incidental or consequential damages in connection with the furnishing, performance, or use of this material.</span></p><!--Trademarks--><p><strong>商标</strong></p>
  325. <p>关于<span class="DNL">NI</span>的商标信息,见<em><span class="DNL">NI Trademarks and Logo Guidelines</span></em>(<a href="http://www.ni.com/r/rdtrad" target="_blank"><span class="DNL">ni.com/trademarks</span></a>)。此处提及的其他产品和公司名称均为其各自公司的商标或商业名称。</p><!--Patents--><p><strong>专利权</strong></p>
  326. <p>关于NI产品和技术的专利权,请查看软件中的<strong>帮助&raquo;专利信息</strong>、光盘中的<span class="Monospace">patents.txt</span>文件或<a href="http://www.ni.com/r/rdlv17" target="_blank">ni.com/patents</a>上的<em>National Instruments Patent Notice</em>。</p>
  327. </body>
  328. </html>