.NET Core IoT Error while running my C# code

This forum is for users of Microchip MPUs and who are interested in using Linux OS.

Moderator: nferre

ghasseenn
Posts: 6
Joined: Wed Jun 10, 2020 4:10 pm

.NET Core IoT Error while running my C# code

Fri Jun 19, 2020 11:04 am

I want to use .NET core IoT library in order to run C# code for my SAMA5D27 SOM1 EK1 embedded board.

https://github.com/dotnet/iot

I have build this .NET core project composed from project.cs source file :

Code: Select all

using System;
using System.Device.Gpio;
using System.Threading;

namespace led_blink
{
    class Program
    {
        static void Main(string[] args)
        {
            var pin = 81;
            var lightTimeInMilliseconds = 1000;
            var dimTimeInMilliseconds = 200;

            Console.WriteLine($"Let's blink an LED!");
            using (GpioController controller = new GpioController())
            {
                controller.OpenPin(pin, PinMode.Output);
                Console.WriteLine($"GPIO pin enabled for use: {pin}");

                Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs eventArgs) =>
                {
                    controller.Dispose();
                };

                while (true)
                {
                    Console.WriteLine($"Light for {lightTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.High);
                    Thread.Sleep(lightTimeInMilliseconds);
                    Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.Low);
                    Thread.Sleep(dimTimeInMilliseconds);
                }
            }
        }
    }
}
As you can see, the code is used for blinking Led which is situated on PIN 81 which corresponds to PortC pin 17 on my board. I build the project in order to use on arm-linux board.

And this is .csproj file :

Code: Select all

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Iot.Device.Bindings" Version="1.0.0" />
    <PackageReference Include="System.Device.Gpio" Version="1.0.0" />
  </ItemGroup>

</Project>
First, to check if the pin is working well, I used libgpiod library and I turned on the led of pin81 using

Code: Select all

gpioset gpiochip0 81=1
and it is working well.

Furthermore, I have checked my GPIOs using

Code: Select all

gpioinfo 
command and this is the result of the desired pin :

Code: Select all

line  81:       "PC17"       unused   input  active-high
But when I try to run the C# code, it fails with this output message :

Code: Select all

Let's blink an LED!
Unhandled exception. System.IO.IOException: Device or resource busy
   at System.IO.FileStream.WriteNative(ReadOnlySpan`1 source)
   at System.IO.FileStream.FlushWriteBuffer()
   at System.IO.FileStream.FlushInternalBuffer()
   at System.IO.FileStream.Flush(Boolean flushToDisk)
   at System.IO.FileStream.Flush()
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Dispose(Boolean disposing)
   at System.IO.TextWriter.Dispose()
   at System.IO.File.WriteAllText(String path, String contents)
   at System.Device.Gpio.Drivers.SysFsDriver.OpenPin(Int32 pinNumber)
   at System.Device.Gpio.GpioController.OpenPin(Int32 pinNumber)
   at System.Device.Gpio.GpioController.OpenPin(Int32 pinNumber, PinMode mode)
   at led_blink.Program.Main(String[] args) in /home/ubuntu/netcore/Program.cs:line 23
Aborted
PS : I have removed ISC node which is using PC17 GPIO from device tree in order to free the pin

Why my code can't run ? any help please !
Last edited by ghasseenn on Wed Jun 24, 2020 5:08 pm, edited 1 time in total.
blue_z
Location: USA
Posts: 2094
Joined: Thu Apr 19, 2007 10:15 pm

Re: .NET Core IoT Error while running my C# code : Device or resource busy

Tue Jun 23, 2020 12:29 am

Execute your program with strace to obtain the exact syscall (with arguments) that is failing.

Regards

Return to “LINUX”

Who is online

Users browsing this forum: No registered users and 3 guests