首页 > 解决方案 > 在 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> 

标签: c++unixserial-portcygwinunistd.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

推荐阅读