我无法管理从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等效项?
谢谢
答案 0 :(得分:0)
是的,您将需要文件描述符,然后使用FIONREAD
查看是否有可用的文件。
类似以下的方法应该起作用:
int available;
if( ioctl( fd, FIONREAD, &available ) < 0 ) {
// Error handling here
}