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

本文关键字:崩溃 应用程序 然后 UWP | 更新日期: 2023-09-27 17:49:01

我正在树莓派3上使用Windows IoT Core制作应用程序。

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

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

有时它会导致树莓派显示Windows 10"蓝屏死机",说它需要重新启动。

我试过用很多方法重写代码,使用async、task和await的不同变体,但似乎不能让它工作。

下面是我用来初始化串行设备的代码:
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 + ")";
    }       
}

任何帮助/建议将不胜感激。

更新

我重写了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 + ")";
    }
}

然而,问题仍然存在。

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

我尝试使用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端口时遇到类似的问题,并想尝试这个。