UWP 应用变慢,然后在一定数量的串行写入后崩溃

UWP app slows, then crashes after certain number of serial writes

我正在 Raspberry Pi 3.

上使用 Windows IoT Core 制作应用程序

我正在尝试使用串行通信与 Arduino 通信。我在 GitHub 上使用了此示例中显示的大部分方法来创建串行设备并写入它:https://github.com/ms-iot/samples/tree/develop/SerialSample/CS.

程序在前 130 个字节左右运行良好,但之后会开始急剧变慢,并在第 135 个字节左右崩溃。

有时它会导致 Raspberry Pi 显示 Windows 10 "blue screen of death" 表示需要重新启动。

我已经尝试通过多种方式重写代码并使用异步、任务和等待的不同变体,但似乎无法让它工作。

这是我用来初始化串口设备的代码:

private async void setup()
{
    serialPort = await initializeSerial();
}

private async Task<SerialDevice> initializeSerial()
{
    try
    {
        string aqs = SerialDevice.GetDeviceSelector();
        var dis = await DeviceInformation.FindAllAsync(aqs);

        for (int i = 0; i < dis.Count; i++)
        {
            if (dis[i].Name.Contains("Arduino Uno"))
            {
                var serialPort = await SerialDevice.FromIdAsync(dis[i].Id);

                /* Configure serial settings */
                serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
                serialPort.BaudRate = 9600;
                serialPort.Parity = SerialParity.None;
                serialPort.StopBits = SerialStopBitCount.One;
                serialPort.DataBits = 8;
                serialPort.Handshake = SerialHandshake.None;

                // Display configured settings
                //Debug.WriteLine("Serial port configured successfully.");

                // Create cancellation token object to close I/O operations when closing the device
                ReadCancellationTokenSource = new CancellationTokenSource();


                return serialPort;
            }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }

    return null;
}

这是我用来写入串行设备的代码:

private async void sendString(string stringToSend)
{
    try
    {
        if (serialPort != null)
        {
            // Create the DataWriter object and attach to OutputStream
            dataWriteObject = new DataWriter(serialPort.OutputStream);

            //Launch the WriteAsync task to perform the write
            await WriteAsync(stringToSend);
        }
        else
        {
            status.Text = "Select a device and connect";
        }
    }
    catch (Exception ex)
    {
        status.Text = "sendString: " + ex.Message;
    }
    finally
    {
            // Cleanup once complete
            if (dataWriteObject != null)
            {
                dataWriteObject.DetachStream();
                dataWriteObject = null;
            }
    }
}

/// <summary>
/// WriteAsync: Task that asynchronously writes data from the input text box 'sendText' to the OutputStream 
/// </summary>
/// <returns></returns>
private async Task WriteAsync(string stringToWrite)
{
    Task<UInt32> storeAsyncTask;

    dataWriteObject.WriteString(stringToWrite);                

    // Launch an async task to complete the write operation
    storeAsyncTask = dataWriteObject.StoreAsync().AsTask();

    UInt32 bytesWritten = await storeAsyncTask;
    if (bytesWritten > 0)
    {
        counter++;
        status.Text = stringToWrite + ", ";
        status.Text += "bytes written successfully! (" + counter + ")";
    }       
}

任何 help/advice 将不胜感激。

更新

我重写了我的 sendString 方法,并使用 using 语句简化了它:

private async void sendString(string stringToSend)
{
    using (DataWriter dataWriter = new DataWriter(serialPort.OutputStream))
    {
        dataWriter.WriteString(stringToSend);
        await dataWriter.StoreAsync();
        //await dataWriter.FlushAsync();
        dataWriter.DetachStream();

        counter++;
        status.Text = stringToSend + ", ";
        status.Text += "bytes written successfully! (" + counter + ")";
    }
}

但是,问题依然存在。

我尝试使用 UART 引脚而不是 USB 电缆写入数据,并且没有遇到更多问题。这是我用来在 Pi 上初始化 UART 总线的代码,我使用与上面使用的相同的函数编写:

private async Task<SerialDevice> initializeSerial()
{
    try
    {
        string aqs = SerialDevice.GetDeviceSelector("UART0");
        var dis = await DeviceInformation.FindAllAsync(aqs);

        SerialDevice serialPort = await SerialDevice.FromIdAsync(dis[0].Id);

        /* Configure serial settings */
        serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
        serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
        serialPort.BaudRate = 9600;
        serialPort.Parity = SerialParity.None;
        serialPort.StopBits = SerialStopBitCount.One;
        serialPort.DataBits = 8;
        serialPort.Handshake = SerialHandshake.None;

        status.Text = "Serial port configured succesfully.";

        return serialPort;
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }

    status.Text = "Serial port not configured successfully. Are you sure a serial device is connected?";
    return null;
}

这并不能完全解决通过 USB 数据线发送数据的问题,但我将其张贴在这里,以防有人在使用 USB 端口时遇到类似问题并想尝试这样做。