Here is the code. I am using a modifies serial.cs as the standard one has memory problems with line received. The only change is to comment out the definition of the datareceived event so that it never gets called.
using System;
using System.Text;
using System.Collections;
using System.IO.Ports;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GTI = Gadgeteer.Interfaces;
namespace GadgeteerApp1
{
public partial class Program
{
private static GTI.Serial serial { get; set; }
static GTI.DigitalOutput ResetPin;
private const string DELIMITER = "\r\n";
private const double EarthRadiusMetres = 6376500;
private const double deg2rad = 0.01745329251994329576923690768489;
private string VTGString, GGAString;
static double odo, tod, latitude, longitude;
static double distance, totaldist = 0;
static double lastlat, lastlong;
static double speed;
private static bool gpsOK = false;
// Calibration and units
double SecInDay = 3600 * 24;
void ProgramStarted()
{
display_T35.SimpleGraphics.Clear();
display_T35.SimpleGraphics.DisplayText("Rally GPS initializing...", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Magenta, 10, 10);
GT.Socket socket = GT.Socket.GetSocket(7, true, null, "U");
ResetPin = new GTI.DigitalOutput(socket, GT.Socket.Pin.Three, true, null);
serial = new GTI.Serial(socket, 115200, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.One, 8, GTI.Serial.HardwareFlowControl.NotRequired, null);
serial.LineReceivedEventDelimiter = DELIMITER;
serial.AutoReadLineEnabled = true;
Thread.Sleep(500);
//serial.DiscardInBuffer();
serial.Open();
serial.LineReceived += new GTI.Serial.LineReceivedEventHandler(serial_LineReceived);
}
void serial_LineReceived(GTI.Serial sender, string line)
{
if (line.Substring(4, 3) == "VTG")
{
VTGString=line;
processgps();
}
else if(line.Substring(4, 3) == "GGA")
{
GGAString = line;
}
}
void processgps()
{
string VTG, GGA, ts;
int hh, mm;
double sec;
VTG = VTGString;
GGA = GGAString;
speed = double.Parse(VTG.Substring(33, 7));
hh = int.Parse(GGA.Substring(8,2));
mm = int.Parse(GGA.Substring(10, 2));
sec = double.Parse(GGA.Substring(12, 4));
tod = (3600 * hh) + (60 * mm) + sec;
ts = sec2HMS(tod);
lastlat = latitude;
lastlong = longitude;
latitude=latitudefromgps(GGA.Substring(17,10));
longitude = longitudefromgps(GGA.Substring(30, 10));
if (gpsOK) //gpsOK is set after the first pass through this routine and means tha lastlat and lastlong have valid locations so we can start tracking.
{
distance = gpsdistance(lastlat, lastlong, latitude, longitude);
totaldist += distance;
odo = totaldist/1000.0;
}
display_T35.SimpleGraphics.Clear();
display_T35.SimpleGraphics.DisplayText(speed.ToString("F2"), Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Magenta, 10, 10);
display_T35.SimpleGraphics.DisplayText(ts, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Magenta, 10, 40);
//display_T35.SimpleGraphics.DisplayText(latitude.ToString("F8"), Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Magenta, 10, 70);
//display_T35.SimpleGraphics.DisplayText(longitude.ToString("F8"), Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Magenta, 10, 100);
display_T35.SimpleGraphics.DisplayText(odo.ToString("F3"), Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Magenta, 10, 130);
gpsOK = true;
}
String sec2HMS(double t)
{
double thetime;
string hms, tmp;
int h, m;
double s;
thetime = t;
s = thetime / 3600.0;
h = (int)s;
thetime = thetime - (h * 3600);
s = thetime / 60.0;
m = (int)s;
s = thetime - (m * 60);
tmp = h.ToString("N0");
if (tmp.Length == 1) tmp = "0" + tmp;
hms = tmp + ":";
tmp = m.ToString("N0");
if (tmp.Length == 1) tmp = "0" + tmp;
hms = hms + tmp + ":";
tmp = s.ToString("F1");
if (tmp.Length == 3) tmp = "0" + tmp;
hms = hms + tmp;
return hms;
}
double latitudefromgps(string s)
{
double degrees=double.Parse(s.Substring(0,2))+(double.Parse((s.Substring(2)))/60);
return degrees;
}
double longitudefromgps(string s)
{
double degrees = double.Parse(s.Substring(0, 3)) + (double.Parse((s.Substring(3))) / 60);
return degrees;
}
double gpsdistance(double lla, double llo, double cla, double clo)
// lla= last latitude, cla=currenlt latitude, llo and clo and longitude equivalents.
// llar=lla expressed in radianns etc.
{
double llar = lla * deg2rad;
double llor = llo * deg2rad;
double clar = cla * deg2rad;
double clor = clo * deg2rad;
double dlat = clar - llar;
double dlong = clor - llor;
double a = System.Math.Pow(System.Math.Sin(dlat / 2.0), 2) + (System.Math.Cos(lla) * System.Math.Cos(cla) * System.Math.Pow(System.Math.Sin(dlong / 2.0), 2));
double c = 2.0 * System.Math.Atan2(System.Math.Sqrt(a), System.Math.Sqrt(1 - a));
double d = c*EarthRadiusMetres;
return d;
}
}
}