这几天在搞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