C#_读取串口信息并解析信息(字符串)

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

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#串口通信数据解析是指在使用C#编程语言进行串口通信时,对接收到的数据进行解析和处理的过程。首先,需要设置串口通信的参数,如波特率、数据位、停止位和奇偶校验等,以确保两个通信端口的参数匹配。然后,通过串口类提供的方法和事件来实现数据的接收和解析。 在C#中,可以使用SerialPort类来进行串口通信。首先,需要实例化一个SerialPort对象,并设置好串口参数。然后,可以通过订阅DataReceived事件来接收串口数据。当有数据接收到时,该事件会触发,可以在事件处理程序中进行数据解析的操作。 在进行数据解析时,可以根据具体的通信协议和数据格式进行处理。例如,可以使用字节流方式读取接收到的数据,并根据数据的长度和格式进行解析。可以根据数据包的起始标志和结束标志来提取有效数据,并进行相应的处理。还可以使用BitConverter类来将字节数组转换为相应的数据类型,以便进行后续的操作。 另外,也可以根据具体需求使用正则表达式来匹配和提取特定模式的数据。正则表达式可以用于匹配特定的字符串模式,并提取出需要的数据部分。 总之,C#串口通信数据解析是通过设置串口参数、接收串口数据,并根据具体的通信协议和数据格式进行解析和处理的过程。这个过程可以根据具体需求和情况进行定制和扩展。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C#实现串口通信解析](https://blog.csdn.net/kalvin_y_liu/article/details/126885528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值