Hands-On Tutorial

Part 2

In this exercise, we will assume that you have already completed the Hands-on Tutorial - Part 1. If you haven't, you may want to do this before continuing.

In this example, we will once again use 'MathLink VI Server.vi', and 'DataSim.vi'. Since you already have some familiarity with steps necessary to call a VI from Mathematica, we will move a little more quickly this time around. (If you are really in a hurry, simply copy and paste each of the commands from this notebook into your work notebook - but remember to type <SHIFT> and  <Enter> after each step.)

1) In LabVIEW, open  MathLink VI Server.vi. (Tools -> Mathlink -> Main -> MathLink VI Server...)

2) Set the 'Link protocol' ring control to the appropriate setting for your platform - 'FileMap' for Windows, or 'PPC' for Mac. (Again, for information about using the TCP protocol, refer to the Link's formal documentation.)

3) Open a new notebook and type:

 << LabVIEW`VIClient`

followed by <SHIFT> and <Enter>.

4) Next type:

for Windows:
link = ConnectToServer[ LinkProtocol -> "FileMap"]

for Mac:
link = ConnectToServer[ LinkProtocol -> "PPC"]
 
don't press <SHIFT> and <Enter> yet....

5)  Switch back to LabVIEW. Run MathLink VI Server.vi, then  quickly switch back to your new Mathematica notebook and press <SHIFT> and <Enter> to engage the ConnectToServer function.

6) Declare DataSim.vi by typing:
\(inst\  = \ 
              DeclareInstrument[
"(..<your LabVIEW path>..)\user.lib\MathLink\Tutorial.llb\DataSim.vi", \ {{"Function", \ U16, \ 2}}, \ {{"Data", \ DBLMatrix}}]\)

Be sure to replace '(..<your LabVIEW path>..)' with the correct path to LabVIEW on your PC, and use the appropriate path separators for your platform  - specifically: "\\" for Windows users, ":" for Mac. (If you want to call a LabVIEW VI on a Unix PC using the TCP protocol, see Mathematica Link for LabVIEW documentation for more information.) 

When the path information is correct, press <SHIFT> and <Enter> to declare DataSim.vi. 

7) Next, open a VI Reference to DataSim.vi (now referenced by the Mathematica variable 'inst'). Type:

OpenVIRef[link, inst]

followed by <SHIFT> and <Enter>.

8) Call DataSim.vi to generate some data, then process it in Mathematica.

First type:
<<  Statistics`DataSmoothing`
followed by <SHIFT> and <Enter>. This opens the DataSmoothing package and provides access to the ExponentialSmoothing function.Wait for a moment for this command to complete executing, then type:
data   =  CallByReference[link,  inst];
data2  =  Flatten[data,  1];
data3  =  ExponentialSmoothing[data2,  0.1];
ListPlot[data2,  PlotJoined  ->  True,  
            PlotLabel  ->  "Raw Data"];
ListPlot[data3,  PlotJoined  ->  True,  
          PlotLabel  ->  "Filtered Data"]
followed by <SHIFT> and <Enter>.

You have just used Mathematica's built-in data smoothing function to clean up a noisey waveform.

9) Close the link using:

ServerShutdown[link]

followed by <SHIFT> and <Enter>.

This concludes The Hands-on Tutorial - Part 2. A complete transcript of the exercise, including the output produced by Mathematica can be found below. 

_________________________________________________________________________________________________________________

Complete Transcript for this exercise:

<<  LabVIEW`VIClient`
link  =  
          ConnectToServer[ LinkProtocol  ->  "PPC"]
LinkObject["5555", 2, 2]
\(inst\  = \ 
            DeclareInstrument["Macintosh HD:Applications (Mac OS 9):LabVIEW:LabVIEW 6:user.lib:MathLink:Tutorial.llb:DataSim.vi", {{"Function", \ U16, \ 2}}, \ {{"Data", \ DBLMatrix}}]\)

Instrument[
        "Macintosh HD:Applications (Mac OS 9):LabVIEW:LabVIEW \
6:user.lib:MathLink:Tutorial.llb:DataSim.vi", 
        "DataSim.vi", {{"Function", U16, 2}}, {{"Data", DBLMatrix}}]
OpenVIRef[link,  inst]
"DataSim.vi loaded"
<<  Statistics`DataSmoothing`

data   =  CallByReference[link,  inst];
data2  =  Flatten[data,  1];
data3  =  ExponentialSmoothing[data2,  0.3,   .8];
ListPlot[data2,  PlotJoined  ->  True,  
            PlotLabel  ->  "Raw Data"];
ListPlot[data3,  PlotJoined  ->  True,  
          PlotLabel  ->  "Filtered Data"]
"DataSim.vi successfully operated."


⁃  Graphics  ⁃
ServerShutdown[link]
"Server stopped"