Advertisement
synthnassizer

readComData

Jul 5th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. int readComData(const int toReadBytes, char *serDataBuf, const int stream_status){
  2.     unsigned int idx=0; //index of the serDataBuf
  3.     int remainingBytes=toReadBytes, readBytes=0, totalReadBytes=0;
  4.     fd_set rfds;    // File pointer set for select
  5.     struct timeval serInWait;   // Time to wait for serial input
  6.     int selRetVal;  // Return value of select
  7.  
  8.     //inits
  9.     serInWait.tv_sec = SERIAL_TIMEOUT_SEC;
  10.     serInWait.tv_usec = SERIAL_TIMEOUT_USEC;
  11.     FD_ZERO(&rfds);
  12.     FD_SET(fd, &rfds);
  13.  
  14.     //main fn
  15.     while (remainingBytes>0){
  16.         selRetVal = select(fd+1, &rfds, NULL, NULL, &serInWait);
  17.         if (selRetVal==0){
  18.             DEBUG_SERIAL(printf("SERIAL: DATA read timeout @ byte %d.\n",totalReadBytes));
  19.             return SERIAL_TIMEOUT;
  20.         } else if (selRetVal>0){
  21.             readBytes=read(fd,&serDataBuf[totalReadBytes],remainingBytes);
  22.             if (readBytes<remainingBytes){
  23.                 DEBUG_SERIAL(printf("SERIAL: last data read %d bytes. To a total of %d .\n",readBytes,totalReadBytes));
  24.                 remainingBytes-=readBytes;
  25.                 totalReadBytes+=readBytes;
  26.             } else {
  27.                 DEBUG_SERIAL(printf("SERIAL: All remaining DATA have been read %d .\n",toReadBytes));
  28.             }
  29.         }// if selRetVal
  30.     }//end while
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement