Click here to Skip to main content
15,791,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to use .NET core IoT library in order to run C# code for my SAMA5D27 SOM1 EK1 ARM embedded board.

GitHub - dotnet/iot: This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.[^]

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

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) =>

                while (true)
                    Console.WriteLine($"Light for {lightTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.High);
                    Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.Low);

And this is .csproj file :

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


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


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.

First, to check if the pin is working well, I used libgpiod library and I turned on the led of pin81 using
gpioset gpiochip0 81=1
and it is working well.

Furthermore, I have checked my GPIOs using
command and this is the result of the desired pin :

line  81:       "PC17"       unused   input  active-high

But when I try to run the C# code, it fails with this output message :

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

This is my board device tree :[^]

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 !

What I have tried:

I tried using libgpiod library to see if the pin is realy busy or not and I found that the led works fine. But with C# code and .net IoT it does not
Gerry Schmitz 19-Jun-20 13:07pm    
Take out the loop. Start simple until there are no errors then do "incremental" coding and testing. Device "busy" can mean you're not "disposing" when you should.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900