3

First time poster long time reader.

I've been playing round with reading in data from a bluetooth GPS unit. I can connect to it using hyperterm and see the data

The following log is from the hyperterm

$GPRMC,195307.109,A,5208.2241,N,00027.7689,W,000.0,345.8,310712,,,A*7E
$GPVTG,345.8,T,,M,000.0,N,000.0,K,A*07
$GPGGA,195308.109,5208.2242,N,00027.7688,W,1,04,2.1,58.9,M,47.3,M,,0000*7E
$GPGSA,A,3,19,03,11,22,,,,,,,,,5.5,2.1,5.0*3F
$GPRMC,195308.109,A,5208.2242,N,00027.7688,W,000.0,345.8,310712,,,A*73
$GPVTG,345.8,T,,M,000.0,N,000.0,K,A*07
$GPGGA,195309.109,5208.2243,N,00027.7688,W,1,04,2.1,58.9,M,47.3,M,,0000*7E

END LOG

The following log is from my C++ program

$GPGSV,3,3,12,14,20,105,16,28,18,323,,08,07,288,,16,01,178,*7A

$GPRMC,195,3,2ÿþÿÿÿL.š945.109,A,5208.2386,N,00027.7592,W,000.0,169.5,8,323,,08,07,288,,16,01,178,*7A

$GPRMC,195,3,2ÿþÿÿÿL.š310712,,,A*70

$GPVTG,169.5,T,,M,000.0,N,000.0,K,A*06

8,07,288,,16,01,178,*7A

$GPRMC,195,3,2ÿþÿÿÿL.š310712,,,A*70

$GPVTG,169.5,T,,M,000.0,N,000.0,K,A*06

8,07,288,,16,01,178,*7A

$GPRMC,195,3,2ÿþÿÿÿL.š$GPGGA,195946.109,5208.2386,N,00027.7592,W,1.0,K,A*06

8,07,288,,16,01,178,*7A

END LOG

THE PROBLEM I've left the line feeds as they come, the C++ output has extra line feeds, not sure why? The C++ log also has some funky chars...?

The Code

    for (int n=0;n<100;n++) {
        char INBUFFER[100];
        cv::waitKey(1000);
        bStatus = ReadFile(comport,   // Handle
                &INBUFFER,            // Incoming data
                100,                  // Number of bytes to read
                &bytes_read,          // Number of bytes read
                NULL);

        cout << "bStatus " << bStatus << endl;
        if (bStatus != 0)
        {
            // error processing code goes here
        }
        LogFile << INBUFFER;
    }

I'm using settings...

    comSettings.BaudRate = 2400;
    comSettings.StopBits = ONESTOPBIT;
    comSettings.ByteSize = 8;
    comSettings.Parity   = NOPARITY;
    comSettings.fParity  = FALSE;

...which as far as I can tell are the same as the settings used by hyperterm. Any hints on what I'm doing wrong?

cheers!

UPDATE So after updating to use bytes_read and account for the extra LF at the end of NMEA data I now have...

    if (bytes_read!=0) {
        for (int i=0; i < bytes_read; i++) {
            LogFile << INBUFFER[i];
        }
    }

Which appears to have fixed things!

$GPGGA,215057.026,5208.2189,N,00027.7349,W,1,04,6.8,244.6,M,47.3,M,,0000*41
$GPGSA,A,3,32,11,01,19,,,,,,,,,9.7,6.8,7.0*3D
$GPRMC,215057.026,A,5208.2189,N,00027.7349,W,002.0,208.7,310712,,,A*74
$GPVTG,208.7,T,,M,002.0,N,003.8,K,A*09
$GPGGA,215058.026,5208.2166,N,00027.7333,W,1,04,6.8,243.1,M,47.3,M,,0000*42

Thanks folks, your help was much appreciated.

3
  • Does your error processing code goes here really look like that, or are you checking for errors? If so, do you get any? By the way... INBUFFER: ugh! Don't use all uppercase names for variables - keep them for #define symbols. Commented Jul 31, 2012 at 21:03
  • no, I'm not doing any error checking at the mo. Cheers, I'll take note of the case change. Commented Jul 31, 2012 at 21:13
  • The extra line feeds may be because the GPS sends a CR/LF sequence at the end of each line, and that may get interpreted as two separate linefeed character. see here. hemispheregps.com/gpstechinfo/NMEA_0183_Message_Format.htm Commented Jul 31, 2012 at 21:21

1 Answer 1

4

You have a bytes_read var, but you don't do anything with it? Seems to me that you're dumping the entire INBUFFER to the file, no matter how many/few bytes are actually loaded into it?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, i'm now doing. if (bytes_read!=0) { for (int i=0; i <= bytes_read; i++) { LogFile << INBUFFER[i]; } } which has cleaned things up a little. But I'm still getting the odd error here and there $GPGSA,A,1,,,,,,,,,,,,,20.0,20.0,20.0*02 $,GPRMC,210435.193,V,5209.9306,N,00027.1293,W,075.8,356.6,310712,,,N*6A $GPVTG,356.6,T,,M,075.8,NT,140.4,K,N*0F 3$GPGGA,210436.193,5209.9306,N,00027.1293,W,00,00,20.0,106.4,M,47.3,M,,0000*79 ---- Sorry, is it better to update my post above with any results or post them below?
...and in particular if ReadFile actually reads 100 bytes, you're writing random garbage off the end of the buffer as well.
@Oliver9523 - usually best to update the post, or add to it, so earlier answers still make some sort of sense!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.