[英]COMException when running a Background UWP on Windows 10 IOT
I am developing an I2C solution for a Raspberry Pi and Teensy 3.6 Arduino . 我正在为Raspberry Pi和Teensy 3.6 Arduino开发I2C解决方案。 The Pi is going to send NTP via I2C to the Arduino.
Pi将通过I2C将NTP发送到Arduino。 I have done research for weeks, and tried to test the code below, and receive a couple errors.
我已经进行了数周的研究,并试图测试下面的代码,并收到一些错误。
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);
}
}
}
Using the debug console, I have found that the I2C method never goes all the way through the using statement. 使用调试控制台,我发现I2C方法永远不会一直通过using语句。
EXCEPTIONS: 例外:
Exception thrown: 'System.Runtime.InteropServices.COMException' in i2cTestIOT.winmd WinRT information: Unexpected number of bytes was transferred. 抛出异常:i2cTestIOT.winmd中的'System.Runtime.InteropServices.COMException'WinRT信息:传输了意外的字节数。 Expected: '.
预期:'。 Actual: '.
实际:'。 Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Private.CoreLib.ni.dll WinRT information: Unexpected number of bytes was transferred.
抛出异常:System.Private.CoreLib.ni.dll中的“System.Runtime.InteropServices.COMException”WinRT信息:传输了意外的字节数。 Expected: '.
预期:'。 Actual: '.
实际:'。
I can't reproduce this error with Raspberry pi and Arduino Uno using exact your code.(If it is a complete reproducible code). 我无法使用Raspberry pi和Arduino Uno使用您的代码重现此错误。(如果它是完整的可重现代码)。 So
所以
Using the debug console, I have found that the I2C method never goes all the way through the using statement.
使用调试控制台,我发现I2C方法永远不会一直通过using语句。
Make sure the 1
is the right I2C address for your Teensy 3.6 Arduino and check the SDA and SCL connect correctly. 确保
1
是Teensy 3.6 Arduino的正确I2C地址,并检查SDA和SCL连接是否正确。 Refer to Windows IoT Core pin mappings . 请参阅Windows IoT核心引脚映射 。
Add deferral = taskInstance.GetDeferral();
添加
deferral = taskInstance.GetDeferral();
if you want the background application always running. 如果您希望后台应用程序始终运行。 Otherwise when the Run method ends, the background application ends.
否则,当Run方法结束时,后台应用程序结束。
private BackgroundTaskDeferral deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
deferral = taskInstance.GetDeferral();
//
// TODO: Insert code to start one or more asynchronous methods
//
}
Try the StandardMode speed first then FastMode later. 首先尝试StandardMode速度,然后再尝试FastMode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.