lvb2l3
TRANSCRIPT
-
7/27/2019 Lvb2L3
1/24
LV Basics II 37
Lesson 3Local and Global Variables
You Will Learn:
A. An overview on data managementB. How to use local variablesC. How to use global variablesD. Advice about using local and global
variablesE. How to use DataSocket
-
7/27/2019 Lvb2L3
2/24
LV Basics II 38
Data Management Basics
Dataflow is the most efficient way to transfer data Nodes execute when data is available to all inputs Nodes supply data to all outputs when finished executing Data passes immediately from source to destination
Most applications use wires to transfer data Cannot use wires to transfer data between parallel
tasks
-
7/27/2019 Lvb2L3
3/24
LV Basics II 39
Creating Local Variables
Access panel objects from several locations onthe diagram
Two Ways to create alocal variable:
Right-click on anobjects terminal andselect Create >> LocalVariable
Select a local variable
from the Structurespalette
-
7/27/2019 Lvb2L3
4/24
LV Basics II 40
Creating Local Variables
Right-click on the local variable node andchose Select Item to select the desired object
Owned label becomes variable name Select whether you want to read or write to the
local
-
7/27/2019 Lvb2L3
5/24
LV Basics II 41
Local Variable Example
Consider an application where you need to stoptwo data-independent (or parallel ) While Loops atthe same time
Each While Loop plots a sine wave on a chart
-
7/27/2019 Lvb2L3
6/24
LV Basics II 42
Solutions to Local Variable Example
Method 1(Incorrect)
Method 2(Incorrect)
-
7/27/2019 Lvb2L3
7/24
LV Basics II 43
Solution to Local Variable Example
Method 3 (Correct)
-
7/27/2019 Lvb2L3
8/24
LV Basics II 44
Local Variable Reminders
If a front panel object will have a local variableassociated with it, it must have an owned label
When you write to a local variable, you update
its corresponding front panel object When you read from a local variable, you read
the current value of its corresponding frontpanel object
-
7/27/2019 Lvb2L3
9/24
LV Basics II 45
Exercise 3-1 on Page 3-10
Students build Login VI
*You will use this VI in the final application
Approximate time to complete: 15 min.
-
7/27/2019 Lvb2L3
10/24
LV Basics II 46
Exercise 3-2 on Page 3-12 (Optional)
Students finish building Select and Plot Waveform.vi
Approximate time to complete: 15 min.
-
7/27/2019 Lvb2L3
11/24
LV Basics II 47
Global Variables
Special kind of VI Front panel and front panel objects only No block diagram
Panel objects are storage places to write and read data Used to pass data between VIs that are executing or
between VIs that cannot be connected by a wire Read and write global variables
Write global used to write a value to the global Read global used to read the current value of the global Right-click on the node to toggle between read and write
Write Global Read Global
-
7/27/2019 Lvb2L3
12/24
LV Basics II 48
Passing Data Between VIs
VI Number 1 VI Number 2
-
7/27/2019 Lvb2L3
13/24
LV Basics II 49
Creating Global Variables
Create the controls neededas global variables
Each control must have owned label
Right-click usingthe Operating Tool
-
7/27/2019 Lvb2L3
14/24
LV Basics II 50
Global Variable Reminders
Give each global variable an owned label
Write to (initialize) a global variable beforereading from it
If you do not initialize a global variable, thedefault value for that object is returned
You write to the global variable at a separatelocation from where you read it, usually in adifferent VI
-
7/27/2019 Lvb2L3
15/24
LV Basics II 51
Exercise 3-3 on Page 3-19
Students build Data to Global VIand My Globals VI
Approximate time to complete: 20 min.
-
7/27/2019 Lvb2L3
16/24
LV Basics II 52
Exercise 3-4 on Page 3-21
Students build Display Global Data VI
Approximate time to complete: 15 min.
-
7/27/2019 Lvb2L3
17/24
LV Basics II 53
Important Tips about Locals and Globals Make sure to initialize local and global
variables before reading them You must carefully avoid race conditions
Use locals and globals only when necessary Each Read local or global generates a copy of data Accessing globals and locals can increase memory
requirements and reduce execution speed
LabVIEW Code Sequential Code x = x * 5
x = x + 2 OR
x + 2
x = x * 5
No clear datadependency exists, sothe order of executionis not precisely known
-
7/27/2019 Lvb2L3
18/24
LV Basics II 54
DataSocket
An Internet programming technology basedupon TCP/IP that simplifies data exchangebetween computers and applications
Data is sent over the Internet without the
complexity of low-level TCP programming Data can be sent between VIs on same
computer or over network Data can be sent between LabVIEW and any
other programming language Platform independent
-
7/27/2019 Lvb2L3
19/24
LV Basics II 55
Components of DataSocketDataSocket API
Converts data into streamof bytes for transfer
Converts data back tooriginal form at
destination LabVIEW DataSocket API
is in the Communication>> DataSocket palette
DataSocket Server Stand-alone application
that programs can use tosend data over the internet
Automatically manages
connections to one or more clients
-
7/27/2019 Lvb2L3
20/24
LV Basics II 56
DataSocket Functions
DataSocket Writesends any type of datato a specified URL
DataSocket Read
receives the specifiedtype of data from theURL
Uniform ResourceLocator (URL)addresses are used for DataSocket data dstp://
-
7/27/2019 Lvb2L3
21/24
LV Basics II 57
Automatic Publish and Subscribe Right-click on any panel object and select Data
Operations >> DataSocket Connection to create anautomatic connection
When data is attached to a URL, a small rectangleindicates the DataSocket connection status
-
7/27/2019 Lvb2L3
22/24
LV Basics II 58
Exercise 3-5 on Page 3-34
Students build DS Generate Data andDS Read Data VIs
Approximate time to complete: 20-25 min.
-
7/27/2019 Lvb2L3
23/24
LV Basics II 59
Summary Local variables access front panel objects of the VI in which
they are created When you write or read a local variable, you update or read the
current state of its corresponding panel object Global variables pass data between simultaneously running
VIs Write a value to a global variable before reading from it DO NOT OVERUSE Local and Global Variables!!
DataSocket is an Internet-based method of transferring datathat is platform-independent, programming language-independent, and protocol-independentDataSocket uses URLs to specify the specific data connectionDataSocket consists of two parts the API and the Server Any panel object can connect to the DataSocket Server usingthe Data Operations>>DataSocket Connection window
-
7/27/2019 Lvb2L3
24/24
LV Basics II 60
Additional Exercises on Page 3-40
3-6 Students modify In Range VITime to complete: 15 min.
3-7 Students add handshaking to the Data toGlobal and the Display Global Data VIs
Time to complete: 30 min.
3-8 Students modify Select and Plot Waveform VITime to complete: 20-25 min.