reading from com port

Hi all,

I'm attempting to read some data from a com port. It's a bluetooth GPS unit. I've managed to open the port with Matlab and read the NMEA data coming from it.

Question is (as you might have guessed) how do I go about it in C++

I've found this snippet of code

1
2
3
4
5
6
hComPort= CreateFile("\\\\.\\COM24", GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

if (hComPort == INVALID_HANDLE_VALUE){ 
   hComPort= 0;
   return (lastError= ERR_OPEN_COMM); /* Error! */
}


Errors are...
hComPort is unidentified (this is expected, but what do i define it as?)
"\\\\.\\COM24" arg of type "const char *" is incompat with param of type LPCWSTR
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx

1. CreateFile() returns a HANDLE. If you've included <windows.h>, then you have HANDLE defined.
2. You will either have #define CreateFile CreateFileA or #define CreateFile CreateFileW depending on the convention that you have selected with your compiler (unicode or multi-byte). You have two options:
a. Change your project settings to select unicode character sets
b. Replace "\\\\.\\COM24" with L"\\\\.\\COM24"

1
2
3
4
5
6
HANDLE hComPort= CreateFile(L"\\\\.\\COM24", GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

if (hComPort == INVALID_HANDLE_VALUE){ 
   hComPort= 0;
   return (lastError= ERR_OPEN_COMM); /* Error! */
}
Last edited on
That's great cheers, I've also found this code

http://www.control.com/1026217270/index_html

Now I just need to figure out how to read in line at a time.

It uses
char INBUFFER[500];
to store the data being read from the GPS, but the NMEA data consists of different length of data being streamed from the device.

So one might be
$GPSXX 3,4,5,4,34,2,34,4,3F
another might be
$GPSYY 45.3,363,343,6,3,2,564,N,235,36,E,6D

I'm guessing I need to tell it to stop storing data in INBUFFER if some end of line char is detected, that sound about right?
Topic archived. No new replies allowed.