Click here to Skip to main content
Licence CPOL
First Posted 15 Jun 2010
Views 22,264
Downloads 917
Bookmarked 42 times

Native DLL for GPS communication

By werner.keilholz | 22 Jul 2010
Giving full control over the communication with a GPS device in a limited environment.
1 vote, 14.3%
1

2
1 vote, 14.3%
3

4
5 votes, 71.4%
5
4.14/5 - 7 votes
μ 4.14, σa 2.75 [?]

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:

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:

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)

About the Author

werner.keilholz

Software Developer (Senior)

France France

Member
I am an IT professional working in the field of graphical user interfaces, building and system simulation software.
 
While my professional activity involves less and less actual coding, 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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMissingMethodException was unhandled PinmemberLuiG811:41 4 Oct '10  
AnswerRe: MissingMethodException was unhandled Pinmemberwerner.keilholz12:10 4 Oct '10  
GeneralRe: MissingMethodException was unhandled PinmemberLuiG81:22 5 Oct '10  
GeneralRe: MissingMethodException was unhandled Pinmemberwerner.keilholz12:47 5 Oct '10  
Generalgreat job , thanks . PinmemberMember 228177114:32 20 Sep '10  
GeneralRe: great job , thanks . Pinmemberwerner.keilholz8:32 21 Sep '10  
GeneralMy vote of 5 PinmemberDr. Jones DK10:55 29 Jun '10  
Generalgood work Pinmemberloyal ginger5:34 16 Jun '10  
GeneralRe: good work Pinmemberwerner.keilholz13:11 16 Jun '10  
GeneralMy vote of 1 PinmemberKarstenK23:29 15 Jun '10  
GeneralRe: My vote of 1 Pinmemberwerner.keilholz13:10 16 Jun '10  
GeneralI can't build it in VS 2008 Pinmemberfranva21:17 15 Jun '10  
GeneralRe: I can't build it in VS 2008 Pinmemberwerner.keilholz13:09 16 Jun '10  
GeneralRe: I can't build it in VS 2008 Pinmemberfranva23:55 16 Jun '10  
GeneralRe: I can't build it in VS 2008 PinmemberMr.Jinky21:25 20 Jun '10  
GeneralRe: I can't build it in VS 2008 Pinmemberwerner.keilholz8:11 21 Jun '10  
QuestionIs there any API Document? Pinmemberfranva20:23 15 Jun '10  
AnswerRe: Is there any API Document? Pinmemberwerner.keilholz13:08 16 Jun '10  
GeneralRe: Is there any API Document? Pinmemberfranva23:57 16 Jun '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120209.1 | Last Updated 22 Jul 2010
Article Copyright 2010 by werner.keilholz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid