解析GNGGA,GNVTG,GNRMC这些NMEA句子的C#代码

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文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈嗨哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值