I am using the “66 Channel LS20031 GPS 10Hz Receiver”.
I’ve downloaded the driver from TinyWiki.
GPS is connected to COM2 with the following connection:
GPS -> FEZ
Pin1(VDD) -> 3v3
Pin2(Rx) -> TTL Tx
Pin3(Tx) -> TTL Rx
GND -> GND
GND -> GND
The GPS is working, the led on the gps component is flashing when he has enough satellites.
This is the coordinates i am receiving (in string).
Lon: 00525.1767
Lat: 5137.5706
Not the place where i am living. First, it’s 50 km off… Second, the dots are wrong placed, or is it me? Thought it should be something like this:
Lat: 5.251765
Lon: 51.375703
This is the driver code i am using now. Can someone look at this place
//
// Import Namespaces
//
using System;
using System.IO.Ports;
using System.Threading;
using Microsoft.SPOT;
//
// FEZ NETMF Namespace
//
namespace GHIElectronics.NETMF.FEZ
{
//
// FEZ_Components Partial Class
//
public static partial class FEZ_Components
{
//
// Helper class to work with GPS
//
public class GPS
{
//
// Default Variables
//
char[] split_char = new char[] { ',' };
string[] sentence_fields;
SerialPort _port;
byte[] buffer = new byte[200];
bool _data_is_valid;
private static GPSStateMachine gps_decoder_state;
char[] pree_buffer = new char[10];
char[] pree = { 'G', 'P', 'R', 'M', 'C' };
int pree_index = 0;
char[] sentence = new char[200];
char[] GPRMC_sentence_array = new char[200];
int GPRMC_sentence_array_length = 0;
int sentence_index = 0;
string sentence_string = "";
//
// Location Variables
//
string _date;
string _time;
public string _longitude;
public string _latitude;
string _valid;
string _longitudeDir;
string _latitudeDir;
string _speed;
string _direction;
//
// GPSStateMachine
//
private enum GPSStateMachine
{
FindPre,
CopyingSentence,
}
//
// Sub New
//
public GPS(string PortName)
{
Initialize(PortName);
}
//
// Initialize
//
public void Initialize(string portName)
{
_port = new SerialPort(portName, 57600, Parity.None, 8, StopBits.One);
_port.ReadTimeout = 0;
_port.ErrorReceived += new SerialErrorReceivedEventHandler(_port_ErrorReceived);
_port.Open();
_port.DataReceived += new SerialDataReceivedEventHandler(_port_DataReceived);
}
//
// DataReceived
//
void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int buffer_index = 0;
int data_received = _port.Read(buffer, 0, buffer.Length);
while (buffer_index < data_received)
{
switch (gps_decoder_state)
{
case GPSStateMachine.FindPre:
if (buffer[buffer_index] == pree[pree_index])
{
pree_index++;
if (pree_index >= pree.Length)
{
gps_decoder_state = GPSStateMachine.CopyingSentence;
pree_index = 0;
}
}
else
pree_index = 0;
break;
case GPSStateMachine.CopyingSentence:
sentence[sentence_index] = (char)buffer[buffer_index];
if (sentence[sentence_index] == '*' || sentence[sentence_index] == '\r' || sentence[sentence_index] == '\n')
{
if (sentence_index > 10)
{
if (sentence[12] == 'A')
{
lock (GPRMC_sentence_array)
{
Array.Copy(sentence, GPRMC_sentence_array, sentence_index);
GPRMC_sentence_array_length = sentence_index;
_data_is_valid = true;
}
}
else
{
_data_is_valid = false;
}
}
sentence_index = 0;
gps_decoder_state = GPSStateMachine.FindPre;
}
else
{
sentence_index++;
if (sentence_index >= sentence.Length)
{
sentence_index = 0;
gps_decoder_state = GPSStateMachine.FindPre;
}
}
break;
}
buffer_index++;
}
}
//
// ErrorReceived
//
void _port_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
}
//
// GetLongitude
//
public string GetLongitude()
{
return _longitude;
}
//
// GetLatitude
//
public string GetLatitude()
{
return _latitude;
}
//
// GetDirection
//
public string GetDirection()
{
return _direction;
}
//
// GetSpeed
//
public string GetSpeed()
{
return _speed;
}
//
// IsValid
//
public bool IsValid()
{
if (_valid == "A")
{
return (true);
}
else
{
return (false);
}
}
//
// ParseSentence
//
private void ParseSentence()
{
if (sentence_fields.Length == 13)
{
_date = sentence_fields[9];
_time = sentence_fields[1];
_longitude = sentence_fields[5];
_latitude = sentence_fields[3];
_valid = sentence_fields[2];
_longitudeDir = sentence_fields[6];
_latitudeDir = sentence_fields[4];
_speed = sentence_fields[7];
_direction = sentence_fields[8];
}
else
{
}
}
//
// GetPosition
//
public bool GetPosition()
{
if (_data_is_valid == true)
{
lock (GPRMC_sentence_array)
{
sentence_string = new string(GPRMC_sentence_array, 0, GPRMC_sentence_array_length);
sentence_fields = sentence_string.Split(split_char);
ParseSentence();
_data_is_valid = false;
return true;
}
}
else
{
return false;
}
}
}
}
}