1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| using System;
using System.IO;
using LabVIEW;
namespace CallingLabVIEWFromCSharpUsingActiveX
{
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
_Application labVIEWApp = new ApplicationClass();
if (labVIEWApp == null)
{
Console.WriteLine("CallsVIThroughActiveX : An error occurred getting labVIEWApp");
return;
}
// Run the LabVIEW example "robot.vi"
RunRobotVI(labVIEWApp);
labVIEWApp.Quit();
}
// Run the example VI robot.vi asynchronously. While the VI is running, set control values
// to control the robot arm. Then, stop the VI by setting the "stop" control to true. Running
// a VI is more like pushing the run button on a VI as compared to calling a subVI.
static void RunRobotVI(_Application labVIEWApp)
{
string viPath = Path.Combine(labVIEWApp.ApplicationDirectory, @"examples\picture\robot.llb\robot.vi");
// Assign an object reference to vi
VirtualInstrument vi = labVIEWApp.GetVIReference(viPath, "", false, 0);
if (null == vi)
{
Console.WriteLine("RunRobotVI : An error occurred getting vi");
return;
}
// Setting vi.ShowFPOnCall to true will not open the front panel when using the "Run" method.
// Must explicitly open the front panel and then close it when done.
vi.OpenFrontPanel(true, FPStateEnum.eVisible);
// Use the "Run" method to run the VI. This is like pushing the run button on a VI inside LabVIEW.
// Use the "Call" or "Call2" methods to call a VI more like a subVI or a function call.
vi.Run(true);
// Change joint values on the robot arm
for (int i = 0; i < 100; ++i)
{
double value = (double)i / 10.0;
vi.SetControlValue("Joint A", value);
vi.SetControlValue("Joint B", value);
vi.SetControlValue("Claw Joint", value);
System.Threading.Thread.Sleep(5);
}
// Stop the robot VI by setting the stop button to true
vi.SetControlValue("stop", true);
// Close the front panel.
// Note: Setting vi.CloseFPAfterCall to true will not close the panel when using the "Run" method.
vi.CloseFrontPanel();
// Get some control values from the VI
double jointA = (double)vi.GetControlValue("Joint A");
double jointB = (double)vi.GetControlValue("Joint B");
double clawJoint = (double)vi.GetControlValue("Claw Joint");
Console.WriteLine("Robot.vi - Get Control Values");
Console.WriteLine("JointA = " + jointA + ", JointB = " + jointB + ", ClawJoint = " + clawJoint);
Console.WriteLine("");
} |
Partager