简体   繁体   中英

C# UWP reading serial data throws exception

So I found a way to read serial data from the UART channel, using windows 10 iot core. Now, I get some data spit out, but, it crashes like instantly and returns Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.ni.dll .

The code I use to get the data:

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

Now this code is found on https://docs.microsoft.com/en-us/windows/iot-core/learn-about-hardware/pinmappings/pinmappingsrpi

The data it spits out:

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

I need to run this multi-threaded, since I need to update the GUI and store data and process data and get data from localstorage and display it. But that's out of the scope of this question. The issue here is, every second I got new GPS data, but it just crashes and I don't know why...

'System.NullReferenceException' occurs at this line: var dis = await DeviceInformation.FindAllAsync(aqs); because you repeatedly call SerialDevice.GetDeviceSelector("UART0"); but only first time you will get the UART0 device other you will get null result of aqs .

The right format is initialize the device one time and read date in a while loop. For example like this:

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

                });

            }

    }

For example, you can call above function in a button click event to start the read operation:

Private void Button_Click(object sender, RoutedEventArgs e)
{
    read_gps_data();
}

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