鬼鬼一哈 发表于 2011-11-08 09:54

使用select去读串口数据,监听端口的时候,select阻塞了....鼠标卡住不动了?

本帖最后由 鬼鬼一哈 于 2011-11-08 10:25 编辑

不过数据还是能接受的到了,就是在限定时间内,鼠标不能用,超时后就又可以用了。如何解决select阻塞的问题?程序读写代码如下:
if( write (gprs_fd, gsm_data,71) == 71)
{
      fd_set inset;
      FD_ZERO(&inset);
      FD_SET(gprs_fd, &inset );
      struct timeval tv;
      tv.tv_sec = 24;
      tv.tv_usec = 0;
      memset(buff,0,2048 );
      while(1)
      {
            int res = select(gprs_fd+ 1, &inset, NULL, NULL, &tv );
            switch(res)
            {
            case -1:
               {
          printf("Select error\n" );
        }
        break;
         case 0:             /* Timeout */
               {
                   printf("Time out\n" );
                   return 0;
               }
               break;
         default:
               {
                   if(FD_ISSET(gprs_fd, &inset))
                   {
                     int real_read = read(gprs_fd, buff, 2048 );
                     if (real_read < 0)    printf("read err\n" );
                     else                     printf("%s",buff );
                  }
             }
               break;
      }
}

鬼鬼一哈 发表于 2011-11-08 10:56

本帖最后由 鬼鬼一哈 于 2011-11-08 11:26 编辑

多线程加阻塞,貌似挺好使的哈。。嘎嘎。。。ok啦!

fengfengdiandia 发表于 2012-07-19 09:27

select阻塞时是不是程序一直停在select函数中,请问又是如何解决的,多线程+阻塞

fj97897217 发表于 2012-07-23 11:40

应该是停在select那里 等待你监听的事件!
页: [1]
查看完整版本: 使用select去读串口数据,监听端口的时候,select阻塞了....鼠标卡住不动了?