For whatever reason, using GPS is a pain. The real breakthrough was in having the app catch exceptions and then trow them away and try again. I blame the poor implementation of serial port over bluetooth as well as the inexcusable lack of a working Bluetooth intermediate driver for smartphones. There are only a few things you cannot do without a stylus, and there is no need to require those sorts of things to change system settings.
Anyway, here's a small app that gives you lat/lon when you run it and hit the right soft key. status is a label covering the whole screen.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace GPSmkII
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool Parse(string sentence)
{
switch(sentence.Split(',')[0]) {
case "$GPRMC":
return parseLocation(sentence);
default:
return false;
}
}
public bool parseLocation(string sentence)
{
string[] words = sentence.Split(',');
if ((words[3] != "" && words[4] != "") && (words[5] != "" && words[6] != ""))
{
double lat = Convert.ToDouble(words[3].Substring(0, 2));
lat += Convert.ToDouble(words[3].Substring(2)) / 60.0;
double lon = Convert.ToDouble(words[5].Substring(0, 3));
lon += Convert.ToDouble(words[5].Substring(3)) / 60.0;
status.Text += "Lat: " + lat + '\n';
status.Text += "Lon: " + lon + '\n';
return true;
}
return false;
}
private void menuItem2_Click(object sender, EventArgs e)
{
while (true)
{
try
{
SerialPort sp1 = new SerialPort("COM8");
sp1.Open();
sp1.ReadTimeout = 2000;
while (!Parse(sp1.ReadLine()))
{ }
return;
}
catch (Exception ex)
{
status.Text += ex.Message + '\n';
}
}
}
private void menuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment