GNGGA:Global Positioning System Fix Data,这个句子提供了GPS定位的时间、日期、接收机天线位置的地理坐标(纬度和经度)、高度、质量因子()、模式指示符等。
GNVTG:Magnetic Track and Speed Over the Ground,这个句子提供了地磁航向和地速信息。
GNRMC:Recommended Minimum Navigation Information,这个句子提供了一些推荐的最小导航信息,包括日期、时间、船位、航向、速度等。
为了保存这些数据到Excel文件,我们可以使用EPPlus库。以下是一个基本的示例代码:
using System;
using System.IO.Ports;
using System.Collections.Generic;
using OfficeOpenXml;
public class Nmea0183Parser
{
private SerialPort _port;
private List<string[]> _data = new List<string[]>();
public Nmea0183Parser(string portName)
{
_port = new SerialPort(portName); // replace with your port name
_port.BaudRate = 9600; // replace with your baud rate
_port.Parity = Parity.None;
_port.StopBits = StopBits.One;
_port.DataBits = 8;
_port.Handshake = Handshake.None;
_port.RtsEnable = true;
}
public void Start()
{
_port.Open();
}
public void Stop()
{
_port.Close();
}
public void ParseNmeaSentence(string sentence)
{
var parts = sentence.Split(',');
var data = new string[parts.Length];
for (int i = 0; i < parts.Length; i++)
{
data[i] = parts[i].Trim();
}
_data.Add(data);
}
public void SaveToExcel()
{
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("NMEA Data");
for (int i = 0; i < _data.Count; i++)
{
for (int j = 0; j < _data[i].Length; j++)
{
worksheet.Cells[i + 1, j + 1].Value = _data[i][j];
}
}
var fileName = $"NMEA_data_{DateTime.Now:yyyyMMddHHmmss}.xlsx"; // you can customize the file name here.
package.SaveAs(new FileInfo(fileName));
}
}
}
然后解析的解析NMEA0183代码的示例:
public void ParseGNGGA(string sentence)
{
var parts = sentence.Split(‘,’);
var data = new Dictionary<string, string>();
for (int i = 1; i < parts.Length; i += 2)
{
data[parts[i - 1]] = parts[i];
}
// Now you can process the data dictionary here.
// For example, you can convert it to a DataTable and save it as an Excel file.
}
public void ParseGNVTG(string sentence)
{
var parts = sentence.Split(‘,’);
var data = new Dictionary<string, string>();
for (int i = 1; i < parts.Length; i += 2)
{
data[parts[i - 1]] = parts[i];
}
// Now you can process the data dictionary here.
// For example, you can convert it to a DataTable and save it as an Excel file.
}
public void ParseGNRMC(string sentence)
{
var parts = sentence.Split(‘,’);
var data = new Dictionary<string, string>();
for (int i = 1; i < parts.Length; i += 2)
{
data[parts[i - 1]] = parts[i];
}
// Now you can process the data dictionary here.
// For example, you can convert it to a DataTable and save it as an Excel file.
}
这些方法将NMEA句子解析为一个包含字段名称和值的字典。可以根据需要进一步处理这些数据,例如将其保存到Excel文件中。在上面的示例代码中,我已经添加了一个将数据保存到Excel文件中的方法(SaveToExcel)。你可以将解析的数据传递给该方法以保存为Excel文件。