ReadFile USB串口太慢了

时间:2016-02-05 08:17:09

标签: c++ visual-studio serial-port

我需要保存来自GPS的数据。我正在使用Windows 7系统和GPS使用USB端口连接。我正在使用基于visual studio对话框的应用程序

GPS数据是这样的

"$GPGLL,2219.2500182,N,09019.0118688,E,055547.65,A,A*61"

我需要将这些数据保存在文件中。我彻底完成了this link并相应地设置了参数。

char buffer[56];

这是我打开端口的代码

    hcomm= CreateFile("COM8",  
                    GENERIC_READ, 
                    0, 
                    NULL, 
                    OPEN_EXISTING,
                    0,
                    NULL);
if (hcomm == INVALID_HANDLE_VALUE)
    TRACE("%s","error");
memset(&port, 0, sizeof(port));
port.DCBlength = sizeof(port);
if ( !GetCommState(hcomm, &port))
 TRACE("getting comm state");
 if (!BuildCommDCB("baud=19200", &port))
 TRACE("building comm DCB");
 if (!SetCommState(hcomm, &port))
  TRACE("adjusting port settings");
  timeouts.ReadIntervalTimeout = 0;
  timeouts.ReadTotalTimeoutMultiplier = 0;
  timeouts.ReadTotalTimeoutConstant = 0;
  if (!SetCommTimeouts(hcomm, &timeouts))
     TRACE("setting port time-outs.");

我正在阅读以下数据

while(loop which executes after every 20 ms) {
 ReadFile(hcomm, buffer, sizeof(buffer), &read, NULL);
        if ( read ){
        //code to write data to file
               }

虽然我收到数据,但速度非常低。我以10 Hz的频率接收数据。我想以50读数/秒保存数据。

有人能帮助我吗?

修改 根据@Paul R的建议,我将波特率提高到115200.现在,它保存数据@20条消息/秒。我的GPS支持最大更新速率20 Hz和最大波特率115200.如果我想保存数据@ 50条消息/秒,我将需要做什么..

例如,如果更新速率为20 Hz,则每次读数可用50 ms。所以,如果我每隔20分钟读一次端口,那么它是不是应该两次或以适当的比例保存每个条目?

1 个答案:

答案 0 :(得分:1)

它只是基本的算术。在19200 bps时,每秒可以接收大约1920个字符(假设每个字符= 8个数据位+ 1个起始位+ 1个停止位)。上面的示例消息大约是55个字符+行终止符等,这意味着每秒最多30个消息。如果您必须在每个收到的消息之间向设备发送消息,那么它甚至会更低。因此,以这种数据速率无法实现50条消息/秒。

简单解决方案:将数据速率从19200 bps提高到更高,例如57600 bps。