簡體   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