Hi friends,
I write code for Reading GPS Data from Serial Port in smart device using C# but getting some errors (also in the comment //)
Note 1: This code is working in Windows Forms in C# but the same code tried in Smart Device in C# is giving the errors.
Note 2: My references are:
mscorlib
,
System
,
System.Core
,
System.Data
,
System.Data.DataExtensions
,
System.Drawing
,
System.Windows.Forms
,
System.Web.Service
,
System.Xml
,
System.Xml.Linq
Note 3: I am using Microsoft Visual Studio 2008 (.net) in that Smart Device-> usa windows mobile 5.0 Packet PC R2 Emulator
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Net;
namespace SerialGPS
{
[System.Runtime.InteropServices.ComVisible(true)]
public partial class Form1 : Form
{
SerialPort serialPort = new SerialPort();
public delegate void myDelegate();
private string outputFile;
private string inputFile;
private bool internetConnected = false;
bool loadingFile = false;
StreamWriter sw;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] portNames = System.IO.Ports.SerialPort.GetPortNames();
for (int i = 0; i < portNames.Length; i++)
{
portNumberBox.Items.Add(portNames[i]);
}
if (HasInternetConnection())
{
string map = System.IO.File.ReadAllText(Application.StartupPath + "\\VEMap.html");
webBrowser1.ObjectForScripting = this;
internetConnected = true;
}
timer1.Interval = 1500;
}
private void GPSConnectBtn_Click(object sender, EventArgs e)
{
if (GPSConnectBtn.Text == "Connect")
{
GPSConnectBtn.Text = "Disconnect";
if (serialPort.IsOpen)
{
serialPort.Close();
timer1.Enabled = false;
}
try
{
serialPort.PortName = portNumberBox.Text;
serialPort.BaudRate = 9600;
serialPort.Parity = System.IO.Ports.Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = System.IO.Ports.StopBits.One;
serialPort.Open();
timer1.Enabled = true;
statusTxt.Text = "GPS on port " + portNumberBox.Text + " connected.";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
GPSConnectBtn.Text = "Connect";
statusTxt.Text = "GPS on port " + portNumberBox.Text + " disconnected.";
serialPort.Close();
timer1.Enabled = false;
}
}
public void UpdateGPSData()
{
try
{
if (serialPort.IsOpen)
{
string data = serialPort.ReadExisting();
if (!String.IsNullOrEmpty(data))
{
GPSData.Text = data + "\r\n";
GPSData.ScrollToCaret();
ProcessNMEAData(data);
if (!String.IsNullOrEmpty(outputFile))
{
sw.WriteLine(data);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void UpdateMapData(string msg)
{
mapData.AppendText(msg + "\r\n");
}
private void AddPushpin(double lat, double lon, string description)
{
object[] param = new object[] { lat, lon, description };
webBrowser1.Document.InvokeScript("AddPushpin", param);
}
private void SaveGPS_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
outputFile = saveFileDialog1.FileName;
sw = File.AppendText(outputFile);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateGPSData();
}
private bool HasInternetConnection()
{
HttpWebRequest req;
HttpWebResponse resp;
try
{
req = (HttpWebRequest)WebRequest.Create("http://googlemaps.com");
resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode.ToString().Equals("OK"))
{
return true;
}
}
catch (Exception)
{
UpdateMapData("No internet connection");
}
return false;
}
private void ProcessNMEAData(string data)
{
string[] NMEALine = data.Split('$');
string[] NMEAType;
for (int i = 0; i < NMEALine.Length; i++)
{
NMEAType = NMEALine[i].Split(',');
switch (NMEAType[0])
{
case "GPGGA":
ProcessGPGGA(NMEAType);
break;
case "GPGLL":
break;
case "GPGSA":
break;
case "GPGSV":
break;
case "GPRMC":
break;
case "GPVTG":
break;
default:
break;
}
}
}
private void ProcessGPGGA(string[] data)
{
double lat, lon;
double rawLatLong;
rawLatLong = double.Parse(data[2].Replace(":00", ""));
lat = ((int)(rawLatLong / 100)) + ((rawLatLong - (((int)(rawLatLong / 100)) * 100)) / 60);
if (data[3] == "S")
lat *= -1;
rawLatLong = double.Parse(data[4].Replace(":00", ""));
lon = ((int)(rawLatLong / 100)) + ((rawLatLong - (((int)(rawLatLong / 100)) * 100)) / 60);
if (data[5] == "W")
lon *= -1;
currentLatitudeTbx.Text = lat.ToString();
currentLongitudeTbx.Text = lon.ToString();
if (internetConnected)
{
if (!loadingFile && FollowCbx.Checked)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<div>Latitude: {0}<br/>Longitude: {1}<br/>Altitude: {2} {3}</div>", lat, lon, data[9], data[10]);
AddPushpin(lat, lon, sb.ToString());
}
else if (loadingFile)
{
object[] param = new object[] { lat, lon };
webBrowser1.Document.InvokeScript("AddPoint", param);
}
}
}
private void openFileBtn_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
inputFile = openFileDialog1.FileName;
openFileTbx.Text = inputFile;
LoadTrip();
}
}
private void LoadTrip()
{
if (!string.IsNullOrEmpty(inputFile))
{
StreamReader inputStream = new StreamReader(File.Open(inputFile, FileMode.Open));
loadingFile = true;
webBrowser1.Document.InvokeScript("ClearPoints");
string line = inputStream.ReadLine();
while (!inputStream.EndOfStream)
{
ProcessNMEAData(line);
line = inputStream.ReadLine();
}
webBrowser1.Document.InvokeScript("drawPath");
loadingFile = false;
inputStream.Close();
}
}
private void MapCurrentLocBtn_Click(object sender, EventArgs e)
{
double lat, lon;
if (internetConnected && Double.TryParse(currentLatitudeTbx.Text, out lat)
&& Double.TryParse(currentLongitudeTbx.Text, out lon))
{
object[] param = new object[] { lat, lon };
webBrowser1.Document.InvokeScript("centerMap", param);
}
}
private void drawPathBtn_Click(object sender, EventArgs e)
{
if (internetConnected)
{
webBrowser1.Document.InvokeScript("drawPath");
}
}
}
}