C#UWP读取串行数据引发异常
所以我找到了一种使用Windows10IoT内核从UART通道读取串行数据的方法。现在,我得到了一些数据,但是,它像立即崩溃一样返回抛出的异常:System.Private.CoreLib.ni.dll中的System.NullReferenceException 我用于获取数据的代码:C#UWP读取串行数据引发异常,c#,gps,raspberry-pi3,uart,windows-10-iot-core,C#,Gps,Raspberry Pi3,Uart,Windows 10 Iot Core,所以我找到了一种使用Windows10IoT内核从UART通道读取串行数据的方法。现在,我得到了一些数据,但是,它像立即崩溃一样返回抛出的异常:System.Private.CoreLib.ni.dll中的System.NullReferenceException 我用于获取数据的代码: private async Task read_gps_data(string port = "UART0", int baud = 9600) { try
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);
}
}
现在可以在上找到此代码
它吐出的数据:
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并存储数据和流程数据,从localstorage获取数据并显示它。但这超出了这个问题的范围。这里的问题是,每秒钟我都会得到新的GPS数据,但它只是崩溃了,我不知道为什么…'System.NullReferenceException'出现在这一行:
var dis=wait DeviceInformation.findalsync(aqs)因为您反复调用SerialDevice.GetDeviceSelector(“UART0”)
但只有在第一次获得UART0设备时,您才会获得null
的aqs
结果
正确的格式是一次初始化设备,然后在while循环中读取日期。例如:
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;
});
});
}
}
例如,您可以在按钮单击事件中调用上述函数以启动读取操作:
Private void Button_Click(object sender, RoutedEventArgs e)
{
read_gps_data();
}
“System.NullReferenceException”出现在此行:var dis=wait DeviceInformation.findalsync(aqs)因为您反复调用SerialDevice.GetDeviceSelector(“UART0”)
但只有在第一次获得UART0设备时,您才会获得null
的aqs
结果
正确的格式是一次初始化设备,然后在while循环中读取日期。例如:
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;
});
});
}
}
例如,您可以在按钮单击事件中调用上述函数以启动读取操作:
Private void Button_Click(object sender, RoutedEventArgs e)
{
read_gps_data();
}
您的阅读速度可能比GPS发送的速度快。在从串行端口读取数据之前,请确保有可用的字节。我也不会在while(true)
循环中反复设置串行端口。这会引起问题。我本来打算把它放在一个类中,但后来我在SerialPort
中遇到了问题,因为在当前上下文中找不到它。。。还有,我有两个等待接线员在那里,所以我不知道如何处理。。。除此之外,设备读取速度可能比接收速度快,是否有可能同步它们?或者等待数据然后读取?@Naikrovek我修复了类,因此现在我可以通过类调用我的数据,只请求数据,而不一直设置设备。你的读取速度可能比GPS发送的速度快。在从串行端口读取数据之前,请确保有可用的字节。我也不会在while(true)
循环中反复设置串行端口。这会引起问题。我本来打算把它放在一个类中,但后来我在SerialPort
中遇到了问题,因为在当前上下文中找不到它。。。还有,我有两个等待接线员在那里,所以我不知道如何处理。。。除此之外,设备读取速度可能比接收速度快,是否有可能同步它们?或者等待数据然后读取?@Naikrovek我修复了这个类,所以现在我可以通过一个类调用我的数据,只是请求数据,而不是一直设置设备。我尝试使用你的代码(你错过了任务
或无效
),但后来我遇到了同样的问题。它获取数据,但随后就崩溃了,难道没有一种方法可以同步设备并监听输入数据吗?@Robin据我所知,唯一的方法是等待数据到达,而没有回调或事件通知。代码对我有用。如何调用read_gps_data()函数以及哪一行导致崩溃?好的,让它工作,但现在所有其他线程都停止,系统运行非常缓慢,难道没有办法“等待”直到缓冲区中接收到“所有”数据并每秒读取数据吗?@Robin等待,直到所有数据都会导致UI在这段时间内失去响应。我的建议是将缓冲区设置得足够大,以便在一次(一秒钟)内接收发送的数据。@Robin一次或一秒钟从发送方发送多少字节?我尝试使用您的代码(您错过了任务
或无效
),但随后我遇到了同样的问题。它获取数据,但随后就崩溃了,难道没有一种方法可以同步设备并监听输入数据吗?@Robin据我所知,唯一的方法是等待数据到达,而没有回调或事件通知。代码对我有用。如何调用read_gps_data()函数以及哪一行导致崩溃?好的,让它工作,但现在所有其他线程都停止,系统运行非常缓慢,难道没有办法“等待”直到缓冲区中接收到“所有”数据并每秒读取数据吗?@Robin等待,直到所有数据都会导致UI在这段时间内失去响应。我的建议是将缓冲区设置得足够大,以便在一次(一秒钟)内接收发送的数据。@Robin一次或一秒钟从发送方发送多少字节?