|
// 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.
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