📄 gpswin32.cpp.bak
字号:
HRESULT LoadThread()
{
HRESULT hr = E_FAIL;
DWORD dwRet = 0;
s_hExitThread = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!s_hExitThread)
{
dwRet = GetLastError();
hr = HRESULT_FROM_WIN32(dwRet);
goto Exit;
}
m_hThread = CreateThread(NULL, NULL, GPSThreadProc, NULL, NULL, &m_dwThreadID);
if (!m_hThread)
{
dwRet = GetLastError();
hr = HRESULT_FROM_WIN32(dwRet);
goto Exit;
}
hr = S_OK;
Exit:
if (FAILED(hr))
{
if (s_hExitThread)
{
CloseHandle(s_hExitThread);
s_hExitThread = NULL;
}
// tmphThread is NULL & the thread was not created
}
return hr;
}
DWORD WINAPI GPSThreadProc(__opt LPVOID lpParameter)
{
DWORD dwRet = 0;
HANDLE gpsHandles[GPS_CONTROLLER_EVENT_COUNT] = {
s_hExitThread
};
GPS_Located = 0;
// add gps driver..................................
HANDLE hReadcom3Thr = NULL;
HANDLE hWritecom3Thr = NULL;
InitportCOM3();
WriteDataSerialPort();
//hWritecom3Thr = CreateThread(NULL, 0, RequestCom3Thread, NULL, 0, NULL);
// if(!hWritecom3Thr)
//{
// RETAILMSG(BAT_MSG, (L"WriteCom3Thread fail!!!\r\n"));
//}
hReadcom3Thr = CreateThread(NULL, 0, ReadCom3Thread, NULL, 0, NULL);
// RETAILMSG(BAT_MSG, (L"ReadCom3Thread hReadcom3Thr=%x\r\n"),hReadcom3Thr);
if(!hReadcom3Thr)
{
// RETAILMSG(BAT_MSG, (L"ReadCom3Thread fail!!!\r\n"));
}
// add gps driver..................................
//ShowData();
//GPS_Located = 1;
do
{
dwRet = WaitForMultipleObjects(GPS_CONTROLLER_EVENT_COUNT,
gpsHandles,
FALSE,
INFINITE);
if (dwRet == WAIT_OBJECT_0)
{
break;
}
else
{
ASSERT(0);
}
} while (1);
return 0;
}
BOOL InitportCOM3()
{
DCB dcbcom;
COMMTIMEOUTS com3TimeOuts;
RETAILMSG(1, (TEXT("InitportCOM3: InitportCOM3\r\n")));
g_PWR_serialCOMHandle = CreateFile(L"COM3:",GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0);
if (g_PWR_serialCOMHandle == INVALID_HANDLE_VALUE)
{
RETAILMSG(1, (TEXT("InitportCOM3: open COM3 error code=%x\r\n"),GetLastError()));
return 0;
}
if (!GetCommState(g_PWR_serialCOMHandle, &dcbcom))
{
RETAILMSG(1, (TEXT("InitportCOM3: GetCommState error%x\r\n"),GetLastError()));
return 0;
}
//DCB PortDCB;
// Initialize the DCBlength member.
dcbcom.DCBlength = sizeof (DCB);
// Change the DCB structure settings.
dcbcom.BaudRate = 9600; // Current baud
dcbcom.fBinary = TRUE; // Binary mode; no EOF check
dcbcom.fParity = TRUE; // Enable parity checking
dcbcom.fOutxCtsFlow = FALSE; // No CTS output flow control
dcbcom.fOutxDsrFlow = FALSE; // No DSR output flow control
dcbcom.fDtrControl = DTR_CONTROL_DISABLE;
// DTR flow control type
dcbcom.fDsrSensitivity = FALSE; // DSR sensitivity
dcbcom.fTXContinueOnXoff = FALSE; // XOFF continues Tx
dcbcom.fOutX = FALSE; // No XON/XOFF out flow control
dcbcom.fInX = FALSE; // No XON/XOFF in flow control
dcbcom.fErrorChar = FALSE; // Disable error replacement
dcbcom.fNull = FALSE; // Disable null stripping
dcbcom.fRtsControl = RTS_CONTROL_DISABLE;
// RTS flow control
dcbcom.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
dcbcom.ByteSize = 8; // Number of bits/byte, 4-8
dcbcom.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
dcbcom.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (g_PWR_serialCOMHandle, &dcbcom))
{
// Could not configure the serial port.
DWORD dwError = GetLastError ();
MessageBox (g_hWnd, TEXT("Unable to configure the serial port"),
TEXT("Error"), MB_OK);
return FALSE;
}
com3TimeOuts.ReadIntervalTimeout = MAXDWORD;//0xFFFFFFFF;//maxDOWRD//
com3TimeOuts.ReadTotalTimeoutMultiplier = 0;
com3TimeOuts.ReadTotalTimeoutConstant = 0;
com3TimeOuts.WriteTotalTimeoutMultiplier = 1;
com3TimeOuts.WriteTotalTimeoutConstant = INFINITE;
if (!SetCommTimeouts (g_PWR_serialCOMHandle, &com3TimeOuts))
{
RETAILMSG(1, (TEXT("InitportCOM3: SetCommTimeouts error%x\r\n"),GetLastError()));
return 0;
}
if(! SetCommMask(g_PWR_serialCOMHandle, GPS_COM_MASK))
{
RETAILMSG(1, (TEXT("InitportCOM3: SetCommMask error%x\r\n"),GetLastError()));
return 0;
}
// RETAILMSG(BATTMSG, (TEXT("Initcom3 finished\r\n")));
return 1;
}
BOOL WriteDataSerialPort()
{
int i;
unsigned char GPSDataFormat[6][16] = {
{0x00,0x00, 0xB5, 0x62, 0x06 ,0x01, 0x06, 0x00 ,0xF0 ,0x04 ,0x00 ,0x00 ,0x00 ,0x00 ,0x01 ,0x29}, //GPRMC
{0x00,0x00, 0xB5 ,0x62 ,0x06 ,0x01 ,0x06 ,0x00 ,0xF0 ,0x05 ,0x00 ,0x00 ,0x00 ,0x00 ,0x02 ,0x2E}, //GPVTG
{0x00,0x00, 0xB5 ,0x62 ,0x06 ,0x01 ,0x06 ,0x00 ,0xF0 ,0x02 ,0x00 ,0x00 ,0x00 ,0x00 ,0xFF ,0x1F}, //GPGSA
{0x00,0x00, 0xB5 ,0x62 ,0x06 ,0x01 ,0x06 ,0x00 ,0xF0 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x24}, //GPGSV
{0x00,0x00 ,0xB5 ,0x62 ,0x06 ,0x01 ,0x06 ,0x00 ,0xF0 ,0x01 ,0x00 ,0x00 ,0x00 ,0x00 ,0xFE ,0x1A}, //GPGLL
{0x00,0x00 ,0xB5 ,0x62 ,0x06 ,0x01 ,0x06 ,0x00 ,0xF0 ,0x08 ,0x00 ,0x00 ,0x00 ,0x00 ,0x05 ,0x3D}, //GPZDA
//{0x00,0x00 ,0xB5 ,0x62 ,0x06 ,0x01 ,0x06 ,0x00 ,0xF0 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0xFD ,0x15},
};
DWORD dwByteofWrited;
for(i = 0;i <6;i ++)
{
RETAILMSG(1, (TEXT("WriteDataSerialPort: WriteDataSerialPort\r\n")));
PurgeComm(g_PWR_serialCOMHandle, PURGE_RXCLEAR);
if (!WriteFile(g_PWR_serialCOMHandle, &(GPSDataFormat[i]), 16, &dwByteofWrited, NULL))
{
// RETAILMSG(1, (TEXT("WriteDataSerialPort: WriteFile error%x\r\n"),GetLastError()));
return (FALSE);
}
Sleep(500);
}
RETAILMSG(1, (TEXT("WriteDataSerialPort: WriteDataSerialPort finished\r\n")));
}
/*
DWORD RequestCom3Thread(LPVOID pParam)
{
CeSetThreadPriority(GetCurrentThread(), 235);
//WriteDataSerialPort();
#if 0
while(1)
{
WriteRequestCommandzda();
Sleep(5000);
}
#endif
while(1)
{
WriteRequestCommand();
Sleep(5000);
}
}
*/
DWORD ReadCom3Thread(LPVOID pParam)
{
DWORD dwMask;
CRITICAL_SECTION GPSCritical;
//CeSetThreadPriority(GetCurrentThread(), 240);
RETAILMSG(1, (L"ReadCom3Thread!!!\r\n"));
PurgeComm(g_PWR_serialCOMHandle, PURGE_TXCLEAR);//||PURGE_RXCLEAR
while(1)
{
if (WaitCommEvent(g_PWR_serialCOMHandle, &dwMask, NULL))
{
SetCommMask(g_PWR_serialCOMHandle, EV_RXCHAR);
if (dwMask & EV_RXCHAR)
{
EnterCriticalSection(&GPSCritical);
Readcom3date();
LeaveCriticalSection(&GPSCritical);
continue;
}
}
}
}
double Latitude(unsigned char GPGGA_ueseddata[],int GPGGA_ueseddataSize)
{
int inum,dotleft_flagbit = 0,dotright_flagbit = 0,dotflag = FALSE,mNum = 0;
double LatitudeNum= 0.0, fraction = 0.0,floatnum = 0.0,floatnum1 = 0.0;
int i,m;
int number[11] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2e};
// RETAILMSG(BAT_MSG, (TEXT("Readcom3date: Latitude enter \r\n")));
for(i = 0;i < GPGGA_ueseddataSize;i ++)
{
if(GPGGA_ueseddata[i] != 0x2e)
{
for(m = 0; m<11; m++)
{
if(GPGGA_ueseddata[i] == number[m])
{
GPGGA_ueseddata[i] = m;
}
}
}
else continue;
// RETAILMSG(BAT_MSG, (TEXT("GPGGA_ueseddata[%d]=%d \r\n"),i,GPGGA_ueseddata[i]));
}
for(inum = 0; inum < GPGGA_ueseddataSize; inum ++ )
{
if(GPGGA_ueseddata[inum] == 0x2e)
{
dotleft_flagbit = inum;
dotright_flagbit = inum + 1;
dotflag = TRUE;
// RETAILMSG(BAT_MSG, (TEXT("dotleft_flagbit=%d,dotright_flagbit=%d \r\n"),dotleft_flagbit,dotright_flagbit));
break;
}
else
{
continue;
}
}
// if(!dotflag)
// {
// RETAILMSG(BAT_MSG, (TEXT("return \r\n")));
// return;
// }
for(i = 0; i < dotleft_flagbit; i ++)
{
// RETAILMSG(BAT_MSG, (TEXT("Latitude:GPGGA_ueseddata[%d]==%d\r\n"),i,GPGGA_ueseddata[i]));
floatnum1=GPGGA_ueseddata[i] * pow(10,(dotleft_flagbit - i-1));
printf("Latitude:printf: floatnum1=%lf\r\n",floatnum1);
floatnum =floatnum + floatnum1;
printf("Latitude:printf: floatnum=%lf\r\n",floatnum);
}
mNum = 0;
for(i = dotright_flagbit; i < GPGGA_ueseddataSize; i ++)
{
mNum ++;
// RETAILMSG(BAT_MSG, (TEXT("Latitude: mNum= %d\r\n"),-mNum));
// RETAILMSG(BAT_MSG, (TEXT("Latitude:GPGGA_ueseddata[%d]==%d\r\n"),i,GPGGA_ueseddata[i]));
fraction += GPGGA_ueseddata[i] * pow(10,(-mNum));
printf("Latitude:printf: fraction=%lf\r\n",fraction);
}
LatitudeNum = floatnum + fraction;
printf("Latitude:printf: LatitudeNum=%lf\r\n",LatitudeNum);
return (LatitudeNum);
}
BOOL WINAPI Readcom3date()
{
int com3buffer_i;
GPSGGA p;
GGA GPSdata;
DWORD commEvent, bytesRead;
unsigned char *GPGGA_Data,ueseddata[20];
DWORD iNO=0,j = 0, comma_startflag = 0,comma_endflag = 0,dotflag = 0;
int commaNO = 0,usebufferSize = 0,comma_num[14]= {0};
BOOL fRet = TRUE;
HANDLE hFile;
DWORD written = 0;
int number[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
//char *FileBuffer = (char *)LocalAlloc( LPTR, 100);
if (!ReadFile(g_PWR_serialCOMHandle, Com3Buffer+secondreadsize, GPGGA_DataSize , &bytesRead, NULL))
{
fRet = FALSE;
goto Exit;
}
//Showtext();
secondreadsize+= bytesRead;
/*
hFile = CreateFile(L"\\GPSTEXT.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,NULL,NULL);
SetFilePointer(hFile,0,0,FILE_END);
WriteFile(hFile, L"\r\n",4,&written,NULL);
WriteFile(hFile,Com3Buffer,secondreadsize,&written,NULL);
WriteFile(hFile, L"\r\n",4,&written,NULL);
CloseHandle(hFile);
*/
if(((Com3Buffer[0] == 0x24)&&(Com3Buffer[1] == 0x47)&&(Com3Buffer[2] == 0x50)
&&(Com3Buffer[3] == 0x47)&&(Com3Buffer[4] == 0x47 )&&(Com3Buffer[5] == 0x41)
&&(Com3Buffer[6] == 0x2c))&&((Com3Buffer[secondreadsize-1] != 0xa)&&(Com3Buffer[secondreadsize-1] != 0xd)))
{
times++;
if(times>2)
{
times = 0;
}
else
{
return FALSE;
}
}
bytesRead = secondreadsize;
secondreadsize = 0;
times = 0;
if(bytesRead >30)
{
if((Com3Buffer[0] == 0x24)&&(Com3Buffer[1] == 0x47)&&(Com3Buffer[2] == 0x50)
&&(Com3Buffer[3] == 0x47)&&(Com3Buffer[4] == 0x47 )&&(Com3Buffer[5] == 0x41)
&&(Com3Buffer[6] == 0x2c))
{
/*
hFile = CreateFile(L"\\GPSTEXT2.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,NULL,NULL);
SetFilePointer(hFile,0,0,FILE_END);
WriteFile(hFile, L"\r\n",4,&written,NULL);
WriteFile(hFile,Com3Buffer,bytesRead,&written,NULL);
WriteFile(hFile, L"\r\n",4,&written,NULL);
WriteFile(hFile, L"\r\n",4,&written,NULL);
CloseHandle(hFile);
*/
for(iNO = 0; iNO< bytesRead; iNO++)
{
if(Com3Buffer[iNO] == 0x2c)
{
++commaNO;
if(commaNO>13) break;
comma_num[commaNO] = iNO;
}
else
{
continue;
}
}
if(comma_num[7]-comma_num[6]>1)
{
p.GPSFS=Com3Buffer[comma_num[6]+1];
}
if(comma_num[8]-comma_num[7]==2)
{
p.GPSNum[0] = Com3Buffer[comma_num[7]+1];
for(int m = 0; m<10; m++)
{
if(p.GPSNum[0] == number[m])
{
GPSNums = m;
break;
}
}
}
else if (comma_num[8]-comma_num[7]==3)
{
p.GPSNum[0] = Com3Buffer[comma_num[7]+1];
p.GPSNum[1] = Com3Buffer[comma_num[7]+2];
for(int m = 0; m<10; m++)
{
if(p.GPSNum[0] == number[m])
{
GPSNums = m;
}
if(p.GPSNum[1] == number[m])
{
GPSNum2 = m;
}
}
GPSNums = GPSNums*10 + GPSNum2;
}
/*
hFile = CreateFile(L"\\GPSTEXT2.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,NULL,NULL);
SetFilePointer(hFile,0,0,FILE_END);
WriteFile(hFile, L"\r\n",4,&written,NULL);
sprintf( FileBuffer, "%d", GPSNums);
WriteFile(hFile,FileBuffer,2,&written,NULL);
WriteFile(hFile, L"\r\n",4,&written,NULL);
CloseHandle(hFile);
*/
if(p.GPSFS == 0x30)
{
fRet = FALSE;
goto Exit;
}
if(comma_num[2]-comma_num[1]>1)
{
usebufferSize=comma_num[2]-comma_num[1]-1;
for(j=0;j<usebufferSize;j++)
{
p.GPSTime[j]= Com3Buffer[comma_num[1]+1+j];
}
GPSdata.Time = Latitude(p.GPSTime,usebufferSize);
GPSdata.Time += 80000.000;
printf("GPSdata->Time =%f",GPSdata.Time);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -