C# 通用 Windows 应用聚合异常 (Raspberry Pi)

C# Universal Windows App Aggregate Exception (Raspberry Pi)

我正在开发一个使用 FTDI 驱动程序的应用程序,如此 github 项目 https://github.com/Jark/FTDISample 所述 除了我的字符串比较之外,其他一切都与这个示例基本相同。

目前我在使用单例解析从 usb-serial 接收的字节数组时遇到系统聚合异常:

while (true) // todo: build in cancellation support
        {
            try
            {
                var bytesInQueue = device.GetQueueStatus();
                bytesInQueue = Math.Max(bytesInQueue, 1); // to make sure we don't create a cpu eating loop

                var buffer = new byte[bytesInQueue];
                var bytesRead = await device.ReadAsync(buffer, bytesInQueue);
                if (bytesRead != 0)
                    string response = MessageHandler.HandleMessage(buffer.Take((int)bytesRead));
                    if(response != "NONE"){
                        OnWriteASCII(response); 
                    }
            }
            catch (Exception ex)
            {
                WriteToLog(string.Format("Exception occurred: {0}", ex.Message));
            }
        }

更新

在另一个调试会话中,我发现异常在以下方法中的 await 之后触发:

private async Task WriteBytes(byte[] bytesToWrite) {
        try {
            var nrBytesToWrite = bytesToWrite.Length;
  HERE>>    var bytesWritten = await device.WriteAsync(bytesToWrite, (uint)nrBytesToWrite);

            if (bytesWritten != nrBytesToWrite)
                WriteToLog("Write failed, bytes written: '{0}', count: {1}.",
                            Encoding.ASCII.GetString(bytesToWrite).Replace("\r",""), nrBytesToWrite);
            //else
            //    WriteToLog("Written: '{0}' to device, count: {1}.", Encoding.ASCII.GetString(bytesToWrite), nrBytesToWrite);
        } catch (Exception ex) {
            WriteToLog("Failed to write: '{0}' to device, Exception={1}.", BitConverter.ToString(bytesToWrite), ex.Message);
        }
    }

更新

mscorlib.ni.dll!System.Threading.Tasks.Task.ThrowIfExceptional(bool includeTaskCanceledExceptions)  Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task<uint>.GetResultCore(bool waitCompletionNotification)    Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task<uint>.Result.get()  Unknown
FTDI.D2xx.WinRT.USB.winmd!FTDI.D2xx.WinRT.USB.BulkRequest.IssueBulkOut(byte[] data, uint count) Unknown
FTDI.D2xx.WinRT.USB.winmd!FTDI.D2xx.WinRT.USB.BulkRequest.Write.AnonymousMethod__16()   Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task<uint>.InnerInvoke() Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task.Execute()   Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj)    Unknown
mscorlib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)  Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution)  Unknown
mscorlib.ni.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()  Unknown
mscorlib.ni.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Unknown
mscorlib.ni.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()  Unknown

"MessageHandler" class 只有一个方法可用,只是将字节 [] 解析为字符串,进行几次比较并返回一个简单的字符串以写入串行端口。

正在发生的事情是它在某处将此吐到诊断输出中:

Exception thrown: 'System.AggregateException' in mscorlib.ni.dll

我不完全确定是什么导致了它或如何捕获它,它会导致应用程序间歇性崩溃。

我发现错误发生在比较字符串并将其写回串口的步骤中。

谢谢!

我遵循了 http://www.ftdichip.com/Support/Documents/InstallGuides/AN_271%20D2xx%20WinRT%20Guide.pdf

中的文档

并实现了以下格式以从串行设备读取数据。

var action = ThreadPool.RunAsync(async (source) =>
{
  byte[] dataTx = new byte[10];
  for (int i = 0; i < dataTx.Length; i++)
  dataTx[i] = (byte)i;
  while (!cancellationTokenSource.Token.IsCancellationRequested)
  {
     byte[] dataRx = new byte[10];
     await myDevice.WriteAsync(dataTx, 10);
     myDevice.ReadAsync(dataRx, 10);
  }
}, WorkItemPriority.Normal);

根据反对票,我有点不确定为什么这样做。如果通过阅读我的回复不是很明显,我正在尝试捕获异常以展开它但没有成功,并且正在寻求这方面的帮助。

总之谢谢大家的尝试。