用c ++读取COM端口,得到错误

时间:2012-07-31 20:34:42

标签: c++ serial-port

第一次海报长期读者。

我一直在玩蓝牙GPS装置的数据。 我可以使用hyperterm连接到它并查看数据

以下日志来自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

以下日志来自我的C ++程序

$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

问题 我已经离开了这些换行符,C ++输出有额外的换行符,不知道为什么? C ++日志也有一些时髦的字符......?

守则

    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;
    }

我正在使用设置......

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

...据我所知,它与hyperterm使用的设置相同。 关于我做错了什么的暗示?

喝彩!

更新 因此,在更新为使用bytes_read并在NMEA数据结尾处考虑额外的LF后,我现在有...

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

似乎有固定的东西!

$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

谢谢大家,非常感谢你的帮助。

1 个答案:

答案 0 :(得分:4)

你有一个bytes_read var,但是你没有做任何事情吗?在我看来,无论实际加载了多少/几个字节,你都将整个INBUFFER转储到文件中?