national instruments 美商國家儀器 股 台灣分公司 labview...

28
National Instruments 美商國家儀器()台灣分公司 LabVIEW 實機操作課程 實作習題講解

Upload: duongdat

Post on 03-Oct-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

National Instruments 美商國家儀器(股)台灣分公司

LabVIEW

實機操作課程實作習題講解

Page 2: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

LabVIEW 實機操作課程習題

目錄

LabVIEW 實機操作課程習題

Exercise 1、建立人機介面

Exercise 2、建立程式區

Exercise 3、儲存檔案

Exercise 4、建立網頁

Exercise 5、建立執行與安裝檔

LabVIEW 小技巧介紹

Page 3: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

Exercise 1 建立人機介面

目標

練習建立一個簡單的人機介面

設計

圖一 人機介面

運用

1. 開啟空白 VI

2. 將 VI 以 Basic Front Panel.vi 為名,儲存於 C:\LabVIEW Hands-on 目錄中

3. 建立人機介面

從Controls>>Modern>>Numeric>>Knob 拉出 Knob 置於人機介

Page 4: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

從Controls>>Modern>>Boolean>>Round LED拉出Boolean置於

人機介面

從Controls>>Modern>>Graph>>Waveform Chart 拉出

Waveform Graph 置於人機介面

從Controls>>Modern>>Numeric>>Vertical Pointer Slide 拉出

Slide 置於人機介面

從Controls>>Modern>>Boolean>>Stop Button拉出Stop按鍵置

於人機介面

4. 設定物件

點選 Knob 的物件並按滑鼠右鍵,選擇 Properties,按照圖二在 Scale

內把Minimum設為 1,Maximum設為 100

點選 Slide 的物件並按滑鼠右鍵,選擇 Properties,按照圖二在 Scale

內把Minimum設為 1,Maximum設為 100

把物件拉到合適的大小

Page 5: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

圖二 物件設定

5. 將人機介面上的物件排成如圖一所示

Page 6: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

Exercise 2 建立程式區

目標

利用 Exercise 1 建立的人機介面建立程式區

設計

圖三 程式區

運用

1. 開啟 Exercise 1 做的 Basic Front Panel.vi

2. 到 Files>>Save As... 選 Substitute copy for original 並把新程式儲存為

Basic Block Diagram.vi 於 C:\LabVIEW Hands-on 目錄

3. 建立模擬訊號小幫手

從程式區裡的 Functions>>Express>>Input>>DAQ Assistant 把模

擬訊號小幫手拉出來放在程式區裡

Page 7: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

按照圖四設定視窗

Frequency 設 10.1

Amplitude 設 1

Samples Per Second 設 1000

圖四 模擬訊號小幫手設定

4. 建立比較小幫手

從 程 式 區 裡 的 Functions>>Express>>Arithmetic &

Comparison>>Comparison>>Comparison 將比較小幫手拉出來放

在程式區裡

按照圖五設定視窗

Compare Condition 選 >Greater

Comparison Inputs 選 Second signal input

Page 8: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

Result 選 One result per channel

圖五 比較小幫手設定

5. 建立程式區.

從程式區的 Functions>>Programming>> Structures>>While

Loop 拉出While 迴圈,並將元件全部包住

按照圖三把函數用接線連起來

在Operand 2 框框的左側點滑鼠右鍵並選Create>>Constant,並在

定數裡輸入 50

從程式區的 Functions>> Programming>>Timing>>Wait Until

Next ms Multiple 拉出Wait Until Next ms Multiple 函數,並放置在

迴圈內

在Wait Until Next ms Multiple 函數左側點滑鼠右鍵並選

Create>>Constant,並在定數裡輸入 100

Page 9: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

測試

1. 執行此 VI

2. 在人機介面上轉動 Knob 並可調整波形的頻率

3. 在人機介面上調整 Slide 並可調整波形的振幅

4. 當 Slide 超過 50 時 Boolean 燈號就會亮起

5. 可繼續研究此程式

Page 10: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

Exercise 3 儲存檔案

目標

把 Exercise 1 及 Exercise2 產生出來的資料儲存為檔案

設計

圖六 程式區

運用

1. 於 C:\LabVIEW Hands-on 目錄中開啟 Basic Front Panel.vi

2. 到 Files>>Save As... 選 Substitute copy for original 並把新程式儲存為

Save to file.vi 於 C:\LabVIEW Hands-on 目錄中

3. Write to Measurement File 小幫手

從程式區裡的 Functions>>Programming>>File I/O>>Write to

Measurement File 把Write to Measurement File 小幫手拉出來放在

Page 11: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

程式區裡

按照圖七設定視窗

File Name 設為 C:\test.lvm

If a file already exists 設為 Append to file

其餘設定為預設值

圖七 Write to Measurement File 設定

4. 依照圖六把波形的訊號拉到Write to Measurement File 的輸入

測試

1. 執行此 VI

2. 依照 Exercise 1 控制人機介面

3. 開啟 C:\test.lvm 檢視檔案

Page 12: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

Exercise 4 建立網頁

目標

使用 LabVIEW的Web Publishing Tool 把 Exercise 2 做的程式放到網頁上

設計

圖八 網頁程式

運用

1. 開啟 Exercise 2 所做的 Basic Block Diagram.vi

2. 到 Tools>>Web Publishing Tool…

依照圖九做設定

VI name 選擇 Basic Block Diagram.vi

勾選“Request control when connection is established”

點選“Start Web Server”按鍵

Page 13: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

按下Next>按鍵

圖九 網頁設定#1

依照圖十做設定

在 Document title,Header 及 Footer 內鍵入您想要的文字

按下Next>按鍵

圖十 網頁設定#2

Page 14: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

依照圖十一做設定

使用預設值

按下 Save to Disk 按鍵

圖十一 網頁設定#3

Document URL 小視窗出現後按下 Connect 按鍵

測試

按下Connect 按鍵後,Explorer 視窗就會開啟。您可以在 Explorer 上執行程

式。

當 Explorer 在執行程式時,LabVIEW上的程式就無法執行。

Page 15: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

Exercise 5 建立執行與安裝檔

目標

把 Exercise 2 做的程式製作成執行檔與安裝檔.

設計

修改程式並建立 Project 檔

建立執行檔

建立安裝檔

運用

修改程式並建立 Project 檔

1. 開啟 Exercise 2 所做的 Basic Block Diagram.vi

2. 到 Files>>Save As... 選 Substitute copy for original 並把新程式儲存為

Application.vi 於 C:\LabVIEW Hands-on 目錄

3. 修改程式 – 加入Quit LabVIEW函數確保執行檔結束時 LabVIEW會關閉

Page 16: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

圖十二 修改程式

拉出程式區內 Functions>>Programming>>Structures>> Flat

Sequence Structure 放在While loop 的左側

放置Quit LabVIEW於 Sequence Structure 內。Quit LabVIEW可在

程式區裡 Functions>> Programming>>Application Control 內找

依照圖十二把 Stop 按鍵的輸出拉到 Sequence Structure 上面

修改完畢請儲存程式

4. 於 File>> New開啟新的 Project 檔

Page 17: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

圖十三 開啟 Project

依照圖十三在 Project 下選擇 Empty Project 並按OK鍵

依照圖十四按下Add 鍵,將 Application.vi 加入 Project 中

圖十四 加入程式

Project 開啟後 Application.vi 就會在 Project 視窗出現。選擇

File>>Save 把 Project 儲存為 Build Application.lvproj 於

C:\LabVIEW Hands-on 目錄

Page 18: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

圖十五 Project 視窗

建立執行檔

1. 於 Project 視窗內的 Build Specifications 欄位按下滑鼠右鍵並選擇

New>> Application(EXE)

Page 19: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

圖十六 Applcation(EXE)

2. My Application Properties 視窗會出現。在 Category 欄位內 Information

裡,把 Target filename 改為 exercise.exe。把 Destination directory 改為

C:\builds\Build Application\My Application

圖十七 Information 設定

Page 20: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

3. 在Category欄位內Source File裡的Project Files欄位點選Application.vi

並按下向右的藍色箭頭按鍵。此時Application.vi 會被移到 Startup VIs 欄

位內。移動完後請按 Build 鍵開始製作執行檔

圖十八 Source Files 設定

4. 結束後按Done 鍵

圖十六 製作結束

Page 21: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

測試

您可以在C:\builds\Build Application\My Application 內找到 Exercise.exe 並

試著執行。

建立安裝檔

1. 於 Project 視窗內的 Build Specifications 欄位按下滑鼠右鍵並選擇

New>> Installer

圖十七 Installer

2. My Application Properties 視窗會出現。在 Category 欄位內 Product

Information 裡,把 Product name 改為 Installer Exercise。把 Installer

Page 22: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

destination 改為 C:\builds\Build Application\My Installer

圖十八 Product Information 設定

3. 在 Category 欄位內 Source Files 裡的 Project View 欄位點選My

Application 並按下向右的藍色箭頭按鍵。此時剛剛所製作的執行檔

Exercise.exe 和相關檔案會被移到Destination View 欄位內。移動完後請

按 Build 鍵開始製作安裝檔

Page 23: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

圖十九 Source File 設定

4. 執行完後按下Done 鍵並前往 C:\builds\Build Application\My Installer

檢查安裝檔

圖二十 安裝檔路徑

測試

您可以試著點選 Setup.exe 並安裝剛做好的安裝檔

Page 24: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

LabVIEW 小技巧介紹

Demo 1: 好用的快速鍵

Ctrl + E : 切換人機介面跟程式區

Ctrl + R : 執行程式

Ctrl + B : 清理所有程式區斷線

Ctrl + . : 強制中斷程式

Ctrl + H : 開啟/關閉 Context Help 視窗

Page 25: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

Demo 2: 利用Block Diagram的Cleanup功能整理

接線

請您打開一個您所設計的 vi 或者一個 Blank vi。

按Ctrl+E 切換到 Block Diagram。此時程式的線路可能很複雜,如下圖。

此時您可使用Clean Up Diagram功能整理接線,該功能按鍵位於下方圖

片右上紅框標示處。

Page 26: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

下圖為Queue 的 SubVI 的程式界面,但因接線不當,不容易了解其設計

的方式。

此時可選擇工具列的 Edit>>Clean Up Diagram讓程式自動重新接線,

在整理過後您可以很清楚了解這個Queue SubVI 的程式結構。

Page 27: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

接著看到Messy Code 的範例,該 VI 的名稱為Generated Code.vi,下

圖為其 block diagram。

您也可以使用Control+U 的 Clean Up Diagram熱鍵來整理程式接線。

Page 28: National Instruments 美商國家儀器 股 台灣分公司 LabVIEW …download.ni.com/pub/branches/taiwan/LabVIEW_execise.pdf · LabVIEW 實機操作課程習題 目錄 LabVIEW 實機操作課程習題

您也可以自行定義Cleanup 功能,該選擇 Tools>>Options 然後選擇

Block Diagram: Cleanup 選項,您可以在該頁面進行Cleanup 功能的調

整。