检查串行端口Linux中是否有传入数据(cbInQue for linux)
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.
}
除了 Sleep(( 即 windows 函数之外,我想知道是否有一个等效的 linux 函数用于 status.cbInQue 以了解是否有任何数据准备好在端口中读取。 现在我只是继续阅读,没有检查,而且通常我在程序后面什么也没打印出来。
TLDR:Linux有cbInQue等价物吗?
谢谢
是的,您将需要文件描述符,然后使用FIONREAD
查看是否有任何可用内容。
类似以下内容的内容应该有效:
int available;
if( ioctl( fd, FIONREAD, &available ) < 0 ) {
// Error handling here
}
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 检查串行端口Linux中是否有传入数据(cbInQue for linux)