ioctl之FIONREAD

在学习ioctl 时常常跟 read, write 混淆。其实 ioctl 是用来设置硬件控制寄存器,或者读取硬件状态寄存器的数值之类的。

而read,write 是把数据丢入缓冲区,硬件的驱动从缓冲区读取数据一个个发送或者把接收的数据送入缓冲区。

ioctl(keyFd, FIONREAD, &b)

得到缓冲区里有多少字节要被读取,然后将字节数放入b里面。

接下来就可以用read了。

read(keyFd, &b, sizeof(b))

这两个可以用在按键控制上,先是检测按键是否被按下,如果被按下就放在B里,然后user 在读取按键对应数值。

Listing - Getting the number of bytes in the input buffer.
清单 - 读取串行端口输入缓冲区中的字节数
#include <unistd.h>
#include <termios.h>
int fd;
int bytes;
ioctl(fd, FIONREAD, &bytes);

eg:

#include<stdio.h>

#include<stdlib.h>

#include<sys/ioctl.h>

#include<errno.h>

int kbhit(){

int i;

if(ioctl(0,FIONREAD,&i)<0){

printf("ioctl failed, error=%d\n ",errno);

exit(1);

}

return i;

}

main(){

int i=0;

int c=' ';

system("stty raw -echo" );

printf("enter 'q' to quit \n" );

for(;c!='q';++i){

if(kbhit()){

c=getchar();

printf("\n got %c, on iteration %d",c,i);

}

}

system("stty cooked echo" );

return 0;

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值