C# Walkthrough: Opening and Running a Project
- 已更新2023-02-21
- 閱讀時間為 2 分鐘
The NI VeriStand Execution API is designed to automate the operation of an NI VeriStand application. For example, you can read and write channel data, control running models, configure alarm states and read data from alarms, log real-time channel data to TDMS or text file on the host computer, and access Workspace tools. You can access this assembly from any .NET-compatible programming language or environment, including NI LabVIEW and NI TestStand.
Refer to the Execution API reference help for a comprehensive list of classes in this API, as well as links to reference documentation for their members.
The following example, written in C#, uses the Execution API to deploy a system definition file to a target running the VeriStand Gateway, read values from channels, and then undeploy the system definition file.
static void Main(string[] args)
{
// Create an array of channel names, as defined in the system definition file, whose values you want to read after deployment
String[] names = { "Aliases/Delay_Random Output", "Aliases/Delay_Sinewave Output", "Aliases/SineWave" };
// An array whose elements are the values of each channel
Double[] values; values = new double[names.Length];
// The value of a single channel
Double val;
// The maximum amount of time, in milliseconds, to wait for the connection process to complete before returning an error.
uint timeout = 60000;
// The path to the system definition file and the IP address of the target to which you want to deploy the system definition file
String sineWavePath = "C:\\Users\\Public\\Public Documents\\National Instruments\\NI VeriStand 2011\\Projects\\Example\\Sinewave Delay.nivssdf";
String gatewayIP = "localhost";
// Initializes a new instance of the Factory class, which provides access to the NI VeriStand system and the various interfaces available in the Execution API. Any code you write using this API must include a Factory constructor to access NI VeriStand.
Factory FacRef = new Factory();
// Gets the IWorkspace2 interface, which you can use to manage connections between the VeriStand Gateway and one or more targets, to start and stop data logging operations, and to configure events for error and status notifications.
IWorkspace2 Workspace = FacRef.GetIWorkspace2(gatewayIP);
// Connects the VeriStand Gateway to one or more targets and deploys the system definition file.
ErrChk(Workspace.ConnectToSystem(sineWavePath, true, timeout));
Console.WriteLine("Deployed system.") ;
// Gets the values of multiple scalar channels running on the target.
for (int i = 0; i < 50; i++)
{
ErrChk( Workspace.GetMultipleChannelValues(names, out values));
Console.Write("Iteration" + i + ": " + values.Length);
for (int j = 0; j < values.Length; j++)
{
val = values[j];
Console.Write(val + ", ");
}
Console.WriteLine("");
System.Threading.Thread.Sleep(500);
}
// Disconnects the VeriStand Gateway from all connected targets.
ErrChk(Workspace.DisconnectFromSystem("", true));
}
private static void ErrChk(Error error)
{
if (error.IsError)
throw new Exception(String.Format("{0:X}", error.Code) + ": " + error.Message);
}