在 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