#include <windows.h>
#include "stdio.h"
#include "time.h"
#include "string.h"
#include "assert.h"
#include "gps.h"
#include "math.h"
DCB dcb;
COMMTIMEOUTS timeouts;
COMSTAT ComStat;
HANDLE idComDev;
unsigned int testCycle = 0;
volatile char gpsMessage[1030][2] = {0};
volatile int buffHead = 0;
volatile int buffEnd = 0;
DWORD WINAPI receiveGPSProc(LPVOID); //receive gps data
DWORD WINAPI analyseGPSProc(LPVOID); //analyse gps data
/*******************************************************************************
** Function: Get GPS Communication Port from GPSPort.ini **
** In: char buffer **
** Out: set the char buffer **
** Date: 2007-8-1 Author: Owen.Lu **
*******************************************************************************/
void getGPSPort(char backstr[])
{
FILE *fp = 0;
char one;
int ii;
int state = 0;
fp = fopen("gpsport.ini", "r");
assert(fp);
ii = 3;
one = fgetc(fp);
while(!feof(fp))
{
if(one == '\n')
{
break;
}
backstr[ii] = one;
ii++;
one = fgetc(fp);
}
backstr[ii] = '\0';
}
/*******************************************************************************
** Function: Show a MessageBox **
** In: char buffer **
** Out: NULL **
** Date: 2007-8-1 Author: Owen.Lu **
*******************************************************************************/
void showMessage(char message[])
{
MessageBox(NULL, message, "GPS V1.00", MB_OK);
}
/*******************************************************************************
** Function: GPS test Program **
** sense the GPS Port, Get the GPS Data **
** In: NULL **
** Out: NULL **
** Date: 2007-8-1 Author: Owen.Lu **
*******************************************************************************/
void setCOMPortPrj(void)
{
SetCommTimeouts(idComDev, &timeouts);
SetupComm(idComDev, 1024, 512);
dcb.DCBlength = sizeof(DCB);
GetCommState(idComDev, &dcb);
dcb.BaudRate = CBR_4800;
dcb.Parity = 0;
dcb.fParity = TRUE;
dcb.ByteSize = 8;
dcb.fBinary = TRUE;
dcb.StopBits = ONESTOPBIT;
SetCommMask(idComDev, EV_ERR|EV_RXCHAR);
SetCommState(idComDev, &dcb);
}
/*******************************************************************************
** Function: GPS Send Data **
** In: message string **
** Out: NULL **
** Date: 2007-8-1 Author: Owen.Lu **
*******************************************************************************/
int sendComData(char message[])
{
char sendData[100] = {0};
DWORD sendLength = 0;
DWORD realSend = 0;
int ii = 0;
int jj = 0;
BOOL sendState = FALSE;
PurgeComm(idComDev, PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
strcpy(sendData, message);
sendLength = strlen(sendData);
for(ii = 0; ii < (int)sendLength; ii++)
{
sendState = WriteFile( idComDev, (LPCVOID)sendData, 1, &realSend, NULL);
if(!realSend)
{
return 1;
}
for(jj = 0; jj < (int)strlen(sendData); jj++)
{
sendData[jj] = sendData[jj + 1];
}
}
return 0;
}
/*******************************************************************************
** Function: GPS Send Data **
** In: message string **
** Out: message string **
** Date: 2007-8-1 Author: Owen.Lu **
*******************************************************************************/
int getComData(char message[])
{
DWORD dwErrorFlags = 0;
DWORD dwBytesRead = 0;
char getData[100] = {0};
PurgeComm(idComDev, PURGE_TXCLEAR);
ClearCommError(idComDev, &dwErrorFlags, &ComStat);
dwBytesRead = 0;
dwBytesRead = min(8,ComStat.cbInQue);
if(!dwBytesRead)
{
return 0;
}
ReadFile(idComDev, getData, dwBytesRead, &dwBytesRead, NULL);
getData[dwBytesRead] = 0;
strcpy(message, getData);
return (int)dwBytesRead;
}
/*******************************************************************************
** Function: Get GPS Signal Information **
** ................................................ **
** Each frament start with $, there are different frament such as **
** $GPGGA, $GPGSA , $GPGSV , $GPRMC and so on **
** ................................................ **
** $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>*hh **
** <01>: [Time]: hhmmss **
** <02>: [State]: A is efficiency , V is inefficiency **
** <03>: [Latitude]: ddmm.mmmm **
** <04>: [South or North earth]: N is north, S is south **
** <05>: [Longitude]: ddmm.mmmm **
** <06>: [East or West earth]: E is east, W is west **
** <07>: [High ]: range from 0.0 to 999.9 **
** <08>: [direction]: range from 000.0 to 359.9 **
** <09>: [date]: ddmmyy **
** <10>: [Magnetic Change]: range from 000.0 to 180.0 **
** <11>: [Magnetic director change]: E or W **
** In: NULL **
** Out: NULL **
** Date: 2007-8-1 Author: Owen.Lu **
*******************************************************************************/
DWORD WINAPI receiveGPSProc(LPVOID lpparam)
{
char message[10] = {0};
int receiveLength = 0;
int ii = 0;
FILE *fp;
fp = fopen("gps.dat", "w");
while(1)
{
memset(message, 0, sizeof(message));
receiveLength = getComData(message);
if(receiveLength == 0)
{
continue;
}
for(ii = 0; ii < receiveLength; ii++)
{
gpsMessage[buffHead][0] = message[ii];
gpsMessage[buffHead][1] = 1;
buffHead = (buffHead + 1 ) % 1024;
}
fprintf(fp, "%s", message);
fflush(fp);
}
}
/*******************************************************************************
** Function: Analyse GPS Signal Information **
** $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>*hh **
** <01>: [Time]: hhmmss **
** <02>: [State]: A is efficiency , V is inefficiency **
** <03>: [Latitude]: ddmm.mmmm **
** <04>: [South or North earth]: N is north, S is south **
** <05>: [Longitude]: ddmm.mmmm **
** <06>: [East or West earth]: E is east, W is west **
** <07>: [H