예제 #1
0
파일: GccGPS.cpp 프로젝트: abdojobs/gccv2
extern "C" __declspec(dllexport) int GccReadGps(short &hour, short &min, short &sec, short &msec,                 // from GPRMC
                                                double &latitude, double &longitude,                              // from GPRMC
                                                int &num_sat, double &hdop, double &altitude,                 // from GPGGA
                                                double &geoid_sep, int &max_snr,                                   // from GPGSV
                                                double &speed, double &heading,                        // from GPRMC
												short &day, short &month, short &year)                 // from GPRMC
{
    if(__read_lock) { return 0; }
	
    const int BUF_SIZE = 4096;
    DWORD dwBytesRead;
    char buf[BUF_SIZE+1];
    int return_status = 0;
	FILE *file = NULL;

	if (logRawNmea)
	{
		file = fopen("\\tmp.txt", "a");
		fprintf(file, "File handle status: %d\n", (__hGpsPort != INVALID_HANDLE_VALUE ? 1 : 0));
	}

    // reset output vars
    hour = -1;       min = -1;         sec = -1;
    latitude = -32768.0; longitude = -32768.0;
    num_sat = -1;    hdop = -32768.0;      altitude = -32768.0;
    geoid_sep = -32768.0;     max_snr = -1;
	speed = -1.0;    heading = -1.0;
	day = -1;       month = -1;       year = -1;

    if(__hGpsPort == INVALID_HANDLE_VALUE)
    { 
		if (logRawNmea)
			fclose(file);
		return return_status;
	}

    // read file
    __read_lock = true;
    BOOL status = ReadFile(__hGpsPort,	 // handle of file to read
                           buf,		 // address of buffer that receives data
						   filemode ? 600 : BUF_SIZE,	 // number of bytes to read
                           &dwBytesRead, // address of number of bytes read
                           NULL		 // address of structure for data
                           );
    __read_lock = false;

	if (logRawNmea)
		fprintf(file, "GPS status: %d, read status: %d, bytes read: %d \n", GccGpsStatus(), (status ? 1 : 0), dwBytesRead);

    // clear errors if any
    if(status == false)
    {
        DWORD dwErrors;
        COMSTAT ComStat;
        ClearCommError(__hGpsPort,&dwErrors,&ComStat);
		if (logRawNmea)
			fclose(file);
		return return_status;
    }

    // terminate string
    buf[dwBytesRead] = '\0';

    // nothing read - return
    if(dwBytesRead == 0)
    {
        return_status |= READ_NO_ERRORS;
		if (logRawNmea)
			fclose(file);
		return return_status;
    }

    // has some data
    return_status |= READ_NO_ERRORS;
    return_status |= READ_HAS_DATA;

	if(filemode)
	{
		char* ptr = strstr(buf+1, "$GPGGA");
		if(ptr != NULL)
		{
			SetFilePointer(__hGpsPort, (ptr - buf) - dwBytesRead, NULL, FILE_CURRENT);
			dwBytesRead = ptr - buf;
			*ptr = '\0';
		}
	}

    // too much bytes indicate lack of computing power
    if(dwBytesRead > 600)
	{
		//MessageBeep(0);	//test
		if (logRawNmea)
			fprintf(file, "Warning: Too much Bytes read: %d\n", dwBytesRead);

		if(dwBytesRead == BUF_SIZE)
		{
			PurgeComm(__hGpsPort, PURGE_RXCLEAR);
			MessageBeep(1);	//test
		}
		__save_str = std::string(buf);
		__save_str = __save_str.erase(0, dwBytesRead - 600);	//remove old data (would be overwritten anyway)
	}
	else
	{
		// append to string stored from the last read
		__save_str += std::string(buf);
	}

    // chop into lines
    std::vector<std::string> lines;
    std::vector<std::string> words;

    ChopIntoLines(__save_str, lines);

    // check what to do with the last segment. If it is not complete - store.
    if(lines.size() && !filemode)		//in filemode would store "----- received 7 lines ----" without CRLF!
    {
        std::string &last_line = lines[lines.size()-1];
        if(last_line.size() > 3)
        {
            if(last_line[last_line.size()-3] != '*')
            {
                __save_str = last_line;
                lines.pop_back();
            }
        }
        else
        {
            __save_str = last_line;
            lines.pop_back();
        }
    }

	if (logRawNmea)
		fprintf(file, "------------ received %d lines ------------ \n", lines.size() );

    // parse lines
    for(unsigned int i = 0; i < lines.size(); i++)
    {
		if (logRawNmea)
			fprintf(file, "%s\n", lines[i].c_str());

        // process valid lines only, i.e. the one with correct checksum
        if(IsLineValid(lines[i]))
        {
            ChopIntoCommaSepWords(lines[i], words);
            if(words.size() == 0) { continue; } 

            if(words[0] == "$GPGSV")
            {
                ParseGPGSV(words);

				gpgsv_trust_count = 10;
            }
            else if(words[0] == "$GPGGA")
            {
                ParseGPGGA(words);

                return_status |= READ_HAS_GPGGA;

                //hour     = ___hour;     min       = ___min;       sec = ___sec;
                //latitude = ___latitude; longitude = ___longitude;
                hdop = ___hdop;    altitude = ___altitude;    geoid_sep = ___geoid_sep;
            }
            else if(words[0] == "$GPRMC")
            {
                ParseGPRMC(words);

                return_status |= READ_HAS_GPRMC;

				hour = ___hour;     min = ___min;       sec = ___sec;    msec = ___msec;
				latitude = ___latitude; longitude = ___longitude;
                speed = ___speed; heading = ___heading;
				day = ___day; month = ___month; year = ___year;
            }
        }
		else if (logRawNmea)
		{	fprintf(file, "^invalid line\n"); }
    }
	if(gpgsv_trust_count > 0)
	{
		max_snr = ___max_snr; num_sat  = ___num_sat;	//use last values of SNR and NUM to avoid blinking (for 10s)
		return_status |= READ_HAS_GPGSV;				//(some receivers deliver $GPGSV only every 2s or 5s)
		gpgsv_trust_count--;
	}

	if (logRawNmea)
		fclose(file);

    /*  debug write (if want to dump the read buffer, if line chopping did not work)
    if(dwBytesRead)
    {
        FILE *file = fopen("\\tmp.txt", "a");
        fwrite(buf, sizeof(char), dwBytesRead, file);
        fclose(file);
    }
    */

    return return_status;
}
예제 #2
0
//Load external modules from file system.
static BOOL LoadExternalMod(LPSTR lpszModCfgFile)
{
	CHAR    FullPathName[128];
	CHAR*   pBuffer = NULL;
	CHAR*   pFileBuff = NULL;
	CHAR*   pLineBuff = NULL;
	HANDLE  hFile   = NULL;
	DWORD   dwReadSize = 0;
	BOOL    bResult    = FALSE;
	__EXTERNAL_MOD_DESC modDesc;
	int     index      = 0;

	if(NULL == lpszModCfgFile)  //Invalid file.
	{
		goto __TERMINAL;
	}
	//Form the valid path name.
	strcpy(FullPathName,OS_ROOT_DIR);
	strcat(FullPathName,lpszModCfgFile);
	//Try to open the file.
	hFile = CreateFile(FullPathName,
		FILE_ACCESS_READ,
		0,
		NULL);
	if(NULL == hFile)
	{
		PrintLine("Can not open the module configure file.");
		goto __TERMINAL;
	}
	//Try to read the file.
	pBuffer   = (CHAR*)KMemAlloc(MAX_MODCF_LEN + 1,KMEM_SIZE_TYPE_ANY);
	pLineBuff = (CHAR*)KMemAlloc(MAX_LINE_LENGTH + 1,KMEM_SIZE_TYPE_ANY);
	if((NULL == pBuffer) || (NULL == pLineBuff))
	{
		PrintLine("In Module Manager object,LoadExternalMod: Allocate memory failed.");
		goto __TERMINAL;
	}
	pFileBuff = pBuffer;  //Use pFileBuff to operate the memory.
	if(!ReadFile(hFile,
		MAX_MODCF_LEN,
		pFileBuff,
		&dwReadSize))
	{
		PrintLine("Can not read MODCFG.INI file.");
		goto __TERMINAL;
	}
	pFileBuff[dwReadSize] = 0;  //Set the end of memory block.
	
	//Now try to read each line from pBuffer and load it.
	while(FetchLine(pFileBuff,pLineBuff,&index))
	{
		if(IsLineValid(pLineBuff))
		{
			if(FetchModDesc(pLineBuff,&modDesc))
			{
				//PrintLine(pLineBuff);
				LoadModule(&modDesc);  //Load the module into memory.
			}
		}
		pFileBuff += index;  //Skip the processed line.
	}
	bResult = TRUE;

__TERMINAL:
	if(NULL != hFile)
	{
		CloseFile(hFile);
	}
	if(NULL != pBuffer)
	{
		KMemFree(pBuffer,KMEM_SIZE_TYPE_ANY,0);
	}
	if(NULL != pLineBuff)
	{
		KMemFree(pLineBuff,KMEM_SIZE_TYPE_ANY,0);
	}
	return bResult;
}