c++ - 在 Cygwin 中的 ioctl() 函数下调用 FIONREAD 时出错
问题描述
我ioctl
使用以下代码调用了该方法来检查可用的字节数:
::ioctl(serial_port, FIONREAD, &available);
我在 Windows 10 下从 Cygwin 编译这个。但我得到这个错误:
error: ‘FIONREAD’ was not declared in this scope; did you mean ‘FREAD’?
我怎样才能解决这个问题?我想检查输入缓冲区中是否有字节,所以当我轮询时,当它为空时我不会出错。我一直有input/output error with error no.5
。
如果无法找到 FIONREAD 常量,是否有另一种方法可以避免input/output error
?我也在使用阻塞模式VMIN = 0 and VTIME = 1
。因此100ms
,每次阅读都在等待。
这些是我的包含文件:
#include <string.h>
#include<iostream>
#include<stdio.h>
#include <string>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
解决方案
您需要添加sys/socket.h
我的prova.c
测试代码
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
int main()
{
int bytesAv =0 ;
int m_Socket=4 ;
if ( ioctl (m_Socket,FIONREAD,&bytesAv) < 0 )
{
// Error
}
}
编译器没有显示任何错误
$ gcc -c prova.c -Wall
推荐阅读
- python - Python:在字典中列出?
- python - 使用维恩图和 GridSpec [python / matplotlib] 错位子图
- haskell - 理解用于在自然数上定义幺半群的方程的问题
- reactjs - 使用 webpack 和样式化组件将字体导入 next.js 反应项目
- javascript - 无法将数据(addRow)写入Nodejs中的Excel文件
- swift - 具有相同键的 JsonDecoder 解码数组在 swift 中以不同的数据类型出现
- java - 如何计时 Apache Flink 流中的检查点?
- php - 如何从多个表Mysql Php中选择最新记录
- typescript - 全局获取和设置 vue 属性并访问基于 typescript 的组件方法(需要自动化/puppeteer)
- ios - 尝试配置 Crashlytics 时,iOS 应用程序在启动期间崩溃