Using the C# library with .NET Core under ARM64

Using the C# library with .NET Core under ARM64

A few weeks ago, we listed the Yoctopuce libraries supporting Linux ARM64, but C# was not part of the list because .NET Core 2.0 doesn't support this architecture. We nevertheless added ARM64 support to our C# library because it is possible to use a preview version on the next version of .NET Core which will support this platform.



Indeed, the next version of .NET Core, (.NET Core v3.0) will support ARM64 and this version is announced for September 2019. We therefore performed all of our tests with the .NET Core 3.0 Preview 7 version.

The C# library with .NET Core 3.0 Preview


To test our library with .NET Core 3.0 Preview, we must start by installing the SDK on an ARM64 Linux machine, in our case a Nvidia Jetson Nano. The process is easy, we must download the SDK from the Microsoft web site and then follow the instructions.

When the SDK is installed, we can create a new project of type "console" with the following command:

yocto@jetson-dev:~/test$ dotnet new console



Then we must add our C# library to the project. The simplest solution is to use our Yoctopuce.YoctoLib NuGet package. The dotnet add package command downloads and adds the most recent version of our library to the project by using NuGet.

yocto@jetson-dev:~/test$ dotnet add package Yoctopuce.YoctoLib



Then we "only have to" write the application code. For this example, we are simply going to display the version of the C# library that we installed and display the serial numbers of the Yoctopuce modules connected to the USB ports.

class Program
{
    static void Main(string[] args)
    {
        YModule m;
        string errmsg = "";


        Console.WriteLine("Yoctopuce C# Library:" + YAPI.GetAPIVersion());

        if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS) {
            Console.WriteLine("RegisterHub error: " + errmsg);
            Environment.Exit(0);
        }

        Console.WriteLine("Yoctopuce devices on USB:");
        m = YModule.FirstModule();
        while (m != null) {
            Console.WriteLine(m.get_productName()
                + " (" + m.get_serialNumber() + ")");
            m = m.nextModule();
        }

        YAPI.FreeAPI();
    }
}



We can then compile and run the executable directly with the dotnet run command:

yocto@jetson-dev:~/test$ dotnet run Yoctopuce C# Library:1.10.36692 (1.10.36692) RegisterHub error: the user has insufficient permissions to access USB devices (ypkt_lin:406)



The call to YAPI.GetAPIVersion() works well but not the call to YAPI.RegisterHub. It's quite normal. Under Linux, by default, only the super user can access the USB ports. There are two possible solutions: using sudo to temporarily grand root privileges, or adding a udev rule to the system, as described in this post.

If you run the command with sudo, everything works:

yocto@jetson-dev:~/test$ sudo dotnet run Yoctopuce C# Library:1.10.36692 (1.10.36692) Yoctopuce devices on USB: Yocto-LatchedRelay (YLTCHRL1-4C6D0)



The C# library with Mono

The Mono framework also support Linux ARM64. We therefore updated our Mono tutorial
as well as the example available on GitHub.

If you already have an application running under Mono, you must update the Yoctopuce library and recompile the application. You must also copy libyapi-aarch64.so in the same directory as the executable, on top of the dynamic library.

The application can then work under Linux ARM64 with Mono.

yocto@jetson-dev:~/ConsoleApp1/bin/Release$ sudo mono ConsoleApp1.exe Device list YLTCHRL1-4C6D0 (Yocto-LatchedRelay)



Conclusion


With this new C# library, we now fully support the ARM64 architecture (also called aarch64), both with Mono and with the future versions of .NET Core.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.