当前位置: 动力学知识库 > 问答 > 编程问答 >

Reading COM port in c++, getting errors

问题描述:

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.

网友答案:

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?

分享给朋友:
您可能感兴趣的文章:
随机阅读: