Serial port: O_NONBLOCK vs VMIN and VTIME
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Serial port: O_NONBLOCK vs VMIN and VTIME




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Serial port: O_NONBLOCK vs VMIN and VTIME  
Simon Elliott


Report This Message To A Moderator Edit/Delete Message


 
08-01-05 10:54 PM

I've not done any non-blocking serial port I/O with Unix, and now I
want to do some non blocking reads and writes. I'm not clear on the
subtleties of

fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK));

versus

my_termios.c_cc[VMIN]=0;
my_termios.c_cc[VTIME]=0;

Is there a difference between these for reads and writes? Which is
preferred? Would it hurt to do both?

--
Simon Elliott    http://www.ctsn.co.uk





[ Post a follow-up to this message ]



    Re: Serial port: O_NONBLOCK vs VMIN and VTIME  
Floyd L. Davidson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-02-05 01:49 AM

"Simon Elliott" <Simon at ctsn.co.uk> wrote:
>I've not done any non-blocking serial port I/O with Unix, and now I
>want to do some non blocking reads and writes. I'm not clear on the
>subtleties of
>
>fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK));
>
>versus
>
>my_termios.c_cc[VMIN]=0;
>my_termios.c_cc[VTIME]=0;

Read the man page read(2) to see the difference.  With the
O_NONBLOCK flag set, when there is no data the call to read(2)
will return -1 with errno set to EAGAIN to indicate that
condition.

If O_NONBLOCK is not set and c_cc[VMIN] nad c_cc[VTIME] are 0,
read(2) will return 0, which may or may not be useful (for
example, you cannot distinquish between EOF and no data).

>Is there a difference between these for reads and writes? Which is
>preferred? Would it hurt to do both?

When the O_NONBLOCK flag is set both c_cc[VMIN] and c_cc[VTIME]
are ignored.

--
Floyd L. Davidson            <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         floyd@apaflo.com





[ Post a follow-up to this message ]



    Re: Serial port: O_NONBLOCK vs VMIN and VTIME  
Simon Elliott


Report This Message To A Moderator Edit/Delete Message


 
08-02-05 12:51 PM

On 02/08/2005, Floyd L. Davidson wrote:

>
> Read the man page read(2) to see the difference.  With the
> O_NONBLOCK flag set, when there is no data the call to read(2)
> will return -1 with errno set to EAGAIN to indicate that
> condition.
>
> If O_NONBLOCK is not set and c_cc[VMIN] nad c_cc[VTIME] are 0,
> read(2) will return 0, which may or may not be useful (for
> example, you cannot distinquish between EOF and no data).

By EOF do you mean DCD dropping?

I need to read bytes as they are sent, irrespective of DCD, and monitor
DCD separately via ioctl and TIOCMGET. I hope CRTSCTS implements a
"real" RTS/CTS flow control with the RTS and CTS lines only, and
ignores DCD, otherwise I'm going to have to very rapidly learn about
Linux device drivers!
 
>
> When the O_NONBLOCK flag is set both c_cc[VMIN] and c_cc[VTIME]
> are ignored.

Looks like this is what I need. Thanks again for your help.


--
Simon Elliott    http://www.ctsn.co.uk





[ Post a follow-up to this message ]



    Re: Serial port: O_NONBLOCK vs VMIN and VTIME  
Floyd L. Davidson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-02-05 12:51 PM

"Simon Elliott" <Simon at ctsn.co.uk> wrote:
>On 02/08/2005, Floyd L. Davidson wrote:
> 
>
>By EOF do you mean DCD dropping?

No.  If you read from a file... EOF of course is an indication
that the read asked for data past the end of the file.  There is
no physical "end of the file" when reading from a serial port,
but unix implements a method (with ICANON set) where a "special
character" (c_cc[VEOF], which is usually ^D) will cause the
device driver to indicate an end of file condition to a read()
request.

All of which is to say, your use of the serial port might never
need to look for an "EOF"... but if it does you'll need to
configure the port so that it can detect EOF!

>I need to read bytes as they are sent, irrespective of DCD, and monitor
>DCD separately via ioctl and TIOCMGET. I hope CRTSCTS implements a
>"real" RTS/CTS flow control with the RTS and CTS lines only, and
>ignores DCD, otherwise I'm going to have to very rapidly learn about
>Linux device drivers!

If you clear CLOCAL and enable CRTSCTS, you will in fact have
effective hardware flow control.  You can use/monitor the other
modem control lines for whatever purpose you like!

Note all of the various ways that you can also use signals to
interact with the device driver too, though using any of them
except perhaps SIGINT and SIGHUP will get complicated.

In addition to the ones I mentioned in the other article, you
can also get SIGIO to notify when a data is received.  Most
people avoid that like the plague...  but there is a good
example of how to use it on my web site at the URL previously
mentioned.
 
>
>Looks like this is what I need. Thanks again for your help.

You've got me scratching my head and opening a few books to make
sure I get this right!  (In addition to APUE, Stevens wrote a
couple volumes on networking and interprocess communications
that most unix programmers can't live without.  He also used to
post to Usenet before his untimely death, and unfortunately all
the rest of us put together can't make up for what we lost.)

--
Floyd L. Davidson            <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         floyd@apaflo.com





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:26 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register