title: Use epoll() command to read data from a file descriptor
body: <p>I have an open file descriptor for "<strong>/dev/ttyS0</strong>" which used to read data from a <strong>RS232</strong> device. I am trying to use an <strong>epoll</strong> instance to do concurrent read from file descriptor As I am getting an error (Resource temporarily unavailable) while reading data from the file descriptor. I have sample code written with epoll to do this but it is not running as expected. Program is infinitely waiting at <strong>epoll_wait()</strong> call.</p>
  
  <p>Please help me to verify the way I wrote the code<br>
  <strong>fdRS232</strong> is the opened file descriptor for <strong>/dev/ttyS0</strong></p>
  
  <pre><code>int DeviceRS232::readDecoderData(unsigned char *dataBuffer, size_t bufferSize)
  {
      unsigned char recvBuffer[251];
      unsigned char *ptrChar;
      int nBytes, portStatus;
      int inputBufSize = 0;
  
      // Use epoll for read and write event handling
      int fdEpoll, returnStatus;
      struct epoll_event rwEpollEvent;
      struct epoll_event *prwEpollEvent;
  
      fdEpoll = epoll_create1(0);
      if(fdEpoll == -1)
      {
          std::cout &lt;&lt; "Error: " &lt;&lt; strerror(errno);
          return 0;
      }
  
      rwEpollEvent.data.fd = fdRS232;
      rwEpollEvent.events = EPOLLIN | EPOLLET;
  
      returnStatus = epoll_ctl(fdEpoll, EPOLL_CTL_ADD, fdRS232, &amp;rwEpollEvent);
      if(returnStatus == -1)
      {
          std::cout &lt;&lt; "Error: " &lt;&lt; strerror(errno);
          return 0;
      }
      prwEpollEvent = (epoll_event *)calloc(MAX_EVENTS, sizeof(rwEpollEvent));
  
      while(true)
      {
          std::cout &lt;&lt; "test1" &lt;&lt; std::endl;
          int nfd;
          nfd = epoll_wait(fdEpoll, prwEpollEvent, MAX_EVENTS, -1);
          std::cout &lt;&lt; "ndf: " &lt;&lt; nfd &lt;&lt; std::endl;
  
          for(int x=0; x&lt;nfd; x++)
          {
              std::cout &lt;&lt; "test-for" &lt;&lt; std::endl;
              if(prwEpollEvent[x].events &amp; EPOLLERR ||
                 prwEpollEvent[x].events &amp; EPOLLHUP ||
                 !(prwEpollEvent[x].events &amp; EPOLLIN))
              {
                  std::cout &lt;&lt; "test2" &lt;&lt; std::endl;
                  std::cout &lt;&lt; "epoll error: " &lt;&lt; strerror(errno);
                  close(prwEpollEvent[x].data.fd);
                  continue;
              }
              else
              {
                  std::cout &lt;&lt; "test3" &lt;&lt; std::endl;
                  ChangeCTS(fdRS232, 0);
                  ChangeRTS(fdRS232, 0);
  
                  while(inputBufSize &lt;= 0)
                  {
                      ioctl(fdRS232, FIONREAD, &amp;inputBufSize);
                      usleep(1);
                  }
  
  
                  if(inputBufSize &gt; 0)
                  {
                      int decodePacketLen = 0;
                      //unsigned char
                      memset(recvBuffer, 0x00, sizeof(recvBuffer));
                      nBytes = 0;
  
                      usleep(100000);
                      while(nBytes &lt; ((int)recvBuffer[0] + 2))
                      {
                          int index = 0;
                          int recvDataLen = 0;
                          if(nBytes != 0)
                              index = nBytes - 1;
  
                          recvDataLen = read(fdRS232, &amp;recvBuffer[index], 251);
                          if(recvDataLen &lt; 0)
                          {
                              std::cout &lt;&lt; "[INFO@DeviceRS232::receiveDecodedData]File read error: " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
                              //sleep(1);
                          }
  
                          nBytes += recvDataLen;
                          if(nBytes == ((int)recvBuffer[0] + 2))
                              break;
  
                      }
  
                      if(recvBuffer[1] == DECODE_DATA)
                          sendCommandToDecoder(OPCODE_ACK);
  
                      std::cout &lt;&lt; "[INFO @ DeviceRS232::receiveDecodedData]Data Lenght (without CheckSum) : " &lt;&lt; (int)recvBuffer[0] &lt;&lt; std::endl;
  
                      for(int i=0; i&lt;nBytes; i++)
                      {
                          std::cout &lt;&lt; "recvBuffer[" &lt;&lt; i &lt;&lt; "]: ";
                          printf("%x\n", recvBuffer[i]);
                      }
                      std::cout &lt;&lt; "-----------------------------------" &lt;&lt; std::endl;
  
                      //ChangeRTS(fdRS232, 1);
                      //ChangeCTS(fdRS232, 1);
                      //sleep(1);
                  }
  
              }
          }
      }
  
      free(prwEpollEvent);
  
      //strcpy((char *)dataBuffer, (char *)recvBuffer);
      memcpy((char *)dataBuffer, recvBuffer, sizeof(recvBuffer)/sizeof(recvBuffer[0]));
      inputBufSize = 0;
  
      return nBytes;
  }
  </code></pre>