我正在尝试使用 C# API (.NET Core 8) 与 VeriStand 交互。
根据官方教程,我编写了我的代码:
using NationalInstruments.VeriStand.ClientAPI;
class Program
{
static void Main(string[] args)
{
string gatewayIp = "localhost";
try
{
// Initialize Factory class instance to access NI VeriStand system
Factory factory = new Factory();
Console.WriteLine("Factory created successfully");
IWorkspace2 workspace = factory.GetIWorkspace2(gatewayIp);
Console.WriteLine("Workspace obtained successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error type: {ex.GetType()}");
Console.WriteLine($"Error message: {ex.Message}");
Console.WriteLine($"Error stack trace: {ex.StackTrace}");
}
}
}
根据日志,您可以看到 Factory 创建成功,但随后失败并出现函数错误
GetIWorkspace2
:
Factory created successfully
Error type: System.Reflection.TargetInvocationException
Error message: Exception has been thrown by the target of an invocation.
Error stack trace: at System.RuntimeType.CreateInstanceOfT()
at System.Activator.CreateInstance[T]()
at NationalInstruments.VeriStand.ClientAPI.ClientServerManager`1.GetClientServer(String address)
at NationalInstruments.VeriStand.ClientAPI.ClientServerManagerInstances.GetDataServices(String address)
at NationalInstruments.VeriStand.ClientAPI.WorkspaceImpl..ctor(String address)
at NationalInstruments.VeriStand.ClientAPI.WorkspaceAggregator.Create(String address)
at NationalInstruments.VeriStand.ClientAPI.Factory.GetIWorkspace2(String gateway_ip_address)
at Program.Main(String[] args) in C:\Users\Hongb\Documents\Git\ConsoleApp1\ConsoleApp1\Program.cs:line 18
我可以确认如果我使用 VeriStand UI 将“Engine Demo”部署到
localhost
的网关,效果很好。
任何指南将不胜感激,谢谢!
感谢评论中的@shingo指导!在通过
Console.WriteLine(ex.ToString());
打印完整错误并出现内部异常后,它为我指明了正确的方向。
基于缺少的错误
System.ServiceModel.NetNamedPipeBinding
,我意识到这必须是.NET Framework 4应用程序而不是.NET Core 8应用程序,这里是将“Engine Demo”部署到本地主机的网关的完整工作代码
using NationalInstruments.VeriStand.ClientAPI;
namespace VeriStandController
{
internal class Program
{
public static void Main(string[] args)
{
string gatewayIp = "localhost";
string systemDefinitionPath = @"C:\Users\Public\Documents\National Instruments\NI VeriStand 2024\Examples\Stimulus Profile\Engine Demo\Engine Demo.nivssdf";
Factory factory = new Factory();
IWorkspace2 workspace = factory.GetIWorkspace2(gatewayIp);
// Connect to the system and deploy the system definition file
workspace.ConnectToSystem(systemDefinitionPath, true, 60000); // ms
}
}
}
此代码将部署 VeriSand 的“引擎演示”。