Personal tools
Document Actions


by aullmj — last modified 2009-01-21 16:36

Code to read data from Novatel GPS

Click here to get the file

Size 3.1 kB - File type text/x-c++src

File contents

#include <windows.h>
#include <iostream>
using namespace std;

HANDLE OpenComPort(int nPortNo=1,int BaudRate = CBR_4800,int ByteSize = 8,int Parity = NOPARITY,int StopBits= ONESTOPBIT);

void main(void)
	int jj; //loop variables
	bool error;//variable to check for errors reading serial port (not used here).
	char line[257];//buffer used to store 1 line of output
	DWORD numberOfBytesRead = 0; //variable recieves number of bytes read reported by ReadFile
	HANDLE m_hFile=OpenComPort(1,CBR_9600);//open com port
	if (m_hFile==NULL)
		cout << "Com port failed. Check hardware, and make sure no other program is accessing."<<endl;
	strcpy(line,"LOG COM1 GPGGA ONTIME .1\n");
	for (jj=0;jj<100;jj++)
	{//read 100 lines from serial port
		error = ReadFile(m_hFile,LPVOID(line),255,&numberOfBytesRead,NULL);
		line[numberOfBytesRead]=0;//terminate string here, so no random data is displayed.
		cout <<line<<endl;//display data.
	CloseHandle( m_hFile ) ;//close port

HANDLE OpenComPort(int nPortNo,int BaudRate,int ByteSize,int Parity,int StopBits)
	char strPort[256];
	HANDLE m_hFile;
	BOOL fSuccess;
	DCB dcb;
	sprintf(strPort, "COM%d", nPortNo) ;
	//Create the handle for read the COM port
	m_hFile = CreateFile( strPort, GENERIC_READ | GENERIC_WRITE,
		(DWORD)NULL, // exclusive access
		NULL, // no security
		NULL // hTemplate
	{//Check whether the handle is valid or not.
		cout<<"Error: Could not open the RS232 port!"<<endl;
		return NULL;
	SetupComm(m_hFile,(DWORD)2048, (DWORD)2048);// setup the com parameters now that we have a handle
	// Set up the DCB with our settings
	fSuccess = GetCommState(m_hFile,&dcb);// Get it first so we fill all members
	if (!fSuccess)
	{// Handle the error.
		cout<<"Error: Failed to Get the System Communication Settings."<<endl;
		return NULL;
	dcb.BaudRate = BaudRate; // baud rate
	dcb.ByteSize = ByteSize; // data size
	dcb.Parity = Parity; // No Parity Bit
	dcb.StopBits = StopBits; // stop bits
	fSuccess = SetCommState(m_hFile, &dcb); // assign it
	if ( fSuccess == 0 )
	{//Now check the configuration of the communication device is valid or not after assign the com parameters
		cout<<"Error: In Control Setting for a Serial Communications Device."<<endl;
		return NULL;
	// Set up the timeouts to use, they are quite short, since we will loop anyway.
	// Do not make them zero, else we will have a CPU load problem. Too large a value,
	// and we have to wait for comms to time out when shutting down.
	GetCommTimeouts(m_hFile, &timeout); // fill timeout structure
	timeout.ReadIntervalTimeout = 100; // 500ms between incomming chars.
	timeout.ReadTotalTimeoutConstant = 500;
	timeout.ReadTotalTimeoutMultiplier = 0;
	timeout.WriteTotalTimeoutConstant = 2000;
	timeout.WriteTotalTimeoutMultiplier = 60; // 60ms per char sent
	SetCommTimeouts(m_hFile, &timeout);
	return m_hFile;