windows mobile gps 串口通訊 NMEA命令和資料處理(1) (c#)

來源:互聯網
上載者:User
這幾天在搞wm上面的gps 想擷取到衛星資料 找了些資料 大概的就是這兩種方式:一,傳統的gps編程方式 通過串口讀取到gps裝置的資料,解析,處理二,應用windows mobile 提供的gpsid函數庫下面試 gps傳統編程1 串口通訊,首先開啟gps裝置的port並接收資料命名空間using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO.Ports;using GPS;//這個是gps裝置的類,下面將有介紹 public NMEAProtocol protocol = new NMEAProtocol();
        public SerialPort port = new SerialPort();  System.Text.Encoding encoding = System.Text.ASCIIEncoding.GetEncoding(1252);串連的事件private void connectButton_Click(object sender, System.EventArgs e)
  {
   connectButton.Enabled = false;
   disconnectButton.Enabled = true;
            COMlistBox.Enabled = false;
            port.PortName = COMlistBox.SelectedItem as string;
            port.Parity = Parity.None;
            port.BaudRate = 4800;
            port.StopBits = StopBits.One;
            port.DataBits = 8;
            port.Open();
   timer1.Enabled = true;
  } //timer控制項事件private void timer1_Tick(object sender, System.EventArgs e)
  {
   ReadData();
  }//讀取資料
  private void ReadData()
  {
   byte[] bData = new byte[256];   try
   {
    //bData = serialHelper.Read();
                port.Read(bData, 0, 256);    protocol.ParseBuffer(bData);
   }
   catch(Exception e)
   {
                System.Diagnostics.Debug.WriteLine(e.ToString());
    //swallow it.
   }//顯示資料   DisplayNMEARawData(bData);
   DisplayGeneralInfo();
   DisplaySatellites();
  }  private void DisplayNMEARawData(byte[] bData)
  {
   string sData="";
   if(null != bData)
   {
    sData = encoding.GetString(bData);
   }   if (dumpRawDataCheck.Checked)
   {
    //if dumped 100k of data get rid of the oldest 50k
    if(NMEAText.Text.Length > 100*1000)
    {
     NMEAText.Text = NMEAText.Text.Substring(50000,50000);    }    NMEAText.Text =    NMEAText.Text + sData;
    NMEAText.SelectionStart = NMEAText.Text.Length-1;
    NMEAText.ScrollToCaret();
   }
  }  private void DisplayGeneralInfo()
  {
   labelLatitude.Text = protocol.GPGGA.Latitude.ToString();
   labelLongitude.Text = protocol.GPGGA.Longitude.ToString();
   labelAltitude.Text = protocol.GPGGA.Altitude.ToString();            DateTime utc = DateTime.MinValue;               if (protocol.GPRMC.Month != 0 && protocol.GPRMC.Year != 0 && protocol.GPRMC.Day != 0)
            {
               
utc = new DateTime(protocol.GPRMC.Year + 2000, protocol.GPRMC.Month,
protocol.GPRMC.Day, protocol.GPGGA.Hour, protocol.GPGGA.Minute,
protocol.GPGGA.Second, DateTimeKind.Utc);
                labelDate.Text = utc.ToShortDateString();
                labelTimeLocal.Text = utc.ToLocalTime().ToString();
                labelTime.Text = utc.ToShortTimeString();
            }   listGPSQuality.SelectedIndex = (int)protocol.GPGGA.GPSQuality;            labelFixMode.Text = protocol.GPGSA.Mode == 'A' ? "Automatic" : "Manual";
            labelPDOP.Text = protocol.GPGSA.PDOP.ToString();
            labelVDOP.Text = protocol.GPGSA.VDOP.ToString();
            labelHDOP.Text = protocol.GPGSA.HDOP.ToString();            labelDataValid.Text = protocol.GPRMC.DataValid == 'A' ? "Data Valid" : "Navigation Receive Warning";  }  private void DisplaySatellites()
  {
   labelSatellitesInView.Text =  protocol.GPGSV.SatellitesInView.ToString();
   
            Pen circlePen = new Pen(System.Drawing.Color.DarkBlue,1);   Graphics g= picSats.CreateGraphics();
  
   int centerX = picSats.Width/2;
   int centerY = picSats.Height/2;   double maxRadius = (Math.Min(picSats.Height,picSats.Width)-20) / 2;   //draw circles
   double[] elevations = new double[] {0,Math.PI/2, Math.PI/3 ,Math.PI / 6};   foreach(double elevation in elevations)
   {
    double radius = (double)System.Math.Cos(elevation) * maxRadius;
    g.DrawEllipse(circlePen,(int)(centerX - radius) ,(int)(centerY - radius),(int)(2 * radius),(int)( 2*  radius));
   }
   //90 degrees elevation reticule
   g.DrawLine(circlePen,new Point(centerX-3,centerY),new Point(centerX + 3,centerY));
   g.DrawLine(circlePen,new Point(centerX,centerY-3),new Point(centerX,centerY+3));   Pen satellitePen = new Pen(System.Drawing.Color.LightGoldenrodYellow,4);   foreach(Satellite sat in protocol.GPGSV.Satellites.Values)
   {   
    //if has a listitem
    ListViewItem lvItem = (ListViewItem)sat.Thing;    if(null == lvItem)
    {
     lvItem =  new ListViewItem
      (
       new string[]
       {
        sat.Id.ToString() ,
        sat.Elevation.ToString(),
        sat.Azimuth.ToString(),
        sat.Used.ToString()
       }
      );     listSatellites.Items.Add(lvItem);
     
     sat.Thing = lvItem;//lvItem;
    }
    else
    {
     lvItem.Text = sat.Id.ToString();
     lvItem.SubItems[1].Text = sat.Elevation.ToString();
     lvItem.SubItems[2].Text = sat.Azimuth.ToString();
     lvItem.SubItems[3].Text = sat.Used.ToString();
    }    //draw satellites
    double h = (double)System.Math.Cos((sat.Elevation*Math.PI)/180) * maxRadius;
    
    int satX = (int)(centerX + h * Math.Sin((sat.Azimuth * Math.PI)/180));
    int satY = (int)(centerY - h * Math.Cos((sat.Azimuth * Math.PI)/180));    g.DrawRectangle(satellitePen,satX,satY, 4,4);                g.DrawString(sat.Id.ToString(), new Font("Verdana",
8, FontStyle.Regular), new System.Drawing.SolidBrush(Color.Black), new
Point(satX + 5, satY + 5));
   }
  }http://www.devdiv.com/home.php?mod=space&uid=26317&do=blog&id=1187

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.