Activity 3-1: Getting Started
练习3-1:开始写一个程序
Okay, you've read enough for now. It's time to get some hands-on experience. Go ahead and launch LabVIEW. You'll step through the creation of a simple LabVIEW VI that generates a random number and plots its value on a waveform chart. You'll learn more in the next chapter about the steps you'll be taking; for now, just get a feel for the environment.
好的,你已经看得够多的了。是时候开始练练手了!运行LabVIEW,你将一步步的创建一个简单的VI:产生一个随机数并将其显示在波形表上。你将在下一章中学到现在你所做的每一步。现在,你只需要体验LabVIEW的开发环境。
If you're using the full version of LabVIEW, just launch it and you'll be ready to start building your first VI.
如果你已经有了全功能版的LabVIEW,运行它,因为你已经跃跃欲试的要开发你的第一个VI程序了。
If you're using the evaluation version of LabVIEW, you can still do these activities, because the evaluation version of LabVIEW has no restrictions on creating and editing VIs. Just be aware that the evaluation version of LabVIEW stops working after 30 days. After that point, you'll need to buy a license key to activate your LabVIEW software.
如果你在使用评估版本的LabVIEW,你仍需要做一些激活,因为评估版本的LabVIEW虽然在创建和编辑VI的时候没有限制,但要知道,在30天后它将不能工作。到那时候,你需要购买一个许可证来激活你的LabVIEW。
If you're not comfortable working through the activities in this chapter without more background information or have trouble getting them to work, read Chapter 4, "LabVIEW Foundations," and then come back and try again.如果你在进行本章的练习的时候,由于基础的缺乏使你感到棘手,或是不能使程序运行,请先阅读第4章《LabVIEW基础》,然后再回来尝试一遍。
1. Launch LabVIEW (if it's been open, you can quit it first).
1、运行LabVIEW(如果已经运行,你可以先关掉它)。
2. At the LabVIEW Getting Started dialog, click Blank VI to create a new blank VI (the Blank VI option is found under the New heading, and you have to click on the "Blank VI" text, not the icon). An "Untitled 1" VI front panel will appear on your screen.
2、在LabVIEW的开始对话框中点击Blank VI来创建一个空白VI。(Blank VI选项可以在New的标题框中找到,你需要点击“Blank VI”文字而不是图标。) 这时屏幕上显示的是"Untitled 1" VI的前面板。
Go to the floating Controls palette and navigate (by clicking) to the Modern>>Graph subpalette, shown in Figure 3.17. If the Controls palette isn't visible, select View>>Controls Palette from the menu to make it visible. Also, make sure the front panel window is active, or you will see the Functions palette instead of the Controls palette.
找到Controls控制模板并按路径Modern>>Graph进入(点击)到子模板,如图3.17。如果找不到面板,在菜单中选择View>>Controls Palette使其显示出来。同样,保证前面板窗口是激活的,或者你会看到Functions功能模板而不是Controls控制模板。
Figure 3.17. Modern>>Graph palette showing the Waveform Chart and various other charts and graphs
图片3.17、Modern>>Graph模板显示波形图和其他各种图片图表

On the Graph subpalette, select Waveform Chart by clicking it with the mouse button. You will notice that, as you run the cursor over the icons in the Controls palette and subpalettes, the selected button or icon's name appears in a tip strip, as shown in Figure 3.17.
在Graph子模板中,鼠标点击Waveform Chart以选择波形图表。你会注意到,当鼠标指针经过控制模板上面的图标时,图标或按钮的名字将显示在小条上,如图3.17。
Positioning Tool布局工具
You will see the outline of a chart with the cursor "holding" it, as shown in Figure 3.18. Position the cursor in a desirable spot on your front panel and click. The chart magically appears exactly where you placed it, as shown in Figure 3.19. If you want to move it, select the Positioning tool from the Tools palette, and then drag the chart to its new home. If the Tools palette isn't visible, select View>>Tools Palette from the menu.
如图3.18,当你用鼠标按住不放时可以看到图表控件的外轮廓。将指针移动到前面板中你希望放置的地方,图表将自动出现在你想放置的地方,如图3.19。如果你想移动它,从工具模板“Tools”中选择布局工具“Positioning”,将图表拖拉到新的地方。如果没有显示工具模板,在菜单中选择View>>Tools Palette则可以将它激活。
Figure 3.18. Waveform Chart after it has been dragged onto the front panel (and before it has been dropped)
图3.18、被拖拉到前面板后的波形图表(在被释放之前)

Figure 3.19. Waveform Chart after it has been dropped onto the front panel
图3.19、被放置在前面板上的波形图表

3. Go back to the Modern subpalette; then navigate into Boolean subpalette, and choose Stop Button (see Figure 3.20).
3、回到模型模板“Modern”;进入到布尔变量子模板“Boolean”,选择停止按钮“Stop Button”(参考图3.20)。
Figure 3.20. Modern>>Boolean palette showing the Stop Button and other Boolean controls and indicators
图3.20、模型>>布尔变量子模板显示停止按钮及其它布尔变量控制器和指示器

Place it next to the chart, as shown in Figure 3.21.
如图3.21,紧挨图表放置。
Figure 3.21. Your VI front panel, after adding a Waveform Chart and Stop Button
图3.21、在放置了波形图表和停止按钮后的前面板

4. Now change the chart's Y-axis scale range from -10 thru +10 to 0 thru 1. Highlight the number "10" (Y-axis range max) by click-dragging or by double-clicking on it with the Text Edit tool. Now type in 1.0 and click on the enter button that appears in the Toolbar at the top of the window. Change -10 (Y-axis range min) to 0 in a similar manner.
4、将图表的Y轴刻度由-10至10更改为0至1.使用编辑工具点拉或双击10(Y轴量程最大)使其处于高亮状态,输入1.0并点击出现在窗口顶部工具栏中的输入按钮。按同样方法将-10(Y轴量程最小)修改为0。
Enter Button输入按钮
5. Switch to the block diagram by selecting Show Block Diagram from the Window menu. You should see two terminals already there, as shown in Figure 3.22.
5、通过从Window菜单中选择Show Block Diagram选择切换到流程图窗口。可以看到上述两个端子已经出现在流程图纸,如图3.22。
Figure 3.22. Your VI block diagram, showing the Stop Button and Waveform Chart terminals
图3.22、你的VI程序流程图,显示停止按钮和波形图的终端

6. Now you will put the terminals inside a While Loop to repeat execution of a segment of your program. Go to the Programming>>Structures subpalette of the floating Functions palette and select the While Loop (see Figure 3.23). Make sure the block diagram window is active, or you will see the Controls palette instead of the Functions palette.
6、现在你可以将这些终端放入While循环程序中去。在功能模板中找到程序>>结构子模板(Programming>>Structures),选择While Loop(参考图3.23)。在保证流程图窗口是激活的情况下,也许看到的是控制模板(Controls)而不是功能模板(Functions)。
Figure 3.23. Programming>>Structures palette showing a While Loop and other LabVIEW structures
图3.23、程序>>结构模板显示While循环和其它的LabVIEW程序结构

Your cursor will change to a little loop icon. Now enclose the DBL and TF terminals: Click and hold down the mouse button while you drag the cursor from the upper-left to the lower-right corners of the objects you wish to enclose (see Figure 3.24).
你的鼠标指针将变成一个小的循环图标,现在装入图表和按钮两个终端:从你希望放置对象的左上角点击不放并拖拉鼠标至目标的右下角(参考图3.24)。
Figure 3.24. Dragging the mouse cursor to place a While Loop on the block diagram around a region of code
图3.24、在流程图中代码区域周围拖拉鼠标指针放置While循环

When you release the mouse button, the dashed line that is drawn as you drag will change into the While Loop border (see Figure 3.25). Make sure to leave some extra room inside the loop.
当你释放鼠标按钮时,你画出的虚线将变成while循环实体的边界(参考图3.25)。在循环中尽量预留空间。
Figure 3.25. A While Loop containing the code that was enclosed by the dragged region
图3.25、一个包含了拉动区域内代码的While循环

7. Go to the Functions palette and select Random Number (0-1) from the Programming>>Numeric subpalette. Place it inside the While Loop.
7、前往功能模板,在程序>>数值“Programming>>Numeric”子模板中找到随机数模块“Random Number (0-1)”。放置到While循环中。
The While Loop is a special LabVIEW structure that repeats the code inside its borders until the conditional terminal reads a TRUE value (when configured to Stop if True, appearing as a small red stop sign ). It is the equivalent of a Do-While Loop in a more conventional language.
While循环是一个特殊的LabVIEW程序结构,它将重复在它内部的代码直至条件端子读到“真”信号。(当设置为“真时停止”时,条件端子显示为小红色停止标志 )。这与大多数编程语言中的Do-While循环结构类似。
You can configure the conditional terminal of the While Loop to Continue if True (appearing as a small green loop ), from its pop-up menu or by clicking on it with the Operating tool. You'll learn more about While Loops in Chapter 6, "Controlling Program Execution with Structures."通过它的弹出菜单或者使用操作工具点击它,你可以将While循环的条件端子配置成为Continue if Ture(显示为绿色循环)。你可以在第六章“程序控制结构”中学到更多关于While循环的知识。
8. With the Positioning tool active, arrange your diagram objects so that they look like the block diagram in Figure 3.26.
8. 使用布局工具,重新排列你的程序代码使他们看起来如图3.26。
Positioning Tool布局工具
Figure 3.26. Your block diagram after placing the Random Number (0-1) function
图3.26、放置随机数函数(0-1)后的程序框图

9. With the Wiring tool active, click once on the Random Number (0-1) icon, drag the mouse over to the Waveform Chart's terminal, and click again (see Figure 3.27).
9、使用连线工具,单击随机数(Random Number (0-1))图标,鼠标拉到波形图(Waveform Chart)的输入端子,再次单击(如图3.27)。
![]()
Wiring Tool
连线工具
Figure 3.27. Your block diagram, as you drag a wire from the Random Number (0-1) function to the Waveform Chart terminal
图3.27、连接随机数与波形图后的程序框图

You should now have a solid orange wire connecting the two icons, as shown in Figure 3.28. If you mess up, you can select the wire or wire fragment with the Positioning tool and then hit the <delete> key to get rid of it. Now wire the Boolean TF terminal to the conditional terminal of the While Loop. The loop will execute while the switch on the front panel is FALSE (not pressed down) and stop when the switch becomes TRUE (pressed down).
如图3.28,你会有一条实心橙色线连接两个图标。如果你弄乱了,你可以使用布局工具选择线条或者线条的某一段然后按下<delete>键来删除它。现在将布尔变量的控制端子连接到循环结构的条件端子。当前面板的控制端子为“FALSE”(没有按下)时循环将持续运行,当开关为“TURE”(按下)时循环将停止。
Figure 3.28. Your block diagram after completing the wiring tasks
图3.28、完成连线后的程序框图

10. You should be about ready to run your VI. First, switch back to the front panel by selecting Show Front Panel from the Window menu. Now click on the run button to run your VI. You will see a series of random numbers plotted continuously across the chart. When you want to stop, click on the stop Boolean button using the Operating tool (see Figure 3.29).
10、你可以准备运行你的VI程序了。首先,切换到前面板(通过选择Window菜单中Show Front Panel的)。点击运行按钮来运行你的VI程序。你可以看到一系列的随机数在持续的横过图表。当你要停止时,使用操作工具点击停止按钮。
Run Button运行按钮
Operating Tool操作工具
Figure 3.29. Your VI front panel after pressing the Run button
图3.29、点击运行按钮后的VI程序前面板

11. Create a directory or folder called MYWORK in a convenient location (such as your home or documents directory). Save your VI in your MYWORK directory or folder by selecting Save from the File menu and pointing out the proper location to save to. Name it Random Number.vi.
11、在一个方便的地方(比如说你的主目录或文档目录)中新建一个”MYWORK”文件夹。通过选择File菜单下的Save将你的VI保存到新建的“MYWORK”文件夹中或指定一个确切的地方,命名为:Random Number.vi。
Save all of your subsequent activities in MYWORK so you can find them easily!将你以后所有的练习都保存到MYWORK文件夹,这样你就可以轻松的找到它们。
Remember if you get stuck or just want to compare your work, the solutions to every activity in this book can be found in the EVERYONE directory or folder on the accompanying CD. You can view them in the sample version or the full version of LabVIEW.记得一点,如果你碰钉子了或者想要对比一下你的工作,这本书所有的练习例子都可以在附赠光盘中的EVERYONE文件夹中找到。你可以在评估版本或者全功能版本的LabVIEW中查看它们。
Congratulate yourselfyou've just written your first LabVIEW program! Don't worry that it doesn't actually DO much; your programs will be more powerful and have more of a purpose soon enough!
祝贺你自己,你已经完成了你的第一个LabVIEW程序!不要担心这些没有任何实际用处,你不久后的程序功能将更强大和更有目的性!






