简体   繁体   中英

C# Universal Windows App Aggregate Exception (Raspberry Pi)

I'm developing an app that makes use of the FTDI drivers as described by this github project https://github.com/Jark/FTDISample Other than my string comparisons everything is basically the same as this sample.

Currently I'm experiencing a system aggregate exception when using a singleton to parse a byte array received from the 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));
            }
        }

UPDATE

During another debugging session I found that the exception triggers after the await in the following method:

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);
        }
    }

UPDATE

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

The "MessageHandler" class only has that single method available and is just parsing a byte[] to string doing a couple comparisons and returning a simple string to write to the serial port.

What's happening is that somewhere in there it's spitting this to diagnostics output:

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

I'm not entirely sure what's causing it or how to catch it and it's leading to the app crashing intermittently.

I've figured out that something in the steps of comparing the strings and the writing it back to the serial port is where the error is happening.

Thanks!

I followed the documentation at http://www.ftdichip.com/Support/Documents/InstallGuides/AN_271%20D2xx%20WinRT%20Guide.pdf

And implemented the following format for reading data off the serial device.

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);

As per the downvote I am a bit unsure why that was done. If it's not obvious by reading my responses I was TRYING to catch the exception to unwrap it but was unsuccessful, and was looking for help in that respect.

Either way thanks for trying everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM