Win IoT Core 上的 UWP错误:Pin ' 当前以不兼容的共享模式打开当使用 Dispose

问题描述

我已经实现了一个 UWP 接口。在其中创建了几个页面。在其中一个页面上,我配置了 GPIO 和 SPI。如果多次快速打开此页面,我就会出现问题。

我收到此错误:

The process cannot access the file because it is being used by another process.
Pin ' is currently opened in an incompatible sharing mode. Make sure this pin is not already in use by this application or another application.'

如果我慢慢来,就不会发生这个错误。

在编写代码时,我参考了示例:Windows Iot Core Sample GPIO

我的代码:

public FileManagerPage()
{
    this.InitializeComponent();
    spiConfig();
}

private SpiDevice STM32F103C6T6 = null;
GpioController gpioController = null;
GpioPin gpioPinSTM = null; 
const int GPIO_PIN25 = 25; 

void StopScenario()
{
    if (STM32F103C6T6 != null)
    {
        STM32F103C6T6.Dispose();
        STM32F103C6T6 = null;
    }
    if (gpioPinSTM != null)
    {
        gpioPinSTM.ValueChanged -= Gpio_ValueChanged;
        gpioPinSTM.Dispose();
        gpioPinSTM = null;
    }
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    StopScenario();
}

private void GPIOConfig()
{
    gpioController = GpioController.GetDefault();
    if (gpioController == null)
    {
        return;
    }
    gpioPinSTM = gpioController.OpenPin(GPIO_PIN25); //ERROR: Pin ' is currently opened in an incompatible sharing mode.
    gpioPinSTM.SetDriveMode(GpioPinDriveMode.Input);
    gpioPinSTM.ValueChanged += Gpio_ValueChanged;
}

private async void spiConfig() 
{
    SpiConnectionSettings spiSettings = null;
    SpiController spiController = null;

    String spiDeviceSelector = SpiDevice.GetDeviceSelector();
    IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);
    spiController = await SpiController.GetDefaultAsync();
    if (spiController == null)
    {
        return;
    }
    spiSettings = new SpiConnectionSettings(0);
    spiSettings.ClockFrequency = 5000000;
    spiSettings.DataBitLength = 8; 
    spiSettings.Mode = SpiMode.Mode1; 
    STM32F103C6T6 = await SpiDevice.FromIdAsync(devices[0].Id,spiSettings); 
    GPIOConfig();
}

private void Gpio_ValueChanged(GpioPin sender,GpioPinValueChangedEventArgs args)
{
    if (args.Edge == GpioPinEdge.RisingEdge)
    {
        System.Diagnostics.Debug.WriteLine($"got");
    }
}

我无法理解为什么在使用“protected override void OnNavigatedFrom(NavigationEventArgs e)”方法时会出现这个错误,该方法在页面关闭时启动,它实现了“void StopScenario()”方法,它释放GPIO 和 SPI 资源。

任何想法如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)