C#UWP读取串行数据引发异常

时间:2018-06-27 17:03:32

标签: c# gps raspberry-pi3 uart windows-10-iot-core

因此,我找到了一种使用Windows 10 IoT核心版从UART通道读取串行数据的方法。现在,我得到了一些数据,但是它立即崩溃并返回Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.ni.dll

我用来获取数据的代码:

private async Task read_gps_data(string port = "UART0", int baud = 9600)
        {
            try
            {
                await Task.Run(async () =>
                {
                    while (true)
                    {
                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                        {
                            string aqs = SerialDevice.GetDeviceSelector("UART0");                   /* Find the selector string for the serial device   */
                            var dis = await DeviceInformation.FindAllAsync(aqs);                    /* Find the serial device with our selector string  */
                            SerialDevice SerialPort = await SerialDevice.FromIdAsync(dis[0].Id);    /* Create an serial device with our selected device */

                            /* Configure serial settings */
                            SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                            SerialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
                            SerialPort.BaudRate = 9600;                                             /* mini UART: only standard baudrates */
                            SerialPort.Parity = SerialParity.None;                                  /* mini UART: no parities */
                            SerialPort.StopBits = SerialStopBitCount.One;                           /* mini UART: 1 stop bit */
                            SerialPort.DataBits = 8;

                            ///* Write a string out over serial */
                            //string txBuffer = "Hello Serial";
                            //DataWriter dataWriter = new DataWriter();
                            //dataWriter.WriteString(txBuffer);
                            //uint bytesWritten = await SerialPort.OutputStream.WriteAsync(dataWriter.DetachBuffer());

                            /* Read data in from the serial port */
                            const uint maxReadLength = 1024;
                            DataReader dataReader = new DataReader(SerialPort.InputStream);
                            uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
                            string rxBuffer = dataReader.ReadString(bytesToRead);
                            Debug.WriteLine(rxBuffer);
                            Debug.WriteLine("\n");
                        });
                    }
                });
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

现在可以在https://docs.microsoft.com/en-us/windows/iot-core/learn-about-hardware/pinmappings/pinmappingsrpi

上找到此代码

它吐出的数据:

Exception thrown: 'System.NullReferenceException' in Tripmaster.exe
,ANTSTATUS=OPEN*2B
$GPRMC,165556.000,A,5048.7305,N,00305.1325,E,0.00,109.81,270618,,,D*6F
$GPVTG,109.81,T,,M,0.00,N,0.00,K,D*39
$GPGGA,165556.000,5048.7305,N,00305.1325,E,2,7,1.41,8.2,M,47.2,M,,*58
$GPGSA,A,3,20,27,10,32,22,14,08,,,,,,2.41,1.41,1.96*05
$GPGSV,4,1,13,08,73,179,27,18,68,282,,11,54,285,,01,42,274,*7F
$GPGSV,4,2,13,10,37,055,34,27,37,145,28,22,32,214,33,32,32,101,31*70
$GPGSV,4,3,13,28,27,312,,36,25,145,35,14,21,127,31,20,10,052,29*78
$GPGSV,4,4,13,03,09,217,15*41
$GPGLL,5048.7305,N,00305.1325,E,165556.000,A,D*53
$GPTXT,01,01,02,ANTSTATUS=OPEN*2B
$GPRMC,165557.000,A,5048.7305,N,00305.1325,E,0.00,109.81,270618,,,D*6E
$GPVTG,109.81,T,,M,0.00,N,0.00,K,D*39
$GPGGA,165557.000,5048.7305,N,00305.1325,E,2,7,1.41,8.2,M,47.2,M,,*59
$GPGSA,A,3,20,27,10,32,22,14,08,,,,,,2.41,1.41,1.96*05
$GPGSV,4,1,13,08,73,179,26,18,68,282,,11,54,285,,01,42,274,*7E
$GPGSV,4,2,13,10,37,055,33,27,37,145,27,22,32,214,32,32,32,101,30*78
$GPGSV,4,3,13,28,27,312,,36,25,145,34,14,21,127,30,20,10,052,29*78
$GPGSV,


Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.ni.dll

我需要运行此多线程,因为我需要更新GUI并存储数据和处理数据,并从本地存储中获取数据并显示它。但这超出了这个问题的范围。这里的问题是,我每秒都会收到新的GPS数据,但是它崩溃了,我不知道为什么...

1 个答案:

答案 0 :(得分:2)

'System.NullReferenceException'发生在此行:var dis = await DeviceInformation.FindAllAsync(aqs);,因为您反复调用SerialDevice.GetDeviceSelector("UART0");,但是只有第一次获得UART0设备时,您才会得到null的结果{{ 1}}。

正确的格式是将设备初始化一次,并在while循环中读取日期。例如这样的

aqs

例如,您可以在按钮单击事件中调用上述函数以启动读取操作:

    private async void read_gps_data(string port = "UART0", int baud = 9600)
    {
            string aqs = SerialDevice.GetDeviceSelector("UART0");                   /* Find the selector string for the serial device   */
            var dis = await DeviceInformation.FindAllAsync(aqs);                    /* Find the serial device with our selector string  */
            SerialDevice SerialPort = await SerialDevice.FromIdAsync(dis[0].Id);    /* Create an serial device with our selected device */

            /* Configure serial settings */
            SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            SerialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
            SerialPort.BaudRate = 9600;                                             /* mini UART: only standard baudrates */
            SerialPort.Parity = SerialParity.None;                                  /* mini UART: no parities */
            SerialPort.StopBits = SerialStopBitCount.One;                           /* mini UART: 1 stop bit */
            SerialPort.DataBits = 8;


            /* Read data in from the serial port */
            const uint maxReadLength = 1024;
            DataReader dataReader = new DataReader(SerialPort.InputStream);

            string rxBuffer = "";

            while (true)
            {
                await Task.Run(async () =>
                {
                    try
                    {
                        uint bytesToRead = await dataReader.LoadAsync(maxReadLength);
                        rxBuffer = dataReader.ReadString(bytesToRead);
                        Debug.WriteLine(rxBuffer);
                        Debug.WriteLine("\n");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }

                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                    {
                        // Update UI here
                        receivedData.text = rxBuffer;
                    });

                });

            }

    }