我想打开蜂鸣器的PWM引脚。但是,如果我尝试调用pwmController.OpenPin(6)方法,则应用程序崩溃并显示System.Runtime.InteropServices.SEHException

我已经仔细检查了ms-iot-samples之类的样本源。但是我看不出我的问题是什么。

一个想法是缺少某些权限,但是如果我尝试添加示例<iot:Capability Name="lowLevelDevices" />,则无法再构建该应用程序。

资源

private PwmPin buzzerPin;
private PwmController pwmController;

public RainbowHAT()
{
    // ... do something else
    InitAsync();
}

private async void InitAsync()
{
    Logger.Log(this, "Init");

    // Setup PWM controller.
    if (LightningProvider.IsLightningEnabled)
    {
        LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
    }

    var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());
    if (pwmControllers == null || pwmControllers.Count < 2)
    {
        throw new OperationCanceledException("Operation canceled due missing GPIO controller");
    }

    pwmController = pwmControllers[1];
    pwmController.SetDesiredFrequency(50);

    // Setup buzzer
    buzzerPin = pwmController.OpenPin(13); <-- CRASH
    buzzerPin.SetActiveDutyCyclePercentage(0.05);
    buzzerPin.Start();
}


我也尝试了以下tip to reduce the min required Windows version,但这也无济于事。

最佳答案

PWM控制器需要闪电支持。因此,您需要将控制器驱动程序设置为Direct Memory Mapped DriverHere是有关Raspberry Pi上PWM的示例。

c# - 如果我尝试打开PWM引脚,Windows 10 Iot Core应用程序将崩溃-LMLPHP

您还需要修改代码,如下所示:

    private async void InitAsync()
    {
        Logger.Log(this, "Init");

        // Setup PWM controller.
        if (LightningProvider.IsLightningEnabled)
        {
            var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());
            if (pwmControllers == null || pwmControllers.Count < 2)
            {
                throw new OperationCanceledException("Operation canceled due missing GPIO controller");
            }

            pwmController = pwmControllers[1];
            pwmController.SetDesiredFrequency(50);

            // Setup buzzer
            buzzerPin = pwmController.OpenPin(13);
            buzzerPin.SetActiveDutyCyclePercentage(0.05);
            buzzerPin.Start();
        }
    }

关于c# - 如果我尝试打开PWM引脚,Windows 10 Iot Core应用程序将崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56378895/

10-13 04:02