繁体   English   中英

检查串行端口linux中是否有传入数据(对于Linux为cbInQue)

[英]Check if there's incoming data in serial port linux (cbInQue for linux)

我在管理从Arduino单元进入Linux串行端口的数据时遇到麻烦。

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

状态为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?

谢谢

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

类似于以下内容的东西应该起作用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM