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β
- Get a Toradex Hardware with Torizon OS installed.
- Set up the Torizon IDE extension Environment.
- Connect a Torizon OS Target Device.
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.
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.
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:
Thus, we can directly connect the GPIO pin to the carrier board's LED interface, as follows:
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:
{
"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:
- Add flags to the Docker Run command.
- Create a Docker Compose file.
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 thevolumes
anddevice_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:
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.