【问题标题】:Check if there's incoming data in serial port linux (cbInQue for linux)检查串口linux中是否有传入数据(cbInQue for linux)
【发布时间】:2019-03-13 05:12:11
【问题描述】:

我在管理从 Arduino 单元进入 linux 串行端口的数据时遇到问题。

基本上,我有一个可以读取和打印(或只是存储)的工作代码,但它是为 Windows 平台编写的:

status 是 COMSTAT 类型

int SerialPort::readSerialPort(char *buffer, unsigned int buf_size)
{
  DWORD bytesRead;
  unsigned int toRead = 0;
  Sleep(500);
  ClearCommError(this->handler, &this->errors, &this->status);

  if (this->status.cbInQue > 0) { //if there's something to read 
    if (this->status.cbInQue > buf_size) {//if there's something to read&bigger than buffer size
        toRead = buf_size; //read as much as buffer alows 
    }
    else toRead = this->status.cbInQue; //else just read as much as there is
}

if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL))
    return bytesRead; //return read data


return 0;//return 0 if nothing is there.
}

除了 Windows 函数 Sleep() 之外,我想知道 status.cbInQue 是否有一个等效的 linux 函数来了解端口中是否有任何数据可以读取。 现在我只是继续阅读,没有检查,而且我经常在程序的后面没有打印任何内容。

TLDR:是否有 linux 的 cbInQue 等价物?

谢谢

【问题讨论】:

  • 您可以在Linux中读取串口,例如cat /dev/ttyS0,这样您就可以看到是否有任何流量。只是为了调试知道你的程序在做什么。
  • 嗯,通常我确实从 arduino 获得串行输入,但我检查了最简单的情况,即 arduino 不停地发送诸如“来自 Arduino 的 Hello World”之类的东西。我正在寻找 a-C++ 实现,以便我可以将它作为代码中的安全网应用。这就是我如何运行它并等到有任何东西(然后才阅读它)

标签: c++ linux serial-port port


【解决方案1】:

是的,您将需要您的文件描述符,然后使用FIONREAD 来查看是否有任何可用的内容。

类似下面的东西应该可以工作:

int available;
if( ioctl( fd, FIONREAD, &available ) < 0 ) {
    // Error handling here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多