Click here to Skip to main content
15,878,814 members
Articles / Mobile Apps / Windows Mobile

Native DLL for GPS communication

Rate me:
Please Sign up or sign in to vote.
4.25/5 (8 votes)
22 Jul 2010CPOL2 min read 63.1K   3.2K   45   21
Giving full control over the communication with a GPS device in a limited environment.

Introduction

The .NET Framework provides handy functions for communication with ports, such as the COM port required to read GPS data. This has been described in other articles on CodeProject.

Although these functions are also available in the Compact Framework, listening to GPS data by simply opening a stream using the correct COM port does not work, on many devices, due to limitations in the Compact Framework installed on the device.

This article describes a DLL written in C++ giving full control over the communication with the COM port. It exports functions to open, configure, and close the port, as well as read information form the port character by character.

Background

When developing my Maplorer application (www.maplorer.com), I found that communication would only work from native applications, and only for a buffer size of one, single character. In principle, reading GPS data is fairly simple, as the GPS device constantly sends all readings to a (device-specific) COM port as a never-ending string. All you have to do is listen to that port and parse the string using the NMEA syntax. As .NET functionality was not available on my device, I developed a DLL to provide it.

Using the Code

The functions contained in the DLL can be used very easily in any .NET application via the DllImport statement; for example, to use the function in any of your classes, just add:

C#
class MyClass 
{
    [DllImport("GpsDll.dll")]
    public static extern int InitGpsCom(int Port, int BaudRate, 
                  int ByteSize, int StopBits, int Parity);

    [DllImport("GpsDll.dll")]
    public static extern char ReadNextChar();

    [DllImport("GpsDll.dll")]
    public static extern int CloseGpsCom();
    ...
}

The three functions listed are used to open communications (InitGpsCom), read the next character (ReadNextChar), and free the port.

The ReadNextChar function can be easily used to read NMEA sentences, knowing that each new sentence starts with a '$' sign; for example, in C#, you would write something like this:

C#
public String ReadNextSentence()
{
    String result = "";
    char c;

    while ((c = ReadNextChar()) != '$')
        result += c;
    
    result = "$" + result;

    return result;
}

GpsDllSample.zip provides a complete example for using the DLL in C#, including an NMEA class to parse NMEA sentences and determine latitude, longitude, bearing, etc.

Points of Interest

This project mainly shows you that you can always gain control of a device on your own: if functionality is not available in your programming environment, you can usually find it one level deeper!

The drawback of this solution is the fact that it limits your applications to the CPU architecture the DLL is compiled for - you now run native code, not only .NET byte code.

History

  • Version 1.0: February 2010.

License

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


Written By
Software Developer (Senior)
France France
I am an IT professional working in the field of graphical user interfaces, building and system simulation software.

My passion are portable devices, from robots to cell phones and GPS systems, which I enjoy using, customizing and programming in my free time.

I currently write a free moving-map application for GPS devices: http://maplorer.com

Comments and Discussions

 
QuestionGreat job! Pin
Helios_vmg2-Aug-12 18:38
Helios_vmg2-Aug-12 18:38 
Excellent, this is exactly what I was looking for! Every other GPS code sample I could find uses .NET to connect to the GPS, but my device doesn't support .NET. Yours is the only sample I could find that uses purely native interfaces.
Thanks very much!
QuestionMissingMethodException was unhandled Pin
LuiG84-Oct-10 10:41
LuiG84-Oct-10 10:41 
AnswerRe: MissingMethodException was unhandled Pin
werner.keilholz4-Oct-10 11:10
werner.keilholz4-Oct-10 11:10 
GeneralRe: MissingMethodException was unhandled Pin
LuiG85-Oct-10 0:22
LuiG85-Oct-10 0:22 
GeneralRe: MissingMethodException was unhandled Pin
werner.keilholz5-Oct-10 11:47
werner.keilholz5-Oct-10 11:47 
Generalgreat job , thanks . Pin
Member 228177120-Sep-10 13:32
Member 228177120-Sep-10 13:32 
GeneralRe: great job , thanks . Pin
werner.keilholz21-Sep-10 7:32
werner.keilholz21-Sep-10 7:32 
GeneralMy vote of 5 Pin
Dr. Jones DK29-Jun-10 9:55
professionalDr. Jones DK29-Jun-10 9:55 
Generalgood work Pin
loyal ginger16-Jun-10 4:34
loyal ginger16-Jun-10 4:34 
GeneralRe: good work Pin
werner.keilholz16-Jun-10 12:11
werner.keilholz16-Jun-10 12:11 
GeneralMy vote of 1 Pin
KarstenK15-Jun-10 22:29
mveKarstenK15-Jun-10 22:29 
GeneralRe: My vote of 1 Pin
werner.keilholz16-Jun-10 12:10
werner.keilholz16-Jun-10 12:10 
GeneralI can't build it in VS 2008 Pin
franva15-Jun-10 20:17
franva15-Jun-10 20:17 
GeneralRe: I can't build it in VS 2008 Pin
werner.keilholz16-Jun-10 12:09
werner.keilholz16-Jun-10 12:09 
GeneralRe: I can't build it in VS 2008 Pin
franva16-Jun-10 22:55
franva16-Jun-10 22:55 
GeneralRe: I can't build it in VS 2008 Pin
Mr.Jinky20-Jun-10 20:25
Mr.Jinky20-Jun-10 20:25 
GeneralRe: I can't build it in VS 2008 Pin
werner.keilholz21-Jun-10 7:11
werner.keilholz21-Jun-10 7:11 
QuestionIs there any API Document? Pin
franva15-Jun-10 19:23
franva15-Jun-10 19:23 
AnswerRe: Is there any API Document? Pin
werner.keilholz16-Jun-10 12:08
werner.keilholz16-Jun-10 12:08 
GeneralRe: Is there any API Document? Pin
franva16-Jun-10 22:57
franva16-Jun-10 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.