The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 11-13-2007
porter porter is offline
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by arunchaudhary19 View Post
and using usleep to free the cpu and da poll after some time..
Not sure what you mean by usleep. If you are using select or poll the thread will block until something is ready, (use a timeout pointer NULL).

I basically use a loop with the file-descriptor to read and write from, and another pipe() created pair.

Then the read set is the fd and the read end of the pipe.
THe write set is either emtpy, or if have data to send, put in the fd.

Code:
while (1)
{
    fd_set fdr,fdw;
    FD_ZERO(&fdr);
    FD_ZERO(&fdw);
    FD_SET(fd,&fdr);
    FD_SET(pipe_read,&fdr);
    if (dataToWrite) FD_SET(fd,&fdw);

    if (select(1+max(fd,pipe_read),&fdr,&fdw,NULL,NULL)>0)
    {
         if (FD_ISSET(pipe_read,&fdr)) read(pipe_read,buf,1);
         if (FD_ISSET(fd,&fdr)) { ... }
         if (FD_ISSET(fd,&fdw)) { ... }
    }
}
Then if need to write to serial port set the dataToWrite flag, write a byte to the pipe write end, that will unblock the select, it will loop round and reselect the fd for writing,
Reply With Quote