using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using GNSSReceiver;
using System.Threading;
namespace LOG_AGNSS_Test
{
class DataOperation
{
public SerialPort d_CommPort;//工作串口
string d_PortMessage;//串口消息
Message_1 d_GNSSMessage=new Message_1();//格式化结构体
public void Init(string d_comm_port_name, int d_port_baudrate)
{
d_CommPort = new SerialPort(d_comm_port_name);
d_CommPort.BaudRate = d_port_baudrate;
d_CommPort.DataBits = 8;
d_CommPort.DataReceived += new SerialDataReceivedEventHandler(GetMessageFromEquipment);
d_CommPort.Open();
ComputesThread = new Thread(StartComputesThread);
ComputesThread.Start();
}
public string ALLMessage = "";
private void GetMessageFromEquipment(object sender, SerialDataReceivedEventArgs e)//从设备中获取信息(串口)
{
d_PortMessage = "";
d_PortMessage = d_CommPort.ReadExisting();
if (d_PortMessage != "")
{
ALLMessage+=d_PortMessage;
Console.WriteLine(d_PortMessage);
}
}
Thread ComputesThread;
public void StartComputesThread()
{
while(true)
{
Thread.Sleep(10);
if (d_CommPort .IsOpen&& d_CommPort.BytesToRead <= 0)
{
StartComputes();
}
}
}
public void StopComputesThread()
{
d_CommPort.Close();
ComputesThread.Abort();
}
public bool StartComputes()//开始解析
{
if (ALLMessage == "")
return false;
int charTag=0;
char char_Inspector = ALLMessage[charTag];
while (true)
{
if (char_Inspector=='#')//遇到控制信息LOG头
{
string computes_Order_String = "";
while (char_Inspector != '\r')//获取当前LOG字符串
{
computes_Order_String += char_Inspector;
charTag++;
if (charTag >= ALLMessage.Length)
break;
char_Inspector = ALLMessage[charTag];
}
string []Sections=computes_Order_String.Split(';');
switch (Sections[0].Substring(1,3))
{
case "BD2"://北斗星历
Sections = Sections[1].Split(',');
//Sections[0]-Sections[32]对应27页的表,表中ID-(减)2
if (Sections.Length == 33)//有效
{
}
break;
case "GPS"://GPS星历
Sections = Sections[1].Split(',');
//Sections[0]-Sections[31]对应37页的表,表中ID-(减)2
if (Sections.Length == 32)//有效
{
Console.WriteLine(Sections[0]);
}
//
//d_GNSSMessage.m_data.nav_model.EphemAF0=0;
break;
case "GLO"://GLO星历
Sections = Sections[1].Split(',');
//Sections[0]-Sections[28]对应35页的表,表中ID-(减)2
if (Sections.Length == 29)//有效
Console.WriteLine(Sections[28]);
break;
case "ION"://GPS电离层及UTC数据
Sections = Sections[1].Split(',');
//Sections[0]-Sections[16]对应46页的表,表中ID-(减)2
if (Sections.Length == 17)//有效
{
d_GNSSMessage.m_data.iono_model.Alfa0 = Convert.ToDouble(Sections[0]);
d_GNSSMessage.m_data.iono_model.Alfa1 = Convert.ToDouble(Sections[1]);
d_GNSSMessage.m_data.iono_model.Alfa2 = Convert.ToDouble(Sections[2]);
d_GNSSMessage.m_data.iono_model.Alfa3 = Convert.ToDouble(Sections[3]);
d_GNSSMessage.m_data.iono_model.Beta0 = Convert.ToDouble(Sections[4]);
d_GNSSMessage.m_data.iono_model.Beta1 = Convert.ToDouble(Sections[5]);
d_GNSSMessage.m_data.iono_model.Beta2 = Convert.ToDouble(Sections[6]);
d_GNSSMessage.m_data.iono_model.Beta3 = Convert.ToDouble(Sections[7]);
System.Reflection.FieldInfo[] fields = d_GNSSMessage.m_data.iono_model.GetType().GetFields();
foreach (System.Reflection.FieldInfo field in fields)
{
Console.WriteLine(field.Name + ":" + field.GetValue(d_GNSSMessage.m_data.iono_model)); //输出
}
Console.WriteLine();
}
break;
}
}
if (char_Inspector == '$')//遇到定位信息LOG头,
{
string nmea="";
while (char_Inspector!='\r') //获取定位信息字符串
{
nmea += char_Inspector;
charTag++;
if (charTag >= ALLMessage.Length)
break;
char_Inspector = ALLMessage[charTag];
}
if (NmeaObject.CheckIsValid(nmea))//校验和通过
{
string[] Sections = nmea.Split(new char[2] {',','*'});
switch (Sections[0].Substring(3,3))
{
case "GGA":
//Sections[0]-Sections[15]对应38页的表,表ID-(减)1
Console.WriteLine(Sections[1]);
break;
case "GSA":
//Sections[0]-Sections[18]对应39页的表,表ID-(减)1
Console.WriteLine(Sections[18]);
break;
case "GSV":
//Sections[0]-Sections[Sections.Length-1]对应41页的表,表ID-(减)1
Console.WriteLine(Sections[Sections.Length-1]);
break;
case "HDT":
//Sections[0]-Sections[3]对应42页的表,表ID-(减)1
Console.WriteLine(Sections[3]);
break;
case "RMC":
//Sections[0]-Sections[13]对应42页的表,表ID-(减)1
//Console.WriteLine(Sections[1].Substring(0, 2) + ":" + Sections[1].Substring(2, 2) + ":" + Sections[1].Substring(4, 5));
break;
case "VTG":
//Sections[0]-Sections[10]对应43页的表,表ID-(减)1
Console.WriteLine(Sections[10]);
break;
case "ZDA":
//Sections[0]-Sections[7]对应44页的表,表ID-(减)1
Console.WriteLine(Sections[7]);
break;
}
}
}
if (char_Inspector != '#')
{
charTag++;
if (charTag >= ALLMessage.Length)
break;
char_Inspector = ALLMessage[charTag];
}
}
ALLMessage = "";
return true;
}
private Decimal ChangeDataToD(string strData)//科学计数法转double
{
Decimal dData = 0.0M;
if (strData.Contains("E"))
{
dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Any);
}
return dData;
}
}
}
C#_读取串口信息并解析信息(字符串)
于 2020-05-06 13:03:31 首次发布