最近做串口服务器遇到轮询从串口文件描述符中读数据,但是读不到数据:源代码为
uart_fd= open(uart);
while(1)
{
read(uart_fd,buff,sizeof(buff));
}
函数定义:ssize_t read(int fd, void * buf, size_t count);
函数说明:read()会把参数fd所指的文件传送count 个字节到buf 指针所指的内存中。
返回值:返回值为实际读取到的字节数, 如果返回0, 表示已到达文件尾或是无可读取的数据。
若参数count 为0, 则read()不会有作用并返回0。
注意:read时fd中的数据如果小于要读取的数据,就会引起阻塞
626 while(1)
627 {
628 res=0;
629 memset(buff, 0, sizeof(buff));
630 res = xxx_buart_read_bytes(cldev_info->tty_fd, &revdata, 1);
638 ret = xxx_tcp_write(cldev_info->sock_fd, &revdata, res);
646 }
766 static int xxx_epoll_add_event(int epollfd,int fd,int state)
767 {
768 int epoll_res = 0;
769 struct epoll_event ev;
770 ev.events = state;
771 ev.data.fd = fd;
772 epoll_res=epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&ev);
773 return epoll_res;
774 }
775
776
777 static int xxx_epoll_delete_event(int epollfd,int fd,int state)
778 {
779 int epoll_res = 0;
780 struct epoll_event ev;
781 ev.events = state;
782 ev.data.fd = fd;
783 epoll_res=epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,&ev);
784 return epoll_res;
785 }
786 xxx_epoll_add_event(tcpcl_info->pollfd, tcpcl_info->tty_fd, EPOLLIN);
666 while(1)
667 {
668 res = 0;
669 //1.读取有效的线程,将数据放入队列,并使用信号量通知所有的线程
670 epoll_res = epoll_wait(dev_info->pollfd,get_events,((LIG_TTY_MAX<<1)+1),1000);
671 if(epoll_res==-1){
672 pr_log(LOG_ERR,"epoll_wait err %d:%s\n",errno,strerror(errno));
673 continue;
674 }
675 if(epoll_res==0) {
676 //pr_log(LOG_WARNING,"epoll_wait time out\n");
677 continue;
678 }
679 struct epoll_event get_events[(LIG_TTY_MAX <<1 ) + 1];
680 while(epoll_res)
681 {
682 epoll_res--;
683 OBJ_LIG_TCPCL_DEV * tcpcl_info = NULL;
684 for(fori=0;fori<LIG_TTY_MAX;fori++)
685 {
96 if(dev_info->tcpcl_info[fori].tty_fd == get_events[epoll_res].data.fd)
697 {
698 tcpcl_info = &dev_info->tcpcl_info[fori];
699 if(get_events[epoll_res].events & EPOLLIN)
700 {
701 memset(getbuff, 0, sizeof(getbuff));
702 res = xxx_buart_read_bytes(tcpcl_info->tty_fd, getbuff, sizeof(getbuff));
703 }
704 }
705 }