【问题标题】:COMException when running a Background UWP on Windows 10 IOT在 Windows 10 IOT 上运行后台 UWP 时出现 COMException
【发布时间】:2019-06-12 19:46:58
【问题描述】:

我正在为 Raspberry Pi 和 Teensy 3.6 Arduino 开发 I2C 解决方案。 Pi 将通过 I2C 将 NTP 发送到 Arduino。我已经研究了几周,并尝试测试下面的代码,并收到了几个错误。

public sealed class StartupTask : IBackgroundTask
{
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        while(true)
        {
            Wait(5000);
            try
            {
                await I2C();
            }
            catch (Exception ex)
            {
                // eats fast inputs
            }
        }
    }

    private async void Wait(int millis)
    {
        await Task.Delay(millis);
    }

    private async Task I2C()
    {
        var settings = new I2cConnectionSettings(1);
        settings.BusSpeed = I2cBusSpeed.FastMode;
        var controller = await I2cController.GetDefaultAsync();

        using (I2cDevice device = controller.GetDevice(settings))
        {
            byte[] writeBuf = { 0x01, 0x02, 0x03, 0x04 };
            device.Write(writeBuf);
        }
    }
}

使用调试控制台,我发现 I2C 方法永远不会一直通过 using 语句。

例外:

抛出异常:i2cTestIOT.winmd 中的“System.Runtime.InteropServices.COMException” WinRT 信息:传输了意外的字节数。预期的: '。实际的: '。 抛出异常:System.Private.CoreLib.ni.dll 中的“System.Runtime.InteropServices.COMException” WinRT 信息:传输了意外的字节数。预期的: '。实际:'。

【问题讨论】:

    标签: c# windows raspberry-pi iot i2c


    【解决方案1】:

    我无法使用与您的代码完全相同的 Raspberry pi 和 Arduino Uno 重现此错误。(如果它是完整的可重现代码)。所以

    使用调试控制台,我发现 I2C 方法永远不会走 一直到 using 语句。

    确保1 是您的 Teensy 3.6 Arduino 的正确 I2C 地址,并检查 SDA 和 SCL 连接是否正确。请参阅Windows IoT Core pin mappings

    如果您希望 background application 始终运行,请添加 deferral = taskInstance.GetDeferral();。否则当 Run 方法结束时,后台应用程序结束。

    private BackgroundTaskDeferral deferral;
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        deferral = taskInstance.GetDeferral();
    
        //
        // TODO: Insert code to start one or more asynchronous methods
        //
    }
    

    先尝试标准模式速度,然后再尝试快速模式。

    【讨论】:

    • 嗨丽塔,感谢您的回复。对于地址,我使用的是 i2c_t3 库,我尝试了几个不同的地址,目前我正在使用这行代码为 Arduino 启动 i2c:Wire.begin(I2C_SLAVE, 1, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
    • 另外,我使用了延迟线,现在我收到了一个新错误。 sending i2c data Exception thrown: 'System.IO.FileNotFoundException' in i2cTestIOT.winmd WinRT information: Slave address was not acknowledged. Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.ni.dll WinRT information: Slave address was not acknowledged. 我已将引脚 3(pi) 连接到引脚 18(teensy),将引脚 5(pi) 连接到引脚 19(teensy)。我不确定是否需要将 Vout 和 Vin 连接到每个板上。 pjrc.com/teensy/td_libs_Wire.html
    • @Jared 将两个板的 GND 连接在一起并显示您的 Arduino 侧代码。
    • 我放弃了使用 I2C,我只是决定使用 UART,它可以完美运行。否则感谢您的帮助:)
    猜你喜欢
    • 2019-07-21
    • 2018-08-25
    • 2017-02-03
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多