Some experiences of Linux serial IO mode

Source: Internet
Author: User

As we all know, all the devices in the Linux system are in the form of files, and the serial port is the same.

Usually I/O operations are both blocking and non-blocking.


In fact, the concept of "timeout" is a kind of processing method in blocking, which is still the blocking I/O mode.

IO operation of serial port in Linux this article divides it into three states:

Blocking State

Timeout status

Non-blocking state


There are several combinations of these three state transitions:

Blocking-Timeout

Blocking--Non-blocking

Timeout -- blocking

Timeout--Non-blocking

Non-blocking and blocking


Let's analyze it one by one.

First, when a serial port descriptor is opened, it specifies whether its mode is blocking or blocking.

FD = open ("/dev/tttys0", O_RDWR | O_noctty)//opening the serial port with blocking mode FD = open ("/dev/tttys0", O_RDWR | O_noctty | O_ndelay)//Open serial//o_ndelay in non-blocking mode equivalent to O_noblock


When a serial port is blocking the state , it can be set to a timeout state.

Using  struct Termios  cc_t C_cc[nccs] member

    c_cc[vtime ]  timeout for non-canonical mode reads ( units: hundred milliseconds )

    c_cc[vmin ]   minimum number of characters when reading in non-canonical mode

Set timeout if required c_cc[ vmin ] must be equal to 0

? ? This means that the minimum character that can be read is 0, that is, using read to read the data timeout read returns 0 ? ?


There's a place to watch!

when c_cc[Vtime] set to 0 and c_cc[ vmin ] = = 0  , representing timeout 0 seconds (so call it!)

This time using read reads the data will return immediately (have read the data when the number of bytes returned, no data and the general timeout returned 0)

But!

Although the phenomenon appears to be the same as non-blocking mode (read does not block) but the return value is different


non-blocking mode: Read No data immediately returns-1

Time-out 0 seconds: read not reading to data immediately returns 0 ( blocking mode with timeout set)

ret = Read (fd,recvbuf,buf_size), if (ret = =-1)//non-blocking mode "No data returned" {//do Something}ret = read (fd,recvbuf,buf_size); if (ret = = 0)//blocking mode setting timeout 0 seconds "Timeout returned" {//do something}

Although the form of expression, but in the programming must understand which mode and the current state of the serial port to better analyze and deal with the problem.

Here is a question that I have encountered:

I opened the serial port using blocking mode open, but did not set the value of c_cc[VMIN], and it is initialized is 0, so found that the serial port is not blocked, in fact, the reason is the serial port mode or blocking mode Yes, but it is the state of 0 seconds timeout, So when no data arrives, read also returns.



Blocking state and non-blocking state switching

Non-blocking state when used

Fcntl (fd,f_setfl,0);

Can be converted to a blocking state , as well as setting timeouts

When the non-blocking state has timed out, the timeout is also in effect when it is converted to a blocking state

Use when blocking state

Fcntl (fd,f_setfl,fndelay);//fndelay equivalent to Fnonblock

can be converted to non-blocking state , timeout expires


Here's a look at some of the macro definitions in fcntl.h

/* Define Some more compatibility macros to being backward compatible with BSD systems which do not managed to hide these  Kernel macros. */#ifdef__USE_BSD # define fappendo_append# define ffsynco_fsync# define fasynco_async# define fnonblocko_nonblock#  Define FNDELAYO_NDELAY#ENDIF/* use BSD. */

Are you at a glance now? O_ndelay or O_nonblock option for non-blocking mode when opening serial port

Fcntl set the 3rd parameter of non-blocking mode fndelay or Fnonblock is actually O_nonblock mainly for the sake of compatibility


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.