Skip to main content
Version: Torizon OS 7.x.y

Application Development - .NET Example

Introduction​

This article describes the main steps for developing .NET applications for Torizon OS devices using the Torizon IDE extension. It presents a walkthrough of the development process. You will learn to go from a project template to a working application. To help you test and understand the basics, we will also guide you in setting up and running a sample that uses the GPIO interface.

This article covers the following topics:

  • Configure your package manager.
  • Create a new project with the IDE Extension.
  • Install the .NET SDK.
  • Details about the sample.
  • Install Microsoft .NET packages.
  • Give containers access to devices.
  • Deploy and debug projects.
  • Next steps.

This article complies with the Typographic Conventions for Torizon Documentation.

Prerequisites​

Configure your Package Manager​

Since both Linux and Microsoft distributions provide .NET packages, the .NET SDK installation mix packages, and you will run into problems. To avoid that, configure your package manager to ignore the .NET packages from the distribution's repository before creating a new project.

info

When using WSL, the .NET SDK is installed on the Linux side. Thus, the previous information remains relevant.

Create a New Project​

Create a new project from the .NET 6 C# Console Application template. The Torizon IDE extension automatically generates the project folder and the essential files.

Install the .NET SDK​

When working with .NET projects, we need to install the .NET SDK.

During the first load of a new project, the notification Do you want to check for dependencies on your system? will appear. Click Yes.

The extension will then trigger the check-deps task, which checks the necessary local packages (on the development host, such as SDKs) to build the project. For more information, see Check Host Machine Dependencies Required by the Project.

tip

If you don't see the notification, you can manually trigger the check-deps task. For more information, see Workspace - Tasks.

Confirm the installation and your sudo password will be requested.

The Sample​

This sample consists of a .NET application that continuously toggles a GPIO pin. The entire project is available on GitHub. For more information about this and other samples provided by Toradex, see Samples.

Hardware Setup​

To test the GPIO pin, you can utilize either a multimeter or an LED to verify the logic level of the connector. When using an LED, ensure you match the voltage and current of both the LED and the GPIO pin. If necessary, consider using a driver circuit to power the LED correctly.

The Verdin Development Board already provides LEDs with driver circuits for quick evaluation:

LED schematic

Thus, we can directly connect the GPIO pin to the carrier board's LED interface, as follows:

Testbed

Note that the GPIO_3 pin of the Verdin iMX8M Plus, accessible via the SODIMM_210 pin on the Verdin Development Board, was used. If you have different hardware and are unsure where to begin, refer to the How to Use GPIO on Torizon OS article.

Source Code​

The code uses the System.Device.Gpio library to interact with GPIO pins, abstracting hardware complexities. With this setup, we can control GPIO pins with high-level code.

To run this sample, copy the source code below and paste it into the Program.cs file of your project.

Program.cs source code

Install Microsoft .NET Packages​

NuGet​

While developing your projects, you may need to add .NET packages using the NuGet package manager. It is a way to reuse community code and streamline your application development.

Our GPIO sample requires the System.Device.Gpio .NET package. To add it, run the following command in the VS Code's terminal:

dotnet add package System.Device.Gpio

The following video shows this package addition.

Package Dependencies​

Sometimes .NET packages require installing C libraries. You can search for package dependencies on the NuGet page or in package documentation. In our case, the LibGpiodDriver documentation shows we need the libgpiod-dev package. Since our sample will run inside a container, we need to add this package to the container image. To do so, modify the torizonPackages.json file as follows:

torizonPackages.json
{
"deps": [
+ "libgpiod2"
],
"devDeps": [
+ "libgpiod-dev"
]
}

Note we added libgpiod2 instead of libgpiod-dev to deps because we do not need static libraries and headers in the runtime container. For detailed information, refer to the Add Dependencies and Tools to Existing Projects article.

Peripheral Access​

Projects that utilize peripherals require additional configuration. You need to specify the devices that the application container will access. On Linux-based OSes like Torizon OS, devices are available in user space through the /dev directory. For more information, see Peripheral Access Overview.

There are two methods to make devices accessible within the container:

The Torizon IDE extension manages the creation and running of containers. Additionally, it automatically creates the docker-compose.yml file in the project directory, so you only need to add the necessary parameters.

As an example, the following docker-compose.yml allows the container to access GPIO banks. The highlighted lines show the required configuration:

Docker Compose file for the GPIO Sample

Note that there are two services:

  • gpiodotnet-debug: Contains configurations of the debug container. Here we added the volumes and device_cgroup_rules properties to allow the container to access all GPIO banks, easing the debugging process.
  • gpiodotnet: Builds the release container image. Here, we only give the container access to the GPIO bank (/dev/gpiochip0) used in our source code. It is a security best practice to provide more granular permissions to release containers. For more information about this service, see Build, Test and Push Applications for Production.

Command-Line Arguments​

Depending on the development phase of your project, whether you are debugging or have already built a container image, the process of sending arguments to applications varies. For detailed information, refer to the Pass Arguments to Containerized Applications article.

Deploy and Debug​

After configuring your project, you can deploy and debug it. Since this is common to all languages supported by the extension, please refer to the Deploy and Debug article.

Next Steps​

Check out the following articles for an walkthrough on other programming languages.

Toradex also provides a lot of other code samples in different languages and different use cases that can be a good starting point for your applications. For more information see, Samples.

With the Torizon IDE Extension, you can also:

  • Create a release container image (optimized image with no debugging configurations and dependencies).
  • Push images to container registries.
  • Push applications to Torizon Cloud.

For more information, see Build, Test and Push Applications for Production.

Troubleshooting​

Configured debug type 'coreclr' is not supported.

CoreCLR is the execution engine in .NET Core. If not installed on VS Code, the following error occurs:

CoreCLR Not Supported

To solve this error, click Install coreclr Extension.

A fatal error occurred. A file not being found, such as fxr, libhostfxr.so, or FrameworkList.xml.

This error is generated by mixed packages and occurs because of conflicts between Microsoft and Ubuntu repositories. To solve this error, configure your package manager to ignore the .NET packages from the distribution's repository.



Send Feedback!