ARM 嵌入式板的 C# .NET Core IoT 错误:未处理的异常。 System.IO.IOException: 设备或资源忙

C# .NET Core IoT Error for ARM embedded board : Unhandled exception. System.IO.IOException: Device or resource busy

我想使用 .NET 核心 IoT 库 以便 运行 C# 代码用于我的 SAMA5D27 SOM1 EK1 ARM嵌入式板.

.NET core IoT github

我已经构建了这个由 project.cs 源文件组成的 .NET 核心项目:

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);
                }
            }
        }
    }
}

这是 .csproj 文件:

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

如您所见,代码用于闪烁位于 PIN 81 上的 Led,对应于 PortC pin 17我的董事会。我构建项目是为了在 arm-linux board.

上使用

首先,为了检查引脚是否正常工作,我使用了 libgpiod 库并使用 gpioset gpiochip0 81=1 打开了 pin81 的 LED,它工作正常。

此外,我已经使用 gpioinfo 命令检查了我的 GPIO,这是所需引脚的结果:

 line  81:       "PC17"       unused   input  active-high

但是当我尝试 运行 C# 代码时,它失败并显示以下输出消息:

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

这是我的开发板设备树:

device_tree

PS:我已经从设备树中删除了使用 PC17 GPIO 的 ISC 节点以释放引脚

ISC_DeviceTree_node

为什么我的代码不能 运行?请帮忙!

好的。从事嵌入式系统工作 45 年。最初我认为错误来自连接到电路板的 PC。我现在意识到该设备是控制台串口。

设备正在从 eprom 启动并首先配置控制台以便进行调试。接下来,设备尝试从磁盘安装操作系统。您的设备具有 USB 连接上的智能卡。我正在阅读您的源代码并认为 USB 是 RS-232,但从设备文献来看,USB 用于智能卡。该错误看起来可能来自智能卡的启动代码。所以要么卡没有插入,没有格式化,要么卡上没有操作系统。

根据您的要求,您可能不需要智能卡。我在工作中使用 Xilinx 嵌入式卡而不使用智能卡,因此我们必须关闭启动选项才能使用智能卡。我们还必须在使用前对 Xilinx 进行编程。我假设 Atmel 有类似的选择。

我终于找到了解决办法。您必须在 Linux 平台和 C# 代码中使用 LibGpioD 驱动程序而不是 SysFs 接口。