从中摘到的信息:
1,函数库文件和原型
#include <sys/select.h> /* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
void FD_CLR(int fd, fd_set *set); int FD_ISSET(int fd, fd_set *set); void FD_SET(intfd, fd_set *set);
void FD_ZERO(fd_set *set);
2,select() may update the timeout argument to indicate how much time was left.
这句是说,调用select()之后,可能会将timeval的值修改为调用后剩余的时间,因此,每次调用之前,timeval的值都要重新设置。
3, On exit, the sets are modified in place to indicate which file descriptors actually changed status. Each of the three file descriptor sets may be specified as NULL if no file descriptors are to be watched for the corresponding class of events.
这句是说,在函数返回时,文件描述符集的内容将会被修改,状态发生变化的文件描述符被置位。(by 杨w:文件描述符相当于一个位图,其中一个bit代表一个文件描述符,如果监视的文件描述符的状态发生了变化,那么它对应的bit会被置1)。
因此,每次调用select()函数之前,fd_set也要重新设置。
4,例子
//变量的声明和定义
int can_fd; fd_set rfds; struct timeval tv;
can_fd = open("/dev/can", O_RDWR); if(can_fd<=0) { qDebug()<<"can't open can_dev "<<endl; cout<<"can't open can_dev"<<endl; return; } else qDebug()<<"open can_dev successfully!"<<endl;
FD_ZERO(&rfds);//清空串口接收端口集 FD_SET(can_fd, &rfds);//设置串口接收端口集 tv.tv_sec = 0; tv.tv_usec = 50000;//50ms