Hands-On Tutorial

Part 1

In this exercise, you will call a VI from Mathematica to acquire some LabVIEW data into a Mathematica notebook. You will also apply some basic data processing to this acquired data. For simplicity, details and background information will be kept to a minimum here. For additional examples and more detailed information, refer to the Mathematica Link for LabVIEW printed and online documentation.    
This tutorial, and the ones that follow, assume a basic working understanding of LabVIEW and Mathematica. If you can open and run a VI, and evaluate Mathematica expressions, you should have all the background you need to work through these tutorials. 
Okay, let's get started.

1) Begin by opening a new Mathematica notebook

2) In this blank notebook, type (or copy & paste):

<< LabVIEW`VIClient`

followed by <SHIFT> and <Enter>. This command invokes the VIClient.m package - the Mathematica package that enables VI Server functionality from inside a Mathematica notebook. 
Character Usage Note: If you are new to Mathematica, you may inadvertently use 'single quote' characters when typing the previous command - in which case your command line would look like this:
<< LabVIEW'VIClient'
Notice that the required character is not a single quote, but rather the accent character that shares a key with the ~ (tilde) character on most standard keyboards. (If you are having difficulty finding this character on your keyboard, simply copy the 'correct' version  and paste it into your notebook. Be sure to type <SHIFT> and <Enter> before continuing.)

3) From your computer desktop, start LabVIEW

4) Open MathLink VI Server.vi . This can be accessed from the LabVIEW Tools menu:

Tools -> Mathlink -> Main -> MathLink VI Server...
Open the VI but don't run it yet. There are some additional preparations to complete first.

Background Information:
MathLink VIServer.vi is the general purpose utility that you use to control a LabVIEW application from within a Mathematica notebook. You will generally run this VI on the computer that is connected to your instruments.

To continue,

5) If you are using a Windows PC for this tutorial, ensure that the 'Link protocol' ring control (on the front panel of MathLink VI Server.vi) is set to 'FileMap'.  If you are using a Mac, this parameter should be set to 'PPC'. (For information about using the TCP protocol, refer to the formal documentation.)

In the next 2 steps, you will open a MathLink connection between the MathLink VI Server.vi and your Mathematica notebook. This is accomplished using the 'ConnectToServer' function. 
Important Note: You must establish the connection quickly or your computer may hang. In some instances, you may be forced to restart if this happens. However, if Mathematica is quickly brought to the foreground and the function noted below is executed reasonably quickly, you can avoid this potential difficulty. (In Tutorial #3, you will investigate an example that demonstrate an automated linking process.) 
6) In order to minimize the potential for lock-up, we will do a little preparation in Mathematica before running the VI, (Tip: read and understand steps #6 and #7 before taking any action.)
First, switch to the notebook, then type (or copy & paste) the following function, but this time DON'T press <SHIFT> and <Enter> just yet. (The <SHIFT> and <Enter> will come after the MathLink VIServer.vi has been started.)
Windows users type (or copy & paste):
link = ConnectToServer[ LinkProtocol -> "FileMap"]

For the Mac type (or copy & paste):
link = ConnectToServer[ LinkProtocol -> "PPC"]

7) Without pressing <SHIFT> and <Enter>, switch to LabVIEW and run  MathLink VIServer.vi. Quickly switch back to the Mathematica notebook, locate your cursor under the ConnectToServer function from the previous step,  and press <SHIFT> and <Enter>. 

Mathematica should respond with something like this:

Out[2] = LinkObject["5555", 2, 2]

indicating that a connection has been made. If a longer message is produced, this indicates that there was a problem initiating the link. Ensure the 'MathLink VI Server.vi' is running, and try entering the Mathematica commands from steps 2 and  6 again. (If troubles persist, it may help to quit both Mathematica and LabVIEW and begin again. )

8) After a connection between Mathematica and LabVIEW has been opened, we can now run any VI via the Link. First, we must declare a VI to call. For this tutorial, we will use the DataSim.vi:
 Open DataSim.vi. This is easily accomplished by clicking the LabVIEW menu item:

Tools -> MathLink -> Tutorial -> DataSim...

9) Run this VI once to fetch it's disk location. After running the VI, select and copy the contents of the 'path' indicator on the VI's front panel into your system clipboard (using menu 'Edit -> Copy' or the keyboard equivalent for your platform) . Using this path information, you will build an instrument (or VI) declaration. Type the following instrument declaration, but replace [*DataSim path goes here*] by pasting in the path data copied to your system clipboard.   

inst = DeclareInstrument [
      "[*DataSim.vi path goes here*]", {{"Function", U16,1}}, {{"Data", DBLMatrix}}]

To clarify:
 If you are working on the Mac, and the path to DataSim.vi is:

Macintosh HD:Applications (MacOS9):LabVIEW:user.lib:MathLink:Tutorial.llb:DataSim.vi

Your instrument declaration would look something like this:
inst = DeclareInstrument ["Macintosh HD:Applications (MacOS9):LabVIEW:user.lib:MathLink:Tutorial.llb:DataSim.vi", {{"Function", U16,1}}, {{"Data", DBLMatrix}}]
If you are working on the Mac, simply paste in the path, and you are ready to proceed. 
On the other hand, if you are working on a Windows platform, there is one additional consideration that you must address- and this point will be particularly important if you are new to Mathematica: specifically, the issue of path separators.
Under Windows, the path separator is the backslash character, "\". This is a potential source of confusion, since "\" is also a control character used by Mathematica to format strings. (For instance, "\t" is a tab and "\r" is a carriage return.) The string "\\" represents a single \ because the first \ is the Mathematica control character that specifies the interpretation of the second one. This should be familiar to veteran Mathematica for Windows users, but might be puzzling to new users and others accustomed to running Mathematica on other operating systems. 
To demonstrate, if the path to DataSim.vi on your PC was: 
C:\LabVIEW\user.lib\MathLink\Tutorial.llb\DataSim.vi
Your VI declaration for DataSim.vi would look like this:
inst = DeclareInstrument["C:\\LabVIEW\\user.lib\\MathLink\\Tutorial.llb\\DataSim.vi", {{"Function", U16,1}}, {{"Data", DBLMatrix}}]

Since the purpose of this exercise is to quickly produce some interesting output, we won't discuss the instrument declaration commands in detail here. (You are encouraged to refer to the Mathematica Link for LabVIEW user guide for a more detailed discussion of the DeclareInstrument function.) 

After the correct path information for your PC is entered into the instrument declaration, once again press <SHIFT> and <Enter>. Mathematica will respond by repeating your declaration back to you.

10) Now that you have declared a VI, you can call it from Mathematica - but first you must open a VI reference. To do this, type (or copy & paste):

OpenVIRef[link, inst]

followed by <SHIFT> and <Enter>. 

11) You are now ready to call the VI and do something interesting with the data. Type (or copy & paste):

data  = CallByReference[link, inst];
data2 = Flatten[data, 1];
ListPlot[data2];
fiteq = Fit[data2, {1, x, x^2}, x]

Type this exactly as you see it here, or simply copy and paste this into the target notebook, then press <SHIFT> and <Enter>. 

Assuming everything has worked according to plan, Mathematica should produce the following output: 

"DataSim.vi successfully operated."

2.1132173832114933` + 
        2.9355588514285076` x + 1.0073100778364064` (x^2)

First, there is a text message indicating that DataSim.vi operated successfully. Below this, is a plot of data returned by DataSim.vi. Below this, an equation defining the least-squares quadratic fit of the transferred data. 

Congratulations, you have now successfully called a VI from Mathematica, and used Mathematica's internal functions to process LabVIEW data - in this case, to calculate a least-squares fit of the data. 

12) We will end this tutorial by shutting down the connection between Mathematica and LabVIEW.

Type:

ServerShutdown[link]

then press <SHIFT> and <Enter>. When Mathematica responds with "Server stopped", the connection has been closed. The MathLink VI Server.vi has also been stopped.

This concludes the first part of the tutorial. If you would like to try another example, go on to Hands-on Tutorial - Part 2.