Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips > Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Tutorials Basic4ppc tutorials

GPS application - Part I

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2007, 12:20 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default GPS application - Part I

This tutorial is made of two parts.
The first part explains how to use the Serial library to create a connection with a GPS device and how to use the GPS library to retrieve the data.
The second part will explain how to convert coordinates from one datum to another and how to convert between Lat/Lon format and UTM format.

First we need to add a reference to the Serial and GPS libraries.
We will use the Serial2 library which requires .Net 2.0.
You can use SerialDesktop and SerialDevice instead but they are no longer supported by new devices and therefore are less recommended.

Create a new file and save it.
Without saving our file we won't be able to add any library as their is still no project folder.

Choose Tools - Components...
Choose Add Both (as we want to add the same libraries to both the desktop and the device) and add GPS.dll.
Add Serial2 in the same way.



Each library added, exposes one or more objects types that can be added to our program.
There are two ways to add objects; at runtime with AddObject and at design time using Tools - Add Object.
Normally we will use the second option.
Add a GPS object and name it GPS1 and a Serial object and name it Serial1.



Create a new form and add a Timer, a ListBox and one MenuItem named mnuConnect.



Now for the real part...
GPS devices whether external or internal send the data via a serial port.
The port could be COM1 to COM9.
See this thread if you want to find the available ports from the registry:
http://www.basic4ppc.com/forum/code-samples-tips/314-reading-com-ports-information-registry.html
The GPS continuously sends NMEA strings.
Using a Serial object and a Timer we check each second if data is waiting in the input buffer.
If there is data we read the data and pass it to a GPS object.
Code:
Sub Timer1_Tick
    
If Serial1.InBufferCount > 0 Then
        GPS1.GPSStream(Serial1.InputString)
    
End If
End Sub
GPS1 collects the partial strings and when there is enough data it parses the data and raises a GPSDecoded event.
Inside the GPSDecoded event we can read the GPS data (lstData is a ListBox):
Code:
Sub GPS1_GPSDecoded
    lstData.Clear
    lstData.Add(
"Status: " & GPS1.Status)
    lstData.Add(
"Number of satellites: " & GPS1.NumberOfSatellites)
    lstData.Add(
"Time: " & GPS1.UTCTime)
    lstdata.Add(
"Lat: " & GPS1.Latitude)
    lstdata.Add(
"Lon: " & GPS1.Longitude)
    
If gps1.SpeedOverGround <> "" Then lstdata.Add("Speed: " & (GPS1.SpeedOverGround * 1.852))
    lstdata.Add(
"Course: " & GPS1.CourseOverGround)
End Sub


To make our application less error prone we've added a global variable named counter.
The GPS continuously sends data even when there is no satellites reception.
On each tick event (each second), if there is no data we increase counter by one.
If counter equals 5 (which means that no data was sent in the last five seconds), we assume that the connection is broken, we close the serial port and ask the user if he wants to reconnect.
Code:
Sub Timer1_Tick
    
If Serial1.InBufferCount > 0 Then
        Label1.Text = 
"GPS is connected."
        GPS1.GPSStream(Serial1.InputString)
        counter = 
0
    
Else
        counter = counter + 
1
        
If counter >= 5 Then 'After 5 seconds without any data we will try to make a new connection.
            counter = 0
            Timer1.Enabled = 
false
            Serial1.PortOpen = 
false
            Label1.Text = 
"No connection."
            GPS1.GPSBuffer = 
"" 'Clear the yet unparsed data.
            If Msgbox("GPS was disconnected." & crlf & "Do you want to reconnect?","",cMsgboxYesNo) = cYes Then
                mnuConnect_Click 
'Try to reconnect.
            End If            
        
End If
    
End If
End Sub


Some notes:
* Most of the GPS object properties are directly parsed from the NMEA strings.
If there is no reception properties like SpeedOverGround, Latitude / Longitude and CourseOverGround will not be zero but rather be an empty string.
Which means that you need to check that the data is not empty before using it in a calculation:
Code:
If gps1.SpeedOverGround <> "" Then lstdata.Add("Speed: " & (GPS1.SpeedOverGround * 1.852)) 'Convert to KMH.
* The attached code uses port 8. You should change the global variable 'port' to match the right port.
* When the user closes Form1 we check if the serial port is still open and close it if necessary.
* You will need to install .Net 2.0 to run the attached code (or remove Serial2 library and add SerialDesktop and SerialDevice).
Attached Files
File Type: zip GPS.zip (9.8 KB, 867 views)
Reply With Quote
  #2 (permalink)  
Old 01-16-2008, 06:22 AM
Newbie
 
Join Date: Jan 2008
Posts: 1
Default

Hi,

This is a great tutorial.
Just a small question: Is it possible to locate another gps object and then using that object as the end point, is it possible to travel towards it?
Reply With Quote
  #3 (permalink)  
Old 01-16-2008, 06:47 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Do you want to calculate the distance and bearing to a specific coordinate?
If yes then you should see the code in GPS4PPC which supports it:
http://www.basic4ppc.com/forum/open-source-projects/400-gps4ppc.html
Reply With Quote
  #4 (permalink)  
Old 02-13-2008, 06:15 AM
Basic4ppc Veteran
 
Join Date: Feb 2008
Posts: 200
Default

What PDAs can work well with GPS.dll ?

For ex., this sample works OK on Eten Glofiish device, but does not work on HTC device: the COM-port that's working OK with a navigation software over NMEA protocol, but if try GPS-Demo with the same port - the port can be opened, but next there is the "OutOfMemoryException" error in "sub_timer1_tick".

Seems the buffer is full of data, but cannot be decoded as NMEA.

But all these devices have the same modernest GPS chipset SirfStar III.
Reply With Quote
  #5 (permalink)  
Old 02-13-2008, 07:39 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The HTC 3300 has a problem with .Net CF and the SerialPort class (which Serial2 is based upon).
See this thread: http://www.basic4ppc.com/forum/showt...emoryexception
I don't know of any other device which doesn't support the Serial2 library.
Reply With Quote
  #6 (permalink)  
Old 05-13-2009, 02:53 PM
Newbie
 
Join Date: May 2009
Posts: 1
Default

i am trying to run the dlls in my Windows Mobile 5.0 Pocket PC application using .net framework 3.5
i've used them in my framework 1.0 application and they worked fine.They worked great actually
my application is running
when i tried them on the new framework i got an error
on Decoder.GPSStream(Buffer)
the application throws an expection.By viewing the details i get those
ExceptionCode: 0xc0000005
ExceptionAddress:0xxxe83d70
Reading:0x00000004
Faulting module: mscoree3_5.dll
offset: 0x00063d70

at SerialPort.ReadFile(int32 hFile,Byte[] Budder,Int32 mNumberOfBytesToRead..
at SerialPort.REadThreadProc()

i am using an Intermec CN3

any ideas?
Reply With Quote
  #7 (permalink)  
Old 05-13-2009, 05:30 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should replace SerialDesktop.dll and SerialDevice.dll with Serial2.dll.
__________________
Basic4android documentation
Reply With Quote
  #8 (permalink)  
Old 09-04-2009, 06:10 AM
Newbie
 
Join Date: Aug 2009
Posts: 3
Default ABOUT THE gps PORT OPEN ERROR

WHEN I RUN THE GPS app(part I),can not open the gps port ,error messaege "open the gps error". i don't what's wrong.
Reply With Quote
  #9 (permalink)  
Old 09-04-2009, 07:13 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I've attached GPSDriverDemo which uses Microsoft GPS Intermediate driver instead of the serial port.
This library requires WM5 or newer device.
Please see if it solves this problem.
Attached Files
File Type: zip GPSDriverDemo.zip (8.8 KB, 173 views)
__________________
Basic4android documentation
Reply With Quote
  #10 (permalink)  
Old 11-10-2009, 02:43 AM
epo epo is offline
Newbie
 
Join Date: Nov 2009
Posts: 4
Default Using serial port not bluetooth

Pls disregard



Hi!

I have a survey grade gps receiver with serial output. I want to use an ipaq h2415 to capture the streaming nmea sentences coming out of it. To do that, I have two options, use a socket compact flash serial adapter (SocketSerial(TM)) or the ipaq serial cable. As the GPS basic4ppc program you describe in this tutorial access the virtual serial port provided by bluetooth, is there a way to modify the program so it will use the hardware serial rather than the virtual one?

Programming ability is very limited and would appreciate all the help.

Thanks.

Last edited by epo : 11-10-2009 at 07:38 PM. Reason: got resolved
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
My first application Erel Tutorials 16 04-18-2010 09:09 PM
GPS application - Part II Erel Tutorials 8 03-19-2009 09:09 AM
Application Name kawawong Questions (Windows Mobile) 1 02-12-2008 10:38 AM
Getting value from other other application in runtime Rioven Questions (Windows Mobile) 19 09-20-2007 10:13 AM
DB Application scott93727 Share Your Creations 0 05-02-2007 06:24 AM


All times are GMT. The time now is 09:53 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0