This tutorial will guide you through using Visual Studio Code to develop and debug C# .NET code modules that can be called by TestStand. You'll also learn how to create a C# CLI application that interacts with the TestStand API.
Before you begin, ensure you have the following prerequisites:
- TestStand installed (version 2022 Q4)
- Visual Studio Code installed
- C# Extension installed
- C# Dev Kit installed
Note: The C# Extension provides base support under the MIT License, while the C# Dev Kit enhances VS Code with additional tools for C# coding, testing, and maintenance. The Dev Kit utilizes the Visual Studio Subscription License Model.
- Building a C# Class Library
- Debugging the Code Module Called by TestStand
- Creating a C# CLI Application with the TestStand API
- Troubleshooting
- Conclusion
- Additional Resources
In this section, we'll create a C# class library and configure it for debugging.
- Create a folder named
my_math_lib
. - Open VS Code from that folder by right-clicking and selecting "Open Folder in VS Code."
- Open the Command Palette (Ctrl+Shift+P) and type .NET: Create Project....
- Select Class Library, name it
my_math_lib
, and save it to the default directory. - Confirm the source files are added to the File Explorer.
- Edit
my_math_lib.csproj
to target .NET Framework 4 and properly load symbols when attaching to TestStand.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>library</OutputType>
<TargetFramework>net40</TargetFramework>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
</Project>
- Paste the following code snippet into Program.cs.
namespace MyMathLib
{
public class Class1
{
public double add(double num1, double num2)
{
return num1 + num2;
}
public double multiply(double num1, double num2){
return num1 * num2;
}
}
}
-
Use the Command Palette (Ctrl+Shift+P) and type .NET: Generate Assets for Build and Debug to create necessary files under
.vscode
for building and debugging the library. -
Remove all configurations from
launch.json
and add the following .NET Framework configuration.
//use this configuration to debug code modules in TestStand
{
"name": ".NET Attach",
"type": "clr",
"request": "attach"
}
-
Click on Program.cs to see the main code again.
-
Use the Command Palette and type .NET: Build. If the build is successful, the terminal should display a message like this:
Learn how to debug a code module called by TestStand.
-
Place a breakpoint on line 7 of Program.cs.
-
Open TestStand.
-
In a new Sequence File, add an Action step using the .NET adapter.
-
Point to the built
my_math_lib.dll
inbin\net40\
of your project folder. -
Set the Root Class to
MyMathLib.Class1
. -
Set the .NET Invocation to
Class1().add(Double, Double)
. -
Set values for
Return Value
,num1
, andnum2
. -
Run the sequence using Run MainSequence.
- Observe the debugging process as demonstrated in the animation below:
Discover how to create a C# CLI application that utilizes the TestStand API.
-
Create a folder named
seq_runner
. -
Open VS Code from that folder by right-clicking and selecting "Open Folder in VS Code."
-
Open the Command Palette (Ctrl+Shift+P) and type .NET: Create Project....
-
Choose Console App, name it
seq_runner
, and save it to the default directory. -
Confirm the source files are added to the File Explorer.
- Edit
seq_runner.csproj
to target .NET Framework 4 and properly load TestStand API assemblies, and include a support folder for the target sequence file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="NationalInstruments.TestStand.Interop.API, Version=22.0.0.49152, Culture=neutral, PublicKeyToken=ad9244eb3b825cd8, processorArchitecture=MSIL">
<HintPath>C:\Program Files\National Instruments\TestStand 2022\API\DotNET\Assemblies\CurrentVersion\NationalInstruments.TestStand.Interop.API.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="NationalInstruments.TestStand.Interop.UI, Version=22.0.0.49152, Culture=neutral, PublicKeyToken=ad9244eb3b825cd8, processorArchitecture=MSIL">
<HintPath>C:\Program Files\National Instruments\TestStand 2022\API\DotNET\Assemblies\CurrentVersion\NationalInstruments.TestStand.Interop.UI.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="NationalInstruments.TestStand.Utility, Version=22.0.0.49152, Culture=neutral, processorArchitecture=MSIL">
<HintPath>C:\Program Files\National Instruments\TestStand 2022\Bin\NationalInstruments.TestStand.Utility.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="Sequence File\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
-
Create a Sequence File folder in the workspace root. Save a simple TestStand sequence file (e.g., displaying a pop-up dialog) as
Test.seq
in this folder. -
Paste the code snippet into Program.cs.
using NationalInstruments.TestStand.Interop.API;
namespace seq_runner
{
class SequenceRunner
{
static void Main(string[] args)
{
String targetSeqFilePath = Path.Combine(Environment.CurrentDirectory, "Sequence File\\Test.seq");
Console.WriteLine("Loading File ->" + targetSeqFilePath);
//String processModelName = ""; // Remove the comment if you're going to use a Process Model.
String entryPoint = "MainSequence";
Engine engine = new Engine();
try{
// Find the sequence file absolute path based on the calling sequence file's directory.
Console.WriteLine("Loading Sequence File...");
// Locate and open the sequence file contianing the sequence to be executed.
SequenceFile targetSeqFile = engine.GetSequenceFileEx(targetSeqFilePath);
/* Remove the comment if you're going to use a Process Model.
Console.WriteLine("Loading Process Model...");
// Locate and open the process model to be used for this execution.
SequenceFile processModel = engine.GetStationModelSequenceFile(out processModelName);
*/
SequenceFile processModel = null; // Comment this line if you're using a Process Model.
// Launch a new execution of the sequence file using the specified process model.
// The SequenceNameParameter is the name of the process model entry point
Console.WriteLine("\nExecution Started\n-----------------------\n");
Execution currentExecution = engine.NewExecution(targetSeqFile, entryPoint, processModel, false, 0);
// Wait for the execution to end.
currentExecution.WaitForEndEx(-1);
Console.WriteLine("\n-----------------------\nExecution Finished\n");
// Release the process model opened previously.
engine.ReleaseSequenceFileEx(processModel, 4);
// Release the sequence file opened previously.
engine.ReleaseSequenceFileEx(targetSeqFile, 4);
System.Threading.Thread.Sleep(1000);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ToString());
}
}
}
}
-
Use the Command Palette (Ctrl+Shift+P) and type .NET: Generate Assets for Build and Debug to create necessary files under
.vscode
for building and debugging the application. -
Click Run or Debug... in the upper-right corner and select Run project associated with this file.
-
Check the debug log; a successful run should look like the animation below:
Debug your code by following these steps:
-
Place breakpoints in your code.
-
Click Run or Debug... in the upper-right corner and choose Debug project associated with this file.
Address common issues and challenges that readers may encounter.
-
Issue 1: Description of the issue.
- Solution or workaround.
-
Issue 2: Description of the issue.
- Solution or workaround.
This tutorial demonstrated how to develop and debug C# modules for TestStand using Visual Studio Code. You can now confidently work with C# DLLs and create C# code that interacts with the TestStand API. By targeting the supported .NET Framework version, you can seamlessly integrate C# components into TestStand projects.
Find more information and resources to enhance your C# development with TestStand.
- Getting Started with C# in VS Code
- Programming with the TestStand API in C#
- .NET Adapter - TestStand Help
- TestStand API Reference Help
Feedback: Help us improve this tutorial. Please provide feedback, report issues, or suggest enhancements. 😃
Author: Felipe Flores, Senior Technical Support Engineer at NI.
Last Updated: August 15th, 2023.