为什么在拔下USB电缆时选择返回?

时间:2012-06-05 14:15:15

标签: c usb embedded-linux

我遇到了一些linux c代码的问题,这些代码使用select,FD_ISSET,read等从ttyUSB端口读取。我的调制解调器使用FTDI串口转USB电缆进行输入。问题是拔下USB电缆时选择取消阻止。有没有办法阻止它这样做?

count = 0;
while ( g_running ) {
   FD_ZERO(&readFdSet);
   maxfd = 0;
   numTransPorts = 0;
   logger( DEBUG, "Begin g_running loop - %d", count );
   for ( i = 0; i < MAX_CONFIG_PORTS; i++ ) {
      if ( configPorts[i].commType == 1 && configPorts[i].pttyHost != NULL ) {
         FD_SET( configPorts[i].pttyHost->fd, &readFdSet );
         logger( DEBUG, "FD_SET - fd=%d, index=%d", configPorts[i].pttyHost->fd, i );
         if ( configPorts[i].pttyHost->fd >= maxfd ) {
            maxfd = configPorts[i].pttyHost->fd;
         }
         numTransPorts++;
      }
   }
   maxfd++; // add one because select check a range to n-1 file descriptors
   if ( maxfd != 0 ) { // indicates no ports are available
      logger( DEBUG, "Calling select() with %d ports and maxfd of %d", numTransPorts, maxfd );
      logger( INFO, "Waiting for input ..." );
      select( maxfd, &readFdSet, NULL, NULL, NULL ); // blocking until one available
      if( result == -1 ){
         logger( INFO, "select() error. errno: %d", errno );
      } else if ( result > 0 ){
         for ( i = 0; i < MAX_CONFIG_PORTS; i++ ) {
            if ( FD_ISSET( configPorts[i].pttyHost->fd, &readFdSet ) ) { // input is available
               logger( INFO, "Input on port %s", configPorts[i].pttyHost->serialPath );
               result = serialPortRead( buffer, configPorts[i].pttyHost->fd );
               if ( result <= 0 ) {
                  // there was an error due to the file descriptor. It
                  // probably indicates that the tty ports are no longer available
               }
            }
         }
      } else {
         logger ( INFO, "select() returns 0" );
      }
   }
   count++;
}

serialPortRead:

int serialPortRead( char *buf, int serialHandle ) {
   //char ch;
   char *ptr;
   int res = 0;
   int bytesRead = 0;
   int i;

   logger( TRACE, "TRACE: serialPortRead() with fd = %d", serialHandle );

   ptr = buf;

   // try 3 times
   for ( i = 0; i < 3; i++ ) {
      while ( (res = read( serialHandle, ptr, 1 )) > 0 ) { // read 1 byte at a time
         if ( *ptr == 0x0d ) { //there is 0x0d as a terminate byte from ECR
            break;
         }
         ptr += res;
      }
      if ( res < 0 && errno == EAGAIN ) {
         continue;
      } else {
         break;
      }
   }

   *ptr = 0x00; // set 0x00 as a terminate byte
   //   pthread_mutex_unlock(&g_serial_trans_mutex);
   if ( res < 0 ) {
      // if res is -1, there is an error
      bytesRead = -1;
      logger( DEBUG, "serialPortRead error. errno = %d", errno );
   } else {
      bytesRead = (int) (ptr - buf);
      logger( DEBUG, "serialPortRead %d bytes", bytesRead );
   }

   return bytesRead;
}

拔下USB线时,选择()取消阻止,暗示输入可用,FD_ISSET返回true。 read(),在serialPortRead中,将返回零读取字节。然后它循环回到select(),再次取消阻塞,表示输入可用,依此类推。因此,你得到一个无限循环的select(),FD_ISSET返回true,fd永远不会被清除,read返回0,依此类推。我怎样才能解决这个问题?我期望的行为是,当没有任何东西可以读取时,select不会错误地解锁?

注意:当选择取消阻止时,它会返回一个正数

1 个答案:

答案 0 :(得分:5)

select()正在返回,因为有信息需要读取 - 在这种情况下,文件描述符已达到“文件结束”的事实。这由read()返回0表示。

read()返回0时,您应该关闭相应的文件描述符。