Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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]);
            }
            //load web browser control page
            if (HasInternetConnection())
            {
                string map = System.IO.File.ReadAllText(Application.StartupPath + "\\VEMap.html");
              //  Error	1	'System.IO.File' does not contain a definition for 'ReadAllText'
	         //   Error	2	'System.Windows.Forms.Application' does not contain a definition for 'StartupPath'      webBrowser1.DocumentText = map;
                webBrowser1.ObjectForScripting = this;
                //Error	3	'System.Windows.Forms.WebBrowser' does not contain a definition for 'ObjectForScripting' and no extension method 'ObjectForScripting' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	

                internetConnected = true;
            }
            timer1.Interval = 1500;
        }
        private void GPSConnectBtn_Click(object sender, EventArgs e)
        {
            if (GPSConnectBtn.Text == "Connect")
            {
                GPSConnectBtn.Text = "Disconnect";
                //close serial port if it is open
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                    timer1.Enabled = false;
                }
                try
                {
                    //configure the parameters of the serial port
                    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
            {
                //close the serial port
                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");
            //Error	4	'System.Windows.Forms.TextBox' does not contain a definition for 'AppendText' and no extension method 'AppendText' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)	
        }
        private void AddPushpin(double lat, double lon, string description)
        {
            object[] param = new object[] { lat, lon, description };
           webBrowser1.Document.InvokeScript("AddPushpin", param);
            //Error	5	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)

        }
        private void SaveGPS_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
          //  saveFileDialog1.RestoreDirectory = true;
            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"))
                {
                    //its connected.
                    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);
                    //Error	6	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
                }
            }
        }
        private void openFileBtn_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            //openFileDialog1.RestoreDirectory = true;
            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");
  //    Error	7	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	

                string line = inputStream.ReadLine();
                while (!inputStream.EndOfStream)
                {
                    ProcessNMEAData(line);
                    line = inputStream.ReadLine();
                }
                webBrowser1.Document.InvokeScript("drawPath");
                //Error	8	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
                loadingFile = false;
                inputStream.Close();
            }
        }
        private void MapCurrentLocBtn_Click(object sender, EventArgs e)
        {
            double lat, lon;
           if (internetConnected && Double.TryParse(currentLatitudeTbx.Text, out lat)
               //Error	9	'double' does not contain a definition for 'TryParse'
               && Double.TryParse(currentLongitudeTbx.Text, out lon))
           //Error	10	'double' does not contain a definition for 'TryParse'	
            {
               object[] param = new object[] { lat, lon };
                webBrowser1.Document.InvokeScript("centerMap", param);
                //Error	11	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	

            }
        }
        private void drawPathBtn_Click(object sender, EventArgs e)
        {
            if (internetConnected)
            {
                webBrowser1.Document.InvokeScript("drawPath");
                //Error	12	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
            }
        }

        //Error	1	'System.IO.File' does not contain a definition for 'ReadAllText'
        //Error	2	'System.Windows.Forms.Application' does not contain a definition for 'StartupPath'      webBrowser1.DocumentText = map;
        //Error	3	'System.Windows.Forms.WebBrowser' does not contain a definition for 'ObjectForScripting' and no extension method 'ObjectForScripting' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
        //Error	4	'System.Windows.Forms.TextBox' does not contain a definition for 'AppendText' and no extension method 'AppendText' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)	
        //Error	5	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)
        //Error	6	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
        //Error	7	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
        //Error	8	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
        //Error	9	'double' does not contain a definition for 'TryParse'
        //Error	10	'double' does not contain a definition for 'TryParse'	
        //Error	11	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	
        //Error	12	'System.Windows.Forms.WebBrowser' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'System.Windows.Forms.WebBrowser' could be found (are you missing a using directive or an assembly reference?)	

    }
}
Posted
Updated 10-Feb-10 22:31pm
v12

Well, we all know that double has a TryParse() method, and I checked the MSDN site, and the WebBrowser object has a Document property (all the way back to .Net 2.0), so it looks like you're missing one or more references. Make sure you have all the necessary references added to your project, and that you're doing a clean/rebuild on the entire solution before moving it to your smart device..
 
Share this answer
 
"This code is working in Windows Forms in C# but the same code tried in Smart Device in C# is giving the errors."

OK, well, that tells you my answer is correct, surely ?

Not having double.TryParse seems a bit insane, but I don't see any other possible answer.
 
Share this answer
 
pradeep kumar patwari wrote:
does not contain a definition for


Which part of this is confusing you ?

I assume this means you're using the Compact edition of C# for an app on a device. Compact means, smaller. I guess all of these methods, which exist in C#, do not exist in the Compact edition.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900