Click here to Skip to main content
15,891,933 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.3K   3.2K   45  
Giving full control over the communication with a GPS device in a limited environment.
// GpsDll.cpp 
//

#include "GpsDll.h"
#include "windows.h"
#include <stdio.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

static HANDLE m_hComm;


__declspec( dllexport ) 
int HelloDll() // just for testing
{
	return 42;
}

__declspec( dllexport ) 
int InitGpsCom(int Port, int BaudRate, int ByteSize, int StopBits, int Parity)
{
    TCHAR szPort[8];     
	HANDLE hComm;
	DCB dcb;
    DWORD flag;  
    COMMTIMEOUTS CommTimeOuts;

	wsprintf(szPort,_T("COM%d:"),Port); 
	hComm = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, 
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

    if ( INVALID_HANDLE_VALUE==hComm )
        return FALSE;

    GetCommState( hComm, &dcb );
    dcb.BaudRate = (DWORD)BaudRate;
    dcb.ByteSize = (BYTE)ByteSize;
    dcb.StopBits = (BYTE)StopBits;
    dcb.fParity=!(Parity ==  0);

    dcb.Parity =  (BYTE)Parity;
    if ( !SetCommState(hComm, &dcb  ) )

    {    
        CloseHandle( hComm ); 

		ASSERT( FALSE); 
        return FALSE;        
	} 

            
    flag = EV_RXCHAR;
    SetCommMask( hComm,  flag|EV_BREAK  );
	
    CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
    CommTimeOuts.ReadTotalTimeoutMultiplier=  0 ;
    CommTimeOuts.ReadTotalTimeoutConstant = 0  ;
    CommTimeOuts.WriteTotalTimeoutMultiplier =  10;
    CommTimeOuts.WriteTotalTimeoutConstant = 0 ;
    if(SetCommTimeouts(  hComm, &CommTimeOuts ) ==  0)
    {
        return FALSE; 
    }
    m_hComm = hComm;
    return TRUE;
}

__declspec( dllexport ) 
int PhysicalReceive(char* pReceive) 
{ 
    DWORD dwError; 
    COMSTAT cs; 
    ULONG nByteRead=0; 
    
    int errorcode; 
	int nLen =0;
	BOOL bReadSuc;
	errorcode = ClearCommError( m_hComm, &dwError, &cs ); 
	bReadSuc= ReadFile(m_hComm, pReceive, 1, &nByteRead, NULL); // max 1 char in buffer !
	if ( bReadSuc &&nByteRead>0 )
	{
		nLen = (int) nByteRead;
		return nLen;
	}
    errorcode=GetLastError();
    return -1;
}

__declspec( dllexport ) 
char ReadNextChar()
{
	char OneChar[2];
	while (PhysicalReceive((char*)&OneChar)==-1) {Sleep(200);}

	return OneChar[0];
}
__declspec( dllexport ) 
int CloseGpsCom()
{
	CloseHandle( m_hComm ); 

	return 1;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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