From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Running VI from C# with Shared Variable

Hi ,
I am trying to run a VI from my C# application.This VI has some sharedvariable's.
But every time I launch this VI from C# sharedvariables get red mark insted of green mark.
 
This is how I am doing it from C#
 
private void runVI(viName)
{

LabVIEW.Application lv;

LabVIEW.VirtualInstrument vi;

lv = new LabVIEW.ApplicationClass();

//Set the path to the VI

string vipath=@"C:\Demo\Monitor.VI";

vi= lv.GetVIReference(vipath,

"",true);

string

[] str1 = new String[2];

str1[

0]="IntegerValue";

str1[

1]="Read StringValue";

//The wrapper function expects to be passed a object type by reference.

//We pass the string array to the object type here

object param1 = (object)str1;

//Define the variable that will pass the expression to be evaluated to

//LabVIEW and typecast it to type object

object[] str2 = new object[2];

str2[0] = 0;

str2[

1] = "";

object param2= (object)str2;

vi.Call2(

ref param1,ref param2,true,false,false,true);

lv.Quit();

}

 

I am arraching project and Vi's run picture.

Can some one help me out?
Thanks
 
Download All
0 Kudos
Message 1 of 3
(3,281 Views)

kkumud,

When you move your cursor over the red arrows, what text appears?

 

Chris C

0 Kudos
Message 2 of 3
(3,249 Views)

Looks like the shared variable links are broken if they are red.  Open the Distributed System Manager and check the names of your variables.  You can also create and delete shared variables in the C# code at run time, like this:

 

// create network variables for LabView communication
            string processName = "COR";
            if (ServerProcess.Exists(processName))
            {
                ServerProcess.Delete(processName);
            }
            ServerProcessInfo procInfo = ServerProcess.Create(processName, true);
            ServerVariableInfo varInfo_K = procInfo.CreateVariable("K", false, 0);
            varInfo_K.VariableType = Type.GetType("System.Double");
            ServerVariableInfo varInfo_R2 = procInfo.CreateVariable("R2", false, 0);
            varInfo_R2.VariableType = Type.GetType("System.Double");
            ServerVariableInfo varInfo_T2 = procInfo.CreateVariable("T2", false, 0);
            varInfo_T2.VariableType = Type.GetType("System.Double");
            ServerVariableInfo varInfo_timeArray = procInfo.CreateVariable("timeArray", false, 0);
            varInfo_timeArray.VariableType = Type.GetType("System.Double[]");
            ServerVariableInfo varInfo_resistanceArray = procInfo.CreateVariable("resistanceArray", false, 0);
            varInfo_resistanceArray.VariableType = Type.GetType("System.Double[]");

 

 Then, when you are ready to read/write the variables, define them as follows:

 

NetworkVariableWriter<double> writer_K = new NetworkVariableWriter<double>("\\\\localhost\\COR\\K");
                NetworkVariableWriter<double[]> writer_time = new NetworkVariableWriter<double[]>("\\\\localhost\\COR\\timeArray");
                NetworkVariableWriter<double[]> writer_resistance = new NetworkVariableWriter<double[]>("\\\\localhost\\COR\\resistanceArray");
                NetworkVariableReader<double> reader_R2 = new NetworkVariableReader<double>("\\\\localhost\\COR\\R2");
                NetworkVariableReader<double> reader_T2 = new NetworkVariableReader<double>("\\\\localhost\\COR\\T2");

 There is a more robust way to get the URL for each variable, but I have had problems with them in the past because the format is a little different than the complier needs.

 

When you are ready to write or read from a variable, just use the WriteData() or ReadData() methods:

 

writer_K.Connect();
writer_K.WriteValue(k);

 Hope this helps you out.

 

-Eric

0 Kudos
Message 3 of 3
(2,676 Views)