1

我在从 Nave32 Flight Controller 读取数据时遇到了一点问题,我可以使用 SerialDevice 类将数据发送到卡,但是当调用 LoadAsync 方法并且没有更多数据可以读取应用程序挂起并且不前进时,进程执行过程中没有异常。我查看了应用程序状态,有一堆任务块,我不知道为什么。见下图

更新

固件源代码在这里

这是我的示例代码

namespace Apps.Receiver
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();
            this.dataRecived.Text = DateTime.Now.ToString();
        }

        private async Task readAsync(SerialDevice serialPort)
        {
            string data = string.Empty;
            using (var dataReaderObject = new DataReader(serialPort.InputStream))
            {
                try
                {
                    dataReaderObject.InputStreamOptions = InputStreamOptions.None;
                    var hasData = true;
                    while (hasData)
                    {

                        var bytesRead = await dataReaderObject.LoadAsync(serialPort.DataBits);
                        if (bytesRead > 0)
                            data += dataReaderObject.ReadString(bytesRead);
                        else
                            hasData = false;


                    }
                }
                catch (Exception ex)
                {
                    status.Text = "reading data fail: " + ex.Message;
                    closeDevice(serialPort);
                }
            }

            dataRecived.Text = data;
        }

        private async void sendData_Click(object sender, RoutedEventArgs e)
        {

            var serialPort = await getSerialDevice("COM3");

            if (dataToSend.Text.Length != 0)
            {

                using (var dataWriteObject = new DataWriter(serialPort.OutputStream))
                {
                    try
                    {
                        if (dataToSend.Text.Equals("#", StringComparison.CurrentCultureIgnoreCase))
                            dataWriteObject.WriteString(dataToSend.Text);
                        else
                            dataWriteObject.WriteString(dataToSend.Text + "\n");

                        var bytesWritten = await dataWriteObject.StoreAsync();
                        if (bytesWritten > 0)
                        {
                            status.Text = dataToSend.Text + ", ";
                            status.Text += "bytes written successfully!";
                        }
                        await readAsync(serialPort);

                    }
                    catch (Exception ex)
                    {
                        status.Text = "writing data fail: " + ex.Message;
                    }
                }
                closeDevice(serialPort);
            }
            else
            {
                status.Text = "Enter the text you want to write and then click on 'WRITE'";
            }
        }

        private void closeDevice(SerialDevice device)
        {
            device.Dispose();
            device = null;
        }

        private async Task<SerialDevice> getSerialDevice(string portName)
        {
            var aqs = SerialDevice.GetDeviceSelector("COM3");
            var devices = await DeviceInformation.FindAllAsync(aqs);

            if (devices.Count == 0)
                throw new Exception("Unablet to connect to device");

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

            if (serialPort == null)
                throw new Exception("Unablet to Open to port " + portName);


            serialPort.BaudRate = 115200;
            serialPort.DataBits = 8;

            return serialPort;
        }

    }
}

在此处输入图像描述

4

1 回答 1

-1

请参阅此示例,尝试这样做是否有帮助:

await reader.LoadAsync(8192);   
while (reader.UnconsumedBufferLength > 0)   
{   
    strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength));   
    await reader.LoadAsync(8192);   
}   
于 2016-01-04T15:39:03.287 回答