【发布时间】: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