jump to navigation

Opening and talking NMEA under Win32 July 22, 2005

Posted by vinnyt in : General , trackback

okay, so I’ve started creating my GPS application. I’m going to start with a win32 program written in C++, I’m not going to use MFC, because frankly I don’t like it, and I feel that I can have a finer grain level of control using straight win32 API. Win32API C++ will allow me to work out the kinks build some functions I need and then get down to the nitty gritty of what I want to do. Then later when I translate this to a PDA or a phone whatever ultimately becomes my platform of choice I will have worked through the kinks. The application design is going to be as follows: It’s going to be a multi-threaded application there will end up being 3 threads when all is said and done. The first thread is the main execution thread that windows creates for you this thread handles user interaction with the GUI of the applications clicks and so forth, very standard in Win32. The second thread is going to handle talking to the GPS device, this thread is responsible for opening the COM port setting up parameters for that port and finally reading and parsing the NMEA sentences that come from the device. Finally the 3rd thread will be responsible for sending message to the webserver when a data connection is available. There are some things I must consider. First, I want to store every single point that I cross, and because I’m using a cellular data network the connection will not always be available to me so there needs to be a queue between the GPS thread and the network thread to store events while they are waiting to be sent. I’m going to make a simplifying assumption here and use a non-locking queue, the way you do this is always leave at least 1 item in the queue. You then don’t have to worry about locking the queue and you can cut out some of the overhead that would be created if I was going to add critical section code. I just have to make sure that when I turn off the application I check that queue to make sure all items have been sent.
So I’m going to start simple and create my connection to the device. Here’s the code I use to do that.
unsigned __stdcall GPSReader( LPVOID lpThreadParameter ){
HANDLE hSerial;
DCB dcbSerialParams = {0};

// my device lives on COM4, and will probably for the duration of this program never move
hSerial = CreateFile( “COM4″, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );

if ( hSerial == INVALID_HANDLE_VALUE )
{
if ( GetLastError() == ERROR_FILE_NOT_FOUND )
{
MessageBox(
NULL, “Could not open COM4″, “FAILED”, MB_OK );
return FALSE;
}
}

dcbSerialParams.DCBlength = sizeof( dcbSerialParams );
if ( !GetCommState( hSerial, &dcbSerialParams ) )
{
MessageBox( NULL, “problem getting state”, “FAILED”, MB_OK );
return FALSE;
}

// The NMEA standard dictates the device must communicate
// at 4800,8,N,1
dcbSerialParams.BaudRate = CBR_4800;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;

if ( !SetCommState( hSerial, &dcbSerialParams ) )
{
MessageBox( NULL, “problem setting state”, “FAILED”, MB_OK );
return FALSE;
}

for ( ;; )
{
HRESULT hres;
char szBuff[1024 + 1] = {0};
DWORD dwBytesRead = 0;
if( !ReadFile( hSerial, szBuff, 1024, &dwBytesRead, NULL ) ) {
return 1;
}
else
MessageBox( NULL, szBuff, “message”, MB_OK );
}
return 1;
}

At present this code simply opens the port reads data from it and displays this data. So next I’ll have to parse this data and slam it in my queue….. Stay tuned.

Comments»

no comments yet - be the first?


The internet phone service, provided by different telecommunication sectors is very feasible for the customers who may use the ordinary phone for broadband phone calls in the very cheapest rates. There is a need of a complete set for voice mail, voip phone adapter and caller ID to have direct accessibility to the broadband internet voip. The most leading company of broadband phone services is known as vonage. The wireless phone connection is very suitable with the different phone plans and services to ensure the quality of services by providing all necessary answer via voip faq.