《朱老师物联网大讲堂》学习笔记
学习地址:www.zhulaoshi.org
非阻塞IO,如果没有读到东西,就立即返回,
改变标准输入为非阻塞式的,
flag = fcntl( 0, F_GETFL );
flag |= O_NONBLOCK;
fcntl( 0, F_SETFL, flag );
非阻塞打开鼠标,
fd = open( "/dev/input/mouse1", O_RDONLY | O_NONBLOCK );
然后出现了一种结果,
从鼠标和键盘所读出的内容都是空的,因为来不及输入,
接着,做了一种改变,
把读鼠标和键盘放到一个while(1)中,
经过这次修改,结果看起来舒服多了,
改变标准输入为非阻塞式的,
flag = fcntl( 0, F_GETFL );
flag |= O_NONBLOCK;
fcntl( 0, F_SETFL, flag );
非阻塞打开鼠标,
fd = open( "/dev/input/mouse1", O_RDONLY | O_NONBLOCK );
然后出现了一种结果,
从鼠标和键盘所读出的内容都是空的,因为来不及输入,
接着,做了一种改变,
把读鼠标和键盘放到一个while(1)中,
经过这次修改,结果看起来舒服多了,
如下是老师课上所讲解的示例代码,
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
// 读取鼠标
int fd = -1;
int flag = -1;
char buf[200];
int ret = -1;
fd = open("/dev/input/mouse1", O_RDONLY | O_NONBLOCK);
if (fd < 0)
{
perror("open:");
return -1;
}
// 把0号文件描述符(stdin)变成非阻塞式的
flag = fcntl(0, F_GETFL); // 先获取原来的flag
flag |= O_NONBLOCK; // 添加非阻塞属性
fcntl(0, F_SETFL, flag); // 更新flag
// 这3步之后,0就变成了非阻塞式的了
while (1)
{
// 读鼠标
memset(buf, 0, sizeof(buf));
//printf("before 鼠标 read.\n");
ret = read(fd, buf, 50);
if (ret > 0)
{
printf("鼠标读出的内容是:[%s].\n", buf);
}
// 读键盘
memset(buf, 0, sizeof(buf));
//printf("before 键盘 read.\n");
ret = read(0, buf, 5);
if (ret > 0)
{
printf("键盘读出的内容是:[%s].\n", buf);
}
}
return 0;
}