Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 总是得到读取超时_C++_Libserial - Fatal编程技术网

C++ 总是得到读取超时

C++ 总是得到读取超时,c++,libserial,C++,Libserial,我目前正试图使PIC32UBLQT/Linux/Mac端口通过串行端口工作。到目前为止,我总是在读取串行端口时超时 这是完整的开源项目 我正在使用/dev/ttyUSB0作为串行端口,这个适配器已经过测试。此外,我还使用逻辑分析仪验证了目标电路正在接收/发送数据到主机程序(PIC32UBL qt)。此外,它完全可以与Windows版本的PIC32UBL配合使用 缺陷零件位于部件cpp:156 unsigned short CComPort::ReadComPort(char* buffer, i

我目前正试图使PIC32UBLQT/Linux/Mac端口通过串行端口工作。到目前为止,我总是在读取串行端口时超时

这是完整的开源项目

我正在使用
/dev/ttyUSB0
作为串行端口,这个适配器已经过测试。此外,我还使用逻辑分析仪验证了目标电路正在接收/发送数据到主机程序(PIC32UBL qt)。此外,它完全可以与Windows版本的PIC32UBL配合使用

缺陷零件位于
部件cpp:156

unsigned short CComPort::ReadComPort(char* buffer, int MaxLen)
{
  if (serialPort != NULL)
  {
    SerialPort::DataBuffer dataBuffer;

    // Added a catch for timeouts
    try
    {
      serialPort->Read(dataBuffer, MaxLen,10);

      std::copy(dataBuffer.begin(), dataBuffer.end(), buffer);
    }
    catch(std::exception &e)
    {

    }

    return dataBuffer.size();
  }
  return 0;
}
因此,
Read
总是抛出一个超时,尝试了不同的计时(1001000):总是超时,还尝试了无限(0):永不退出


这个问题可能与
libserial
正在使用信号处理器,而我正在使用串行/usb FTDI适配器有关吗?

根据chux的评论,我一次只读取一个字符进行测试,它工作得很好,下面是程序中该方法的最终版本:

unsigned short CComPort::ReadComPort(char* buffer, int MaxLen)
{
  if (serialPort != NULL)
  {
    int nCount = 0;

    while(serialPort->IsDataAvailable() && nCount < MaxLen)
    {
      buffer[nCount++] = serialPort->ReadByte(10);
    }

    return nCount;
  }

  return 0;
}
unsigned short-CComPort::ReadComPort(char*buffer,int-MaxLen)
{
如果(serialPort!=NULL)
{
int nCount=0;
而(serialPort->IsDataAvailable()&&nCountReadByte(10);
}
返回n计数;
}
返回0;
}

1)尝试只读取1个字节怎么样?(什么是
maxlen
)2)可靠数据正在接收正确波特率、停止位等的数据?通过将此端口的输出数据作为自己的输入进行循环,可以消除这种潜在的差异。@chux感谢您的指针,它一次读取一个字节,工作正常,将发布答案,包括此函数的工作代码版本。很高兴这对您“有效”,但它可能非常低效。如果数据现在不可用,代码似乎并不真正想要等待-返回。为了提高效率,请考虑使用一个命令来查询一次可获得多个字节的数量。@ CHUX井<<代码> Read <代码>函数不起作用(由于某种原因),此代码不是非常低效的,因为它将循环直到没有可用字符或<代码> Max LIN <代码>……我怀疑<代码> Read()正在“工作”。只是您请求的是
MaxLen
字节,并且小于
MaxLen
字节是可用的,所以它等待,等待。。。。IAC,建议稍后查看此解决方案。