BasicGPS_WinNET
Form1.cs
// Copyright 2006 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See use restrictions at /arcgis/developerkit/userestrictions.
//
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
ESRI.ArcGIS.Mobile.Gps;
namespace
WinGPS
{
public
partial class
Form1 : Form
{
GpsEventArgs m_gpsea = null;
public
Form1()
{
InitializeComponent();
}
private void
butCon_Click(object
sender, EventArgs e)
{
butCon.Enabled = false;
butDiscon.Enabled = true;
serialPortGpsConnection1.Open();
if
(serialPortGpsConnection1.IsOpen)
{
timer1.Enabled = true;
}
else
{
butCon.Enabled = true;
butDiscon.Enabled = false;
timer1.Enabled = false;
}
}
private void
butDiscon_Click(object
sender, EventArgs e)
{
butDiscon.Enabled = false;
butCon.Enabled = true;
timer1.Enabled = false;
serialPortGpsConnection1.Close();
}
private void
DisplaySummary()
{
if
(m_gpsea == null
)
return;
labelLatitude.Text = m_gpsea.Latitude.ToString();
labelLongitude.Text = m_gpsea.Longitude.ToString();
labelAltitude.Text = m_gpsea.Altitude.ToString();
labelFixMode.Text = m_gpsea.FixStatus.ToString();
labelPDOP.Text = m_gpsea.Pdop.ToString();
labelVDOP.Text = m_gpsea.Vdop.ToString();
labelHDOP.Text = m_gpsea.Hdop.ToString();
DateTime utc = DateTime.MinValue;
utc = m_gpsea.Time;
labelDate.Text = utc.ToShortDateString();
labelTimeLocal.Text = utc.ToLocalTime().ToString();
labelTime.Text = utc.ToShortTimeString();
}
private void
DisplaySatellites()
{
labelSatellitesInView.Text = m_gpsea.FixSatelliteCount.ToString();
//code from Coding4Fun: http://msdn.microsoft.com/coding4fun/someassemblyrequired/whereami/default.aspx
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 satPen = new
Pen(System.Drawing.Color.Green, 4);
listSatellites.Items.Clear();
try
{
foreach
(Satellite sat in
m_gpsea.GetSatellites())
{
if
(null
== sat) return;
ListViewItem lvItem = new
ListViewItem
(
new
string[]
{
sat.Id.ToString(),
sat.Elevation.ToString(),
sat.Azimuth.ToString(),
sat.Snr.ToString(),
}
);
listSatellites.Items.Add(lvItem);
//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(satPen, 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));
}
}
catch
{
return;
}
}
private void
timer1_Tick(object
sender, EventArgs e)
{
DisplaySummary();
DisplaySatellites();
}
private void
serialPortGpsConnection1_GpsChanged(object
sender, GpsEventArgs e)
{
if
(InvokeRequired)
{
Invoke(new
GpsChangedEventHandler(serialPortGpsConnection1_GpsChanged), sender, e);
return;
}
m_gpsea = e;
if
(chkRaw.Checked)
{
NMEAText.Text = NMEAText.Text + e.Sentence + "\r\n";
}
}
private void
Form1_Load(object
sender, EventArgs e)
{
if
(string.IsNullOrEmpty(mapCache1.Url))
{
MessageBox.Show("To use the GPS sample you need to specify, at the design time, a URL of a mobile server that covers your area.", "GPS Sample error - MapCache.URL is empty.");
this.Close();
return;
}
//initialize
//serialPortGpsConnection1.PortName = "COM5"; // hardcoded
lblGPSComPort.Text = serialPortGpsConnection1.PortName;
butDiscon.Enabled = false;
//create the cache and open
if
(mapCache1.Exists)
mapCache1.Delete();
mapCache1.Create();
mapCache1.Open();
}
private void
map1_ExtentChanged(object
sender, EventArgs e)
{
mapCache1.GetDataAsync(map1, true
, null
);
}
private void
butClear_Click(object
sender, EventArgs e)
{
NMEAText.Clear();
}
}
}